All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.algolia.client.model.recommend.Distinct.kt Maven / Gradle / Ivy

Go to download

"Algolia is a powerful search-as-a-service solution, made easy to use with API clients, UI libraries, and pre-built integrations. Algolia API Client for Kotlin lets you easily use the Algolia Search REST API from your JVM project, such as Android or backend implementations."

There is a newer version: 3.10.1
Show newest version
/** Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT. */
package com.algolia.client.model.recommend

import com.algolia.client.exception.AlgoliaClientException
import com.algolia.client.extensions.internal.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.jvm.JvmInline

/**
 * Enables [deduplication or grouping of results (Algolia's _distinct_ feature](https://www.algolia.com/doc/guides/managing-results/refine-results/grouping/#introducing-algolias-distinct-feature)).
 *
 * Implementations:
 * - [Boolean] - *[Distinct.of]*
 * - [Int] - *[Distinct.of]*
 */
@Serializable(DistinctSerializer::class)
public sealed interface Distinct {

  @JvmInline
  public value class BooleanValue(public val value: Boolean) : Distinct

  @JvmInline
  public value class IntValue(public val value: Int) : Distinct

  public companion object {

    /** [Distinct] as [Boolean] Value. */
    public fun of(value: Boolean): Distinct {
      return BooleanValue(value)
    }

    /** [Distinct] as [Int] Value. */
    public fun of(value: Int): Distinct {
      return IntValue(value)
    }
  }
}

internal class DistinctSerializer : KSerializer {

  override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Distinct")

  override fun serialize(encoder: Encoder, value: Distinct) {
    when (value) {
      is Distinct.BooleanValue -> Boolean.serializer().serialize(encoder, value.value)
      is Distinct.IntValue -> Int.serializer().serialize(encoder, value.value)
    }
  }

  override fun deserialize(decoder: Decoder): Distinct {
    val codec = decoder.asJsonDecoder()
    val tree = codec.decodeJsonElement()

    // deserialize Boolean
    if (tree is JsonPrimitive) {
      try {
        val value = codec.json.decodeFromJsonElement(Boolean.serializer(), tree)
        return Distinct.of(value)
      } catch (e: Exception) {
        // deserialization failed, continue
        println("Failed to deserialize Boolean (error: ${e.message})")
      }
    }

    // deserialize Int
    if (tree is JsonPrimitive) {
      try {
        val value = codec.json.decodeFromJsonElement(Int.serializer(), tree)
        return Distinct.of(value)
      } catch (e: Exception) {
        // deserialization failed, continue
        println("Failed to deserialize Int (error: ${e.message})")
      }
    }

    throw AlgoliaClientException("Failed to deserialize json element: $tree")
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy