@@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonAnyGetter
66import com.fasterxml.jackson.annotation.JsonAnySetter
77import com.fasterxml.jackson.annotation.JsonCreator
88import com.fasterxml.jackson.annotation.JsonProperty
9+ import com.withorb.api.core.Enum
910import com.withorb.api.core.ExcludeMissing
1011import com.withorb.api.core.JsonField
1112import com.withorb.api.core.JsonMissing
@@ -14,12 +15,13 @@ import com.withorb.api.core.checkRequired
1415import com.withorb.api.errors.OrbInvalidDataException
1516import java.util.Collections
1617import java.util.Objects
18+ import kotlin.jvm.optionals.getOrNull
1719
1820class AccountingProviderConfig
1921@JsonCreator(mode = JsonCreator .Mode .DISABLED )
2022private constructor (
2123 private val externalProviderId: JsonField <String >,
22- private val providerType: JsonField <String >,
24+ private val providerType: JsonField <ProviderType >,
2325 private val additionalProperties: MutableMap <String , JsonValue >,
2426) {
2527
@@ -30,7 +32,7 @@ private constructor(
3032 externalProviderId: JsonField <String > = JsonMissing .of(),
3133 @JsonProperty(" provider_type" )
3234 @ExcludeMissing
33- providerType: JsonField <String > = JsonMissing .of(),
35+ providerType: JsonField <ProviderType > = JsonMissing .of(),
3436 ) : this (externalProviderId, providerType, mutableMapOf ())
3537
3638 /* *
@@ -43,7 +45,7 @@ private constructor(
4345 * @throws OrbInvalidDataException if the JSON field has an unexpected type or is unexpectedly
4446 * missing or null (e.g. if the server responded with an unexpected value).
4547 */
46- fun providerType (): String = providerType.getRequired(" provider_type" )
48+ fun providerType (): ProviderType = providerType.getRequired(" provider_type" )
4749
4850 /* *
4951 * Returns the raw JSON value of [externalProviderId].
@@ -62,7 +64,7 @@ private constructor(
6264 */
6365 @JsonProperty(" provider_type" )
6466 @ExcludeMissing
65- fun _providerType (): JsonField <String > = providerType
67+ fun _providerType (): JsonField <ProviderType > = providerType
6668
6769 @JsonAnySetter
6870 private fun putAdditionalProperty (key : String , value : JsonValue ) {
@@ -94,7 +96,7 @@ private constructor(
9496 class Builder internal constructor() {
9597
9698 private var externalProviderId: JsonField <String >? = null
97- private var providerType: JsonField <String >? = null
99+ private var providerType: JsonField <ProviderType >? = null
98100 private var additionalProperties: MutableMap <String , JsonValue > = mutableMapOf ()
99101
100102 @JvmSynthetic
@@ -118,16 +120,16 @@ private constructor(
118120 this .externalProviderId = externalProviderId
119121 }
120122
121- fun providerType (providerType : String ) = providerType(JsonField .of(providerType))
123+ fun providerType (providerType : ProviderType ) = providerType(JsonField .of(providerType))
122124
123125 /* *
124126 * Sets [Builder.providerType] to an arbitrary JSON value.
125127 *
126- * You should usually call [Builder.providerType] with a well-typed [String ] value instead.
127- * This method is primarily for setting the field to an undocumented or not yet supported
128- * value.
128+ * You should usually call [Builder.providerType] with a well-typed [ProviderType ] value
129+ * instead. This method is primarily for setting the field to an undocumented or not yet
130+ * supported value.
129131 */
130- fun providerType (providerType : JsonField <String >) = apply {
132+ fun providerType (providerType : JsonField <ProviderType >) = apply {
131133 this .providerType = providerType
132134 }
133135
@@ -187,7 +189,7 @@ private constructor(
187189 }
188190
189191 externalProviderId()
190- providerType()
192+ providerType().validate()
191193 validated = true
192194 }
193195
@@ -207,7 +209,143 @@ private constructor(
207209 @JvmSynthetic
208210 internal fun validity (): Int =
209211 (if (externalProviderId.asKnown().isPresent) 1 else 0 ) +
210- (if (providerType.asKnown().isPresent) 1 else 0 )
212+ (providerType.asKnown().getOrNull()?.validity() ? : 0 )
213+
214+ class ProviderType @JsonCreator private constructor(private val value : JsonField <String >) :
215+ Enum {
216+
217+ /* *
218+ * Returns this class instance's raw value.
219+ *
220+ * This is usually only useful if this instance was deserialized from data that doesn't
221+ * match any known member, and you want to know that value. For example, if the SDK is on an
222+ * older version than the API, then the API may respond with new members that the SDK is
223+ * unaware of.
224+ */
225+ @com.fasterxml.jackson.annotation.JsonValue fun _value (): JsonField <String > = value
226+
227+ companion object {
228+
229+ @JvmField val QUICKBOOKS = of(" quickbooks" )
230+
231+ @JvmField val NETSUITE = of(" netsuite" )
232+
233+ @JvmStatic fun of (value : String ) = ProviderType (JsonField .of(value))
234+ }
235+
236+ /* * An enum containing [ProviderType]'s known values. */
237+ enum class Known {
238+ QUICKBOOKS ,
239+ NETSUITE ,
240+ }
241+
242+ /* *
243+ * An enum containing [ProviderType]'s known values, as well as an [_UNKNOWN] member.
244+ *
245+ * An instance of [ProviderType] can contain an unknown value in a couple of cases:
246+ * - It was deserialized from data that doesn't match any known member. For example, if the
247+ * SDK is on an older version than the API, then the API may respond with new members that
248+ * the SDK is unaware of.
249+ * - It was constructed with an arbitrary value using the [of] method.
250+ */
251+ enum class Value {
252+ QUICKBOOKS ,
253+ NETSUITE ,
254+ /* *
255+ * An enum member indicating that [ProviderType] was instantiated with an unknown value.
256+ */
257+ _UNKNOWN ,
258+ }
259+
260+ /* *
261+ * Returns an enum member corresponding to this class instance's value, or [Value._UNKNOWN]
262+ * if the class was instantiated with an unknown value.
263+ *
264+ * Use the [known] method instead if you're certain the value is always known or if you want
265+ * to throw for the unknown case.
266+ */
267+ fun value (): Value =
268+ when (this ) {
269+ QUICKBOOKS -> Value .QUICKBOOKS
270+ NETSUITE -> Value .NETSUITE
271+ else -> Value ._UNKNOWN
272+ }
273+
274+ /* *
275+ * Returns an enum member corresponding to this class instance's value.
276+ *
277+ * Use the [value] method instead if you're uncertain the value is always known and don't
278+ * want to throw for the unknown case.
279+ *
280+ * @throws OrbInvalidDataException if this class instance's value is a not a known member.
281+ */
282+ fun known (): Known =
283+ when (this ) {
284+ QUICKBOOKS -> Known .QUICKBOOKS
285+ NETSUITE -> Known .NETSUITE
286+ else -> throw OrbInvalidDataException (" Unknown ProviderType: $value " )
287+ }
288+
289+ /* *
290+ * Returns this class instance's primitive wire representation.
291+ *
292+ * This differs from the [toString] method because that method is primarily for debugging
293+ * and generally doesn't throw.
294+ *
295+ * @throws OrbInvalidDataException if this class instance's value does not have the expected
296+ * primitive type.
297+ */
298+ fun asString (): String =
299+ _value ().asString().orElseThrow { OrbInvalidDataException (" Value is not a String" ) }
300+
301+ private var validated: Boolean = false
302+
303+ /* *
304+ * Validates that the types of all values in this object match their expected types
305+ * recursively.
306+ *
307+ * This method is _not_ forwards compatible with new types from the API for existing fields.
308+ *
309+ * @throws OrbInvalidDataException if any value type in this object doesn't match its
310+ * expected type.
311+ */
312+ fun validate (): ProviderType = apply {
313+ if (validated) {
314+ return @apply
315+ }
316+
317+ known()
318+ validated = true
319+ }
320+
321+ fun isValid (): Boolean =
322+ try {
323+ validate()
324+ true
325+ } catch (e: OrbInvalidDataException ) {
326+ false
327+ }
328+
329+ /* *
330+ * Returns a score indicating how many valid values are contained in this object
331+ * recursively.
332+ *
333+ * Used for best match union deserialization.
334+ */
335+ @JvmSynthetic internal fun validity (): Int = if (value() == Value ._UNKNOWN ) 0 else 1
336+
337+ override fun equals (other : Any? ): Boolean {
338+ if (this == = other) {
339+ return true
340+ }
341+
342+ return other is ProviderType && value == other.value
343+ }
344+
345+ override fun hashCode () = value.hashCode()
346+
347+ override fun toString () = value.toString()
348+ }
211349
212350 override fun equals (other : Any? ): Boolean {
213351 if (this == = other) {
0 commit comments