Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provide a way to generate Elm types, encoders, and decoders for json-spec Specifications
Generally you will probably want elmDefs
, but sometimes you might
want to directly use the methods of HasType
.
Since not every part of a Specification
may have a name, we can
generate encoders and decoders for anonymous Elm types like records,
as well as named Elm types and type aliases. This package figures out
how to name things given the following rules:
- If a name appears in a
JsonLet
binding, then it gets a name in Elm as a type or type alias. - If a second
JsonLet
binding, with exactly one definition, of the formJsonLet '[ '(name, def) ] (JsonRef name)
appears as the RHS of aJsonLet
binding, then that is interpreted as a constructor name, and the generated Elm definition will be a regular type instead of a type alias. SeeNamed
for an easy shorthand way to spellJsonLet '[ '(name, def) ] (JsonRef name)
- For any
Named
leaf of a tree ofJsonEither
s, the name is interpreted as a data constructor name, otherwise a data constructor name is auto-generated.
Examples:
Type alias
The specification
Named "MyType" JsonString
will produce the Elm type
type alias MyType = String
Type with a constructor
The specification
Named "MyType" (Named "MyDataConstructor" JsonString)
will produce the Elm type
type MyType = MyDataConstructor String
Sum Type
Note that the root of a tree of JsonEither
s must be named, because
Elm has no way to represent anonymous sum types.
The specification
Named "MySumType" ( JsonEither (Named "AnInt" JsonInt) ( JsonEither JsonFloat -- note the omitted name ( Named "AString" JsonString) ) )
will produce the Elm type
type MySumType = AnInt Int | MySumType_2 Float -- auto-generated constructor name. | AString String
Producing actual Elm code
This package gets you as far as having a collection of
Definition
s in hand, which come from the 'elm-syntax'
package. You will need to use the pretty printing
features of that package to actually produce code. See
https://hackage.haskell.org/package/elm-syntax/docs/Language-Elm-Pretty.html,
or you can look at the source code for the tests in this package.
Synopsis
- elmDefs :: forall (spec :: Specification). HasType spec => Proxy spec -> Set Definition
- type Definitions = Writer (Set Definition)
- class HasType (spec :: Specification) where
- typeOf :: Definitions (Type v)
- decoderOf :: Definitions (Expression Void)
- encoderOf :: Definitions (Expression Void)
- type Named (name :: Symbol) (def :: Specification) = 'JsonLet '['(name, def)] ('JsonRef name)
Documentation
elmDefs :: forall (spec :: Specification). HasType spec => Proxy spec -> Set Definition Source #
Generate Elm type, encoder, and decoder Definition
s for all named
types in a Specification
. Note that this will not produce any types,
decoders, or encoders for anonymous parts of the Specification
,
since we wouldn't know what to names to give those things in Elm.
type Definitions = Writer (Set Definition) Source #
class HasType (spec :: Specification) where Source #
Translates Specification
s into "anonymous" Elm types (where
"anonymous" really means the RHS of a definition, which could be truly
anonymous but might in fact be a reference to something previously named
Definition
).
typeOf :: Definitions (Type v) Source #
Produce the anonymous Elm type for the spec, collecting any necessary
Definition
s along the way.
decoderOf :: Definitions (Expression Void) Source #
Produce the Elm Decode for the spec, collecting any necessary
Definition
s along the way
encoderOf :: Definitions (Expression Void) Source #
Produce the Elm Encoder for the spec, collecting any necessary
Definition
s along the way.