Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for JSON decoders #58

Open
MattiaVerticchio opened this issue Oct 29, 2022 · 1 comment
Open

Example for JSON decoders #58

MattiaVerticchio opened this issue Oct 29, 2022 · 1 comment

Comments

@MattiaVerticchio
Copy link

Hi, and thanks for making elm-codegen! 馃榿

If I understood correctly, one of its goals is boilerplate or repetitive code generation, such as JSON codecs or encoders-decoders. However, It isn鈥檛 clear how to use regular Elm types to generate an encoder or decoder, and it would be really helpful to have this common use case in the docs.

Is it possible to add an example to the README.md?

@simonh1000
Copy link

Seconded
I've been trying a couple of hours and got to

renderFile =
    let
        anonFn : Expression -> Expression
        anonFn _ =
            Elm.fn ( "name", Nothing )
                (\firstArgument -> Elm.record [ ( "name", firstArgument ) ])

        setter =
            Elm.Declare.value "decodeHighlight"
                (Gen.Json.Decode.map anonFn Gen.Json.Decode.string)
    in
    Elm.file [ "MyFile" ]
        [ setter.declaration
        ]

but that generates

decodeHighlight : Json.Decode.Decoder (name -> { name : name })
decodeHighlight =
    Json.Decode.map (\mapUnpack -> \name -> { name = name }) Json.Decode.string

which is \mapUnpack -> too much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants