|
| 1 | +// File generated from our OpenAPI spec by Stainless. |
| 2 | + |
| 3 | +package com.withorb.api.models |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonGenerator |
| 6 | +import com.fasterxml.jackson.core.ObjectCodec |
| 7 | +import com.fasterxml.jackson.databind.JsonNode |
| 8 | +import com.fasterxml.jackson.databind.SerializerProvider |
| 9 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize |
| 10 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize |
| 11 | +import com.fasterxml.jackson.module.kotlin.jacksonTypeRef |
| 12 | +import com.withorb.api.core.BaseDeserializer |
| 13 | +import com.withorb.api.core.BaseSerializer |
| 14 | +import com.withorb.api.core.JsonValue |
| 15 | +import com.withorb.api.core.getOrThrow |
| 16 | +import com.withorb.api.errors.OrbInvalidDataException |
| 17 | +import java.util.Objects |
| 18 | +import java.util.Optional |
| 19 | +import kotlin.jvm.optionals.getOrNull |
| 20 | + |
| 21 | +@JsonDeserialize(using = ConversionRateConfig.Deserializer::class) |
| 22 | +@JsonSerialize(using = ConversionRateConfig.Serializer::class) |
| 23 | +class ConversionRateConfig |
| 24 | +private constructor( |
| 25 | + private val unit: UnitConversionRateConfig? = null, |
| 26 | + private val tiered: TieredConversionRateConfig? = null, |
| 27 | + private val _json: JsonValue? = null, |
| 28 | +) { |
| 29 | + |
| 30 | + fun unit(): Optional<UnitConversionRateConfig> = Optional.ofNullable(unit) |
| 31 | + |
| 32 | + fun tiered(): Optional<TieredConversionRateConfig> = Optional.ofNullable(tiered) |
| 33 | + |
| 34 | + fun isUnit(): Boolean = unit != null |
| 35 | + |
| 36 | + fun isTiered(): Boolean = tiered != null |
| 37 | + |
| 38 | + fun asUnit(): UnitConversionRateConfig = unit.getOrThrow("unit") |
| 39 | + |
| 40 | + fun asTiered(): TieredConversionRateConfig = tiered.getOrThrow("tiered") |
| 41 | + |
| 42 | + fun _json(): Optional<JsonValue> = Optional.ofNullable(_json) |
| 43 | + |
| 44 | + fun <T> accept(visitor: Visitor<T>): T = |
| 45 | + when { |
| 46 | + unit != null -> visitor.visitUnit(unit) |
| 47 | + tiered != null -> visitor.visitTiered(tiered) |
| 48 | + else -> visitor.unknown(_json) |
| 49 | + } |
| 50 | + |
| 51 | + private var validated: Boolean = false |
| 52 | + |
| 53 | + fun validate(): ConversionRateConfig = apply { |
| 54 | + if (validated) { |
| 55 | + return@apply |
| 56 | + } |
| 57 | + |
| 58 | + accept( |
| 59 | + object : Visitor<Unit> { |
| 60 | + override fun visitUnit(unit: UnitConversionRateConfig) { |
| 61 | + unit.validate() |
| 62 | + } |
| 63 | + |
| 64 | + override fun visitTiered(tiered: TieredConversionRateConfig) { |
| 65 | + tiered.validate() |
| 66 | + } |
| 67 | + } |
| 68 | + ) |
| 69 | + validated = true |
| 70 | + } |
| 71 | + |
| 72 | + fun isValid(): Boolean = |
| 73 | + try { |
| 74 | + validate() |
| 75 | + true |
| 76 | + } catch (e: OrbInvalidDataException) { |
| 77 | + false |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns a score indicating how many valid values are contained in this object recursively. |
| 82 | + * |
| 83 | + * Used for best match union deserialization. |
| 84 | + */ |
| 85 | + @JvmSynthetic |
| 86 | + internal fun validity(): Int = |
| 87 | + accept( |
| 88 | + object : Visitor<Int> { |
| 89 | + override fun visitUnit(unit: UnitConversionRateConfig) = unit.validity() |
| 90 | + |
| 91 | + override fun visitTiered(tiered: TieredConversionRateConfig) = tiered.validity() |
| 92 | + |
| 93 | + override fun unknown(json: JsonValue?) = 0 |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + override fun equals(other: Any?): Boolean { |
| 98 | + if (this === other) { |
| 99 | + return true |
| 100 | + } |
| 101 | + |
| 102 | + return other is ConversionRateConfig && unit == other.unit && tiered == other.tiered |
| 103 | + } |
| 104 | + |
| 105 | + override fun hashCode(): Int = Objects.hash(unit, tiered) |
| 106 | + |
| 107 | + override fun toString(): String = |
| 108 | + when { |
| 109 | + unit != null -> "ConversionRateConfig{unit=$unit}" |
| 110 | + tiered != null -> "ConversionRateConfig{tiered=$tiered}" |
| 111 | + _json != null -> "ConversionRateConfig{_unknown=$_json}" |
| 112 | + else -> throw IllegalStateException("Invalid ConversionRateConfig") |
| 113 | + } |
| 114 | + |
| 115 | + companion object { |
| 116 | + |
| 117 | + @JvmStatic fun ofUnit(unit: UnitConversionRateConfig) = ConversionRateConfig(unit = unit) |
| 118 | + |
| 119 | + @JvmStatic |
| 120 | + fun ofTiered(tiered: TieredConversionRateConfig) = ConversionRateConfig(tiered = tiered) |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * An interface that defines how to map each variant of [ConversionRateConfig] to a value of |
| 125 | + * type [T]. |
| 126 | + */ |
| 127 | + interface Visitor<out T> { |
| 128 | + |
| 129 | + fun visitUnit(unit: UnitConversionRateConfig): T |
| 130 | + |
| 131 | + fun visitTiered(tiered: TieredConversionRateConfig): T |
| 132 | + |
| 133 | + /** |
| 134 | + * Maps an unknown variant of [ConversionRateConfig] to a value of type [T]. |
| 135 | + * |
| 136 | + * An instance of [ConversionRateConfig] can contain an unknown variant if it was |
| 137 | + * deserialized from data that doesn't match any known variant. For example, if the SDK is |
| 138 | + * on an older version than the API, then the API may respond with new variants that the SDK |
| 139 | + * is unaware of. |
| 140 | + * |
| 141 | + * @throws OrbInvalidDataException in the default implementation. |
| 142 | + */ |
| 143 | + fun unknown(json: JsonValue?): T { |
| 144 | + throw OrbInvalidDataException("Unknown ConversionRateConfig: $json") |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + internal class Deserializer : |
| 149 | + BaseDeserializer<ConversionRateConfig>(ConversionRateConfig::class) { |
| 150 | + |
| 151 | + override fun ObjectCodec.deserialize(node: JsonNode): ConversionRateConfig { |
| 152 | + val json = JsonValue.fromJsonNode(node) |
| 153 | + val conversionRateType = |
| 154 | + json.asObject().getOrNull()?.get("conversion_rate_type")?.asString()?.getOrNull() |
| 155 | + |
| 156 | + when (conversionRateType) { |
| 157 | + "unit" -> { |
| 158 | + return tryDeserialize(node, jacksonTypeRef<UnitConversionRateConfig>())?.let { |
| 159 | + ConversionRateConfig(unit = it, _json = json) |
| 160 | + } ?: ConversionRateConfig(_json = json) |
| 161 | + } |
| 162 | + "tiered" -> { |
| 163 | + return tryDeserialize(node, jacksonTypeRef<TieredConversionRateConfig>())?.let { |
| 164 | + ConversionRateConfig(tiered = it, _json = json) |
| 165 | + } ?: ConversionRateConfig(_json = json) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return ConversionRateConfig(_json = json) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + internal class Serializer : BaseSerializer<ConversionRateConfig>(ConversionRateConfig::class) { |
| 174 | + |
| 175 | + override fun serialize( |
| 176 | + value: ConversionRateConfig, |
| 177 | + generator: JsonGenerator, |
| 178 | + provider: SerializerProvider, |
| 179 | + ) { |
| 180 | + when { |
| 181 | + value.unit != null -> generator.writeObject(value.unit) |
| 182 | + value.tiered != null -> generator.writeObject(value.tiered) |
| 183 | + value._json != null -> generator.writeObject(value._json) |
| 184 | + else -> throw IllegalStateException("Invalid ConversionRateConfig") |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments