Skip to content
This repository has been archived by the owner on Aug 20, 2023. It is now read-only.

Implement type-safe custom fields #1

Merged
merged 8 commits into from
Mar 16, 2022
Merged

Implement type-safe custom fields #1

merged 8 commits into from
Mar 16, 2022

Conversation

Akii
Copy link
Owner

@Akii Akii commented Mar 9, 2022

Basic idea:

@Generated
public sealed interface OrderCustomFieldsType : CustomFields

@Generated
public sealed interface CategoryCustomFieldsType : CustomFields

@Generated
public class TypeACustomFields @JsonCreator constructor(
  @JsonProperty("custom")
  custom: CustomFieldsImpl,
  @JsonProperty("typedFields")
  public val typedFields: Fields
) : CustomFields by custom, OrderCustomFieldsType, CategoryCustomFieldsType {
  @Generated
  public data class Fields @JsonCreator constructor(
    @JsonProperty("aBoolean")
    public val aBoolean: Boolean?,
    @JsonProperty("aReference")
    public val aReference: ProductReference?
  )
}

@Generated
public class TypeBCustomFields @JsonCreator constructor(
  @JsonProperty("custom")
  custom: CustomFieldsImpl,
  @JsonProperty("typedFields")
  public val typedFields: Fields
) : CustomFields by custom, CategoryCustomFieldsType {
  @Generated
  public data class Fields @JsonCreator constructor(
    @JsonProperty("anotherBoolean")
    public val anotherBoolean: Boolean?
  )
}

val foo: OrderCustomFieldsType = null as OrderCustomFieldsType

// check is exhaustive
when (foo) {
    is TypeACustomFields -> foo.typedFields.aBoolean
}

@Akii Akii marked this pull request as ready for review March 16, 2022 18:03
@Akii Akii merged commit b1ee21d into main Mar 16, 2022
@Akii Akii deleted the feature/custom-fields branch March 16, 2022 18:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant