spartanz / schemaz

A purely-functional library for defining type-safe schemas for algebraic data types, providing free generators, SQL queries, JSON codecs, binary codecs, and migration from this schema definition
https://spartanz.github.io/schemaz
Apache License 2.0
164 stars 18 forks source link

Schema serialisation #30

Open vil1 opened 5 years ago

vil1 commented 5 years ago

Schemas must be readable at runtime. In other words, we must provide a way to read/write schemas from/to the wire.

Schemas composed only of One, :+: and :*: (so-called "essential schemas") are (almost) trivially serialisable. But schemas that contain Isos (namely RecordSchema, Union, SeqSchema, etc.) aren't, since Isos are – roughly – functions, and functions aren't trivially serialisable.

On the other hand, these "iso-based" GADT members are merely a way to build schemas for user-defined Scala classes, which is only a part of our intended use cases. We also want to provide a safe way to deal with "dynamic data", eg. processing JSON documents (or Avro records, etc.) at runtime without necessarily having to coerce them to a case class.

These "dynamic" use cases are the ones that are most likely to require schema serialisation, so providing a way to serialise "essential schemas" only sounds like an acceptable first step.

vil1 commented 5 years ago

So, after "brainstorming#2", we've decided to first try (de)serializing schemas while ignoring the "isos" (that can be found in Union, Record, IsoSchema).

This can be done by simply not writing the iso field of these classes, and put Iso.refl (the identity Iso) on the read side.

As a bonus, this could be done "automatically" by defining a schema for Schema and deriving codecs from it (eating our own dogfood).