Add deriving for unions with a single constructor #134
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There seems to be a hole in the handling of union types in the TemplateHaskell derivation of the Avro types. Namely, there is an unstated assumption that the size of the unions are at least 2.
Moreover, running
deriveAvro
on an .avsc schema that contains exactly one constructor results in the confusing error messageAvro unions with a single constructor aren't explicitly mentioned in the specification, but it does say that
which does not rule out the length 1 array case. This is corroborated by the reference implementation used by
avro-tools
, which does generate length 1 arrays from.avdl
schemata containingunion
declarations with a single element.This PR modifies
deriveAvro
to wrap such unions in theIdentity
functor, serving as the degenerate case of theEitherN
collection of types. It also provides more helpful error messages for union typeswith an unsupported number of constructors.
I wasn't sure if using
Identity
is the best way to deal with this, or if you would prefer anEither1
newtype, but this can be easily changed!