@@ -11,6 +11,7 @@ import com.tryfinch.api.core.ExcludeMissing
1111import com.tryfinch.api.core.JsonField
1212import com.tryfinch.api.core.JsonMissing
1313import com.tryfinch.api.core.JsonValue
14+ import com.tryfinch.api.core.toImmutable
1415import com.tryfinch.api.errors.FinchInvalidDataException
1516import java.util.Collections
1617import java.util.Objects
@@ -221,7 +222,7 @@ private constructor(
221222 class Attributes
222223 private constructor (
223224 private val employer: JsonField <Boolean >,
224- private val metadata: JsonValue ,
225+ private val metadata: JsonField < Metadata > ,
225226 private val preTax: JsonField <Boolean >,
226227 private val type: JsonField <String >,
227228 private val additionalProperties: MutableMap <String , JsonValue >,
@@ -232,7 +233,9 @@ private constructor(
232233 @JsonProperty(" employer" )
233234 @ExcludeMissing
234235 employer: JsonField <Boolean > = JsonMissing .of(),
235- @JsonProperty(" metadata" ) @ExcludeMissing metadata: JsonValue = JsonMissing .of(),
236+ @JsonProperty(" metadata" )
237+ @ExcludeMissing
238+ metadata: JsonField <Metadata > = JsonMissing .of(),
236239 @JsonProperty(" pre_tax" ) @ExcludeMissing preTax: JsonField <Boolean > = JsonMissing .of(),
237240 @JsonProperty(" type" ) @ExcludeMissing type: JsonField <String > = JsonMissing .of(),
238241 ) : this (employer, metadata, preTax, type, mutableMapOf ())
@@ -248,8 +251,11 @@ private constructor(
248251 /* *
249252 * The metadata of the pay statement item derived by the rules engine if available. Each
250253 * attribute will be a key-value pair defined by a rule.
254+ *
255+ * @throws FinchInvalidDataException if the JSON field has an unexpected type (e.g. if the
256+ * server responded with an unexpected value).
251257 */
252- @JsonProperty( " metadata " ) @ExcludeMissing fun _metadata (): JsonValue = metadata
258+ fun metadata (): Optional < Metadata > = metadata.getOptional( " metadata " )
253259
254260 /* *
255261 * `true` if the pay statement item is pre-tax. This field is only available for employee
@@ -275,6 +281,13 @@ private constructor(
275281 */
276282 @JsonProperty(" employer" ) @ExcludeMissing fun _employer (): JsonField <Boolean > = employer
277283
284+ /* *
285+ * Returns the raw JSON value of [metadata].
286+ *
287+ * Unlike [metadata], this method doesn't throw if the JSON field has an unexpected type.
288+ */
289+ @JsonProperty(" metadata" ) @ExcludeMissing fun _metadata (): JsonField <Metadata > = metadata
290+
278291 /* *
279292 * Returns the raw JSON value of [preTax].
280293 *
@@ -311,7 +324,7 @@ private constructor(
311324 class Builder internal constructor() {
312325
313326 private var employer: JsonField <Boolean > = JsonMissing .of()
314- private var metadata: JsonValue = JsonMissing .of()
327+ private var metadata: JsonField < Metadata > = JsonMissing .of()
315328 private var preTax: JsonField <Boolean > = JsonMissing .of()
316329 private var type: JsonField <String > = JsonMissing .of()
317330 private var additionalProperties: MutableMap <String , JsonValue > = mutableMapOf ()
@@ -354,7 +367,19 @@ private constructor(
354367 * The metadata of the pay statement item derived by the rules engine if available. Each
355368 * attribute will be a key-value pair defined by a rule.
356369 */
357- fun metadata (metadata : JsonValue ) = apply { this .metadata = metadata }
370+ fun metadata (metadata : Metadata ? ) = metadata(JsonField .ofNullable(metadata))
371+
372+ /* * Alias for calling [Builder.metadata] with `metadata.orElse(null)`. */
373+ fun metadata (metadata : Optional <Metadata >) = metadata(metadata.getOrNull())
374+
375+ /* *
376+ * Sets [Builder.metadata] to an arbitrary JSON value.
377+ *
378+ * You should usually call [Builder.metadata] with a well-typed [Metadata] value
379+ * instead. This method is primarily for setting the field to an undocumented or not yet
380+ * supported value.
381+ */
382+ fun metadata (metadata : JsonField <Metadata >) = apply { this .metadata = metadata }
358383
359384 /* *
360385 * `true` if the pay statement item is pre-tax. This field is only available for
@@ -432,6 +457,7 @@ private constructor(
432457 }
433458
434459 employer()
460+ metadata().ifPresent { it.validate() }
435461 preTax()
436462 type()
437463 validated = true
@@ -454,9 +480,118 @@ private constructor(
454480 @JvmSynthetic
455481 internal fun validity (): Int =
456482 (if (employer.asKnown().isPresent) 1 else 0 ) +
483+ (metadata.asKnown().getOrNull()?.validity() ? : 0 ) +
457484 (if (preTax.asKnown().isPresent) 1 else 0 ) +
458485 (if (type.asKnown().isPresent) 1 else 0 )
459486
487+ /* *
488+ * The metadata of the pay statement item derived by the rules engine if available. Each
489+ * attribute will be a key-value pair defined by a rule.
490+ */
491+ class Metadata
492+ @JsonCreator
493+ private constructor (
494+ @com.fasterxml.jackson.annotation.JsonValue
495+ private val additionalProperties: Map <String , JsonValue >
496+ ) {
497+
498+ @JsonAnyGetter
499+ @ExcludeMissing
500+ fun _additionalProperties (): Map <String , JsonValue > = additionalProperties
501+
502+ fun toBuilder () = Builder ().from(this )
503+
504+ companion object {
505+
506+ /* * Returns a mutable builder for constructing an instance of [Metadata]. */
507+ @JvmStatic fun builder () = Builder ()
508+ }
509+
510+ /* * A builder for [Metadata]. */
511+ class Builder internal constructor() {
512+
513+ private var additionalProperties: MutableMap <String , JsonValue > = mutableMapOf ()
514+
515+ @JvmSynthetic
516+ internal fun from (metadata : Metadata ) = apply {
517+ additionalProperties = metadata.additionalProperties.toMutableMap()
518+ }
519+
520+ fun additionalProperties (additionalProperties : Map <String , JsonValue >) = apply {
521+ this .additionalProperties.clear()
522+ putAllAdditionalProperties(additionalProperties)
523+ }
524+
525+ fun putAdditionalProperty (key : String , value : JsonValue ) = apply {
526+ additionalProperties.put(key, value)
527+ }
528+
529+ fun putAllAdditionalProperties (additionalProperties : Map <String , JsonValue >) =
530+ apply {
531+ this .additionalProperties.putAll(additionalProperties)
532+ }
533+
534+ fun removeAdditionalProperty (key : String ) = apply {
535+ additionalProperties.remove(key)
536+ }
537+
538+ fun removeAllAdditionalProperties (keys : Set <String >) = apply {
539+ keys.forEach(::removeAdditionalProperty)
540+ }
541+
542+ /* *
543+ * Returns an immutable instance of [Metadata].
544+ *
545+ * Further updates to this [Builder] will not mutate the returned instance.
546+ */
547+ fun build (): Metadata = Metadata (additionalProperties.toImmutable())
548+ }
549+
550+ private var validated: Boolean = false
551+
552+ fun validate (): Metadata = apply {
553+ if (validated) {
554+ return @apply
555+ }
556+
557+ validated = true
558+ }
559+
560+ fun isValid (): Boolean =
561+ try {
562+ validate()
563+ true
564+ } catch (e: FinchInvalidDataException ) {
565+ false
566+ }
567+
568+ /* *
569+ * Returns a score indicating how many valid values are contained in this object
570+ * recursively.
571+ *
572+ * Used for best match union deserialization.
573+ */
574+ @JvmSynthetic
575+ internal fun validity (): Int =
576+ additionalProperties.count { (_, value) -> ! value.isNull() && ! value.isMissing() }
577+
578+ override fun equals (other : Any? ): Boolean {
579+ if (this == = other) {
580+ return true
581+ }
582+
583+ return /* spotless:off */ other is Metadata && additionalProperties == other.additionalProperties /* spotless:on */
584+ }
585+
586+ /* spotless:off */
587+ private val hashCode: Int by lazy { Objects .hash(additionalProperties) }
588+ /* spotless:on */
589+
590+ override fun hashCode (): Int = hashCode
591+
592+ override fun toString () = " Metadata{additionalProperties=$additionalProperties }"
593+ }
594+
460595 override fun equals (other : Any? ): Boolean {
461596 if (this == = other) {
462597 return true
0 commit comments