composewell / streamly

High performance, concurrent functional programming abstractions
https://streamly.composewell.com
Other
856 stars 64 forks source link

Serialization template haskell helpers should handle constructor order and name change #2592

Open adithyaov opened 11 months ago

adithyaov commented 11 months ago

We should have another config option that can define the order of constructors so the tag for the constructor is stable.

For example, say we have a datatype:

data List a
    = Nil
    | Cons a (List a)

Say the datatype is updated in a later version to,

data List a
    = Cons a (List a)
    | Nil

The tag given to Cons and Nil will differ. The upgrade process will become rather complex because of this.

We should have another config option named constructorOrder to define the order of constructors manually.

$(deriveSerializeWith
    (defaultConfig { constructorOrder = [''Nil, ''Cons] })
    [d|instance Serialize a => Serialize (List a)|])

This can also possibly be solved by constructorTagAsString if the names of the constructors don't change. constructorOrder is more powerful as it also handles any constructor name changes and is more performant than the former.

adithyaov commented 11 months ago

Practical use-case: bytestring-0.10:

data ByteString = Empty | Chunk_ Strict.Bytestring

bytestring-0.12

data ByteString = Empty | Chunk Strict.Bytestring
adithyaov commented 7 months ago

Pushing this to 0.12.0. This is a nice feature to have but we can think about it later.