Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generify tristate module as reusable module #108

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

myuwono
Copy link
Collaborator

@myuwono myuwono commented Nov 27, 2022

In this PR

  • introduce GenericTriStateModule<T> as a base of tristate (undefined | null | value) serde.

fixes #107

Context

There are times where users need to model an API that need differentiate between:

  • absence of a field
  • presence of a field in json, but being set to null
  • presence of a field in json, with a value set

Option is a very specific case of the above where both absence and presence of a field with a null value indicates a None.
However it's important to note that these generalizations sometimes do not apply. For instance for PATCH endpoints where null means remove, and undefined means do nothing. In this PR we're extracting out this functionality as a separate module that users will be able to customize to support their requirements accordingly.

An example usage of the GenericTriStateModule<T> to form an option module is as follows:

val optionModule: GenericTriStateModule<Option<*>> =
  GenericTriStateModule(
    serializationConfig =
      GenericTriStateSerializationConfig(
        isPresent = { it.isDefined() },
        serializeValue = { option ->
          option.fold({ SerializedValue.AbsentOrNull }, { value -> SerializedValue.Value(value) })
        }
      ),
    deserializationConfig =
      GenericTriStateDeserializationConfig(
        ifAbsent = { None },
        ifNull = { None },
        ifDefined = { it.some() }
      )
  )

Out of scope

  • Defining a data model specifically for tri-state still need to be discussed with the team

@myuwono myuwono requested a review from i-walker November 27, 2022 11:55
}
}

public object OptionSerializerResolver : Serializers.Base() {
Copy link
Member

@i-walker i-walker Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these public members in this file need to have a deprecation cycle

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @i-walker that's a really good point. I'll defer the refactoring of option module as another issue entirely. I think several of these modules access modifier shouldn't have been public. I'll address that separately after this PR.

@myuwono myuwono force-pushed the issue/generic-tristate-module branch from 468b78b to e5a26e6 Compare December 1, 2022 12:48
@myuwono myuwono marked this pull request as draft December 14, 2022 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for generic tristate (undefined | null | defined) codec
2 participants