id | title |
---|---|
scala-codecs |
Scala Json Codecs Backend |
This document provides a description of the JSON codecs backend.
The Json Codecs Backend for Scala contains functions to generate Codecs from types in the IR.
Circe is used as the base JSON library.
graph TD;
Backend-->Core;
Backend-->Features
The Scala backend is split into two aspects: \
- Core - the core Scala codes representing the user business logic
- Feature - the Codecs for each type defined in the input model
This section describes the mapping of Elm types to Scala Codec. It's important to note that all codecs generated from the Elm types are Scala values defined in the Scala AST. The Codec files generated contains value declarations as lamda functions. Below is an outline of how each type is mapped
Variable types in Elm are mapped to Scala Variables
Reference types in Elm are mapped to Scala Ref which uses the Elm path and name to create a Scala Ref value
Not completed yet
An Elm record type maps to Circe object which consists of a list of fields which are applied to the Circe.json.obj function.
Similar to a record
Functions are currently not mapped.
Functions are currently not mapped.
Codecs for custom types are composed using helper functions.
The following functions are defined in the Scala Json Codecs backend
This function takes a modules definition and generates the encoders and decoders for the types defined in that module
Generates and encoder reference for the input type using the FQName
Generates and decoder reference for the input type using the FQName
Type definition could be any of the following:
Type Alias Definition - maps to an encoder for record type
Custom Type Definition - uses helper functions to build encoder for custom
types \
Type definition could be any of the following:
Type Alias Definition - maps to an encoder for record type
Custom Type Definition - uses helper functions to build encoder for custom
types \
Builds encoder for custom type using pattern matching
Builds a decoder for custom type using pattern mathing on the type constructors.
Builds an encoder for one member of a union type
Builds a decoder for on member of a union type using for comprehension.
Codecs can be generated conditionally by adding the --s flag to the morphir-elm gen command.