janwirth / elm-coder-generator

Generate encoders and decoders for elm/json
MIT License
12 stars 1 forks source link

Elm coder generator

Builds JSON decoders and encoders for your Elm types.

Demo using the kakoune editor piping to the CLI.

asciicast

Coverage

Module decls let decls lambdas branches
total 96/151 (64%) 117/168 (70%) 10/13 (77%) 190/436 (44%)

Installation

npm i -g elm-coder-generator

CLI

Generate decoders for Example.elm (available in this repo):

It grabs and generates code only for type definitions inside these annotations.

elm-coder-generator Example.elm

Or pipe to elm-coder-generator:

echo '-- [generator-start]\n type X = Int\n-- [generator-end]' | elm-coder-generator 

output

-- [generator-start]
 type X = Int

-- [elm-generator-start] -- DO NOT MODIFY or remove this line
decodeX =
   Decode.int

encodeX a =
   Encode.int a 
-- [generator-end]

API

const generate = require('elm-coder-generator')

generate('-- [generator-start]\n type X = Int\n -- [generator-end]').then(x => console.log(x))

// -> decodeX =
// ->    Decode.int
// -> 
// -> encodeX a =
// ->    Encode.int a

Development

Clone the repo and build Cli.elm:

$ git clone https://github.com/franzskuffka/elm-coder-generator
$ cd elm-coder-generator
$ elm make src/Cli.elm --output elm-stuff/Cli.js --optimize

Roadmap

Acknowledgements

Special thanks to Dániel Kodaj @dkodaj for building the meat of the coder generator.

Of course, thanks to @evancz and @stoeffel.