arrow-kt / arrow-integrations

Λrrow Integrations is part of Λrrow, a functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
27 stars 6 forks source link

generify tristate module as reusable module #108

Open myuwono opened 1 year ago

myuwono commented 1 year ago

In this PR

fixes #107

Context

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

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