flachnetz / poly

Poly helps with polymorphic (de)serialization to and from json in golang.
MIT License
0 stars 0 forks source link

Go Reference

Poly

Simple polymorphic serialization and deserialization.

type Speaker interface{ Speak() }

type Dog struct { IsDog bool }
func (Dog) Speak() {}

type Cat struct { IsCat bool }
func (Cat) Speak() {}

var animal poly.Poly[Speaker, poly.TypeList[Dog, poly.TypeList[Cat, poly.Nil]]]

// write a dog to json
animal.Value = Dog{}
bytes, _ := json.Marshal(animal)

// deserialize the dog
_ = json.Unmarshal(bytes, &animal)
animal.Value.Speak() // wuff