GithubHelp home page GithubHelp logo

cpulvermacher / elm-auto-encoder-decoder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from choonkeat/elm-auto-encoder-decoder

0.0 0.0 0.0 161 KB

cli to watch *.elm file changes and generate Elm encoder and decoders for each type in the source code. used in https://github.com/choonkeat/elm-webapp

Home Page: https://www.npmjs.com/package/elm-auto-encoder-decoder

JavaScript 2.00% Elm 97.67% Makefile 0.34%

elm-auto-encoder-decoder's Introduction

elm-auto-encoder-decoder

Generate automatic encoder and decoder. Leveraged by choonkeat/elm-webapp Client and Server instances to pass Msg to and fro.

NOTE: While the generated decoders can always decode json encoded by the same version generator, there is no guarantee future versions can decode whatever was encoded in prior versions. :. If you're storing json for later retrieval, I do not recommend using the encoder/decoder here; I don't.

Usage

npx elm-auto-encoder-decoder src/Types.elm

any time src/Types.elm file changes, src/Types/Auto.elm will be regenerated.

ENV variable configurations

GENERATED_SRC will write Auto.elm files to a different directory than the source files, while keeping the directory hierarchy. e.g. GENERATED_SRC=generated/src

WATCHING when set to false will exit after a single run, instead of hanging around waiting for file changes. e.g. WATCHING=false

EXTRA_IMPORT is written to all Auto.elm files if present, e.g. EXTRA_IMPORT='Time.Extra excposing (..)'

How it works

An Elm module Foo.Bar like below

module Foo.Bar exposing (..)

type alias Choice =
    Option Bool


type Option a
    = None
    | Some a

will be encoded and decoded as JSON like

["Foo.Bar.None"]
["Foo.Bar.Some",true]

by the generated Elm functions in the corresponding Foo.Bar.Auto module function

Auto encoder decoders of related types

If src/A.elm imports from src/B.elm, its better to provide both filenames to the cli. The generated encoder/decoder of A will automatically reference the generated encoder/decoder of B

Exposed types

No encoder/decoder functions will be generated for types that are not fully exposed. i.e. a custom type has to be exposed with (..) e.g. MyType(..).

However, if there are exposed types that are composed from unexposed types, you're expected to supply the encoder/decoder functions manually by naming convention (see next point)

Don't be alarmed with "I cannot find ... variable" compiler errors

If your src/Types.elm contain types imported from other modules, e.g. Time.Posix

type alias User = { name : String, createdAt : Time.Posix }

the generated code will likely not compile

-- NAMING ERROR --------------------------------------------- src/Types/Auto.elm

I cannot find a `decodeTimePosix` variable:

248|         |> Json.Decode.map2 (|>) (Json.Decode.at [ "createdAt" ] (decodeTimePosix))
                                                                       ^^^^^^^^^^^^^^^
-- NAMING ERROR --------------------------------------------- src/Types/Auto.elm

I cannot find a `encodeTimePosix` variable:

197|         , ("createdAt", (encodeTimePosix) value.createdAt)
                              ^^^^^^^^^^^^^^^

just implement and expose those function names in your source src/Types.elm (or a different file specified by EXTRA_IMPORT env), for example:

decodeTimePosix : Json.Decode.Decoder Time.Posix
decodeTimePosix =
    Json.Decode.int
        |> Json.Decode.map Time.millisToPosix


encodeTimePosix : Time.Posix -> Json.Encode.Value
encodeTimePosix t =
    Json.Encode.int (Time.posixToMillis t)

elm-auto-encoder-decoder's People

Contributors

choonkeat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.