streamly icon indicating copy to clipboard operation
streamly copied to clipboard

Serialization template haskell helpers should handle constructor order and name change

Open adithyaov opened this issue 2 years ago • 2 comments
trafficstars

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 avatar Sep 24 '23 15:09 adithyaov

Practical use-case: bytestring-0.10:

data ByteString = Empty | Chunk_ Strict.Bytestring

bytestring-0.12

data ByteString = Empty | Chunk Strict.Bytestring

adithyaov avatar Sep 26 '23 10:09 adithyaov

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

adithyaov avatar Jan 14 '24 13:01 adithyaov