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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli tool compiles to 0.17 and doesn't include imports #33

Open
MarcCoquand opened this issue Feb 5, 2018 · 0 comments
Open

Cli tool compiles to 0.17 and doesn't include imports #33

MarcCoquand opened this issue Feb 5, 2018 · 0 comments

Comments

@MarcCoquand
Copy link

When trying to compile a json file with the following command

python json-to-elm/generate.py src/shared/person.json > src/client/decoders/Person.elm

It generates the following:

type alias Person =
    { name : String
    , age : Int
    , location : PersonLocation
    }

type alias PersonLocation =
    { name : String
    , days : Int
    }

decodePerson : Json.Decode.Decoder Person
decodePerson =
    Json.Decode.succeed Person
        |: ("name" := Json.Decode.string)
        |: ("age" := Json.Decode.int)
        |: ("location" := decodePersonLocation)

decodePersonLocation : Json.Decode.Decoder PersonLocation
decodePersonLocation =
    Json.Decode.succeed PersonLocation
        |: ("name" := Json.Decode.string)
        |: ("days" := Json.Decode.int)

encodePerson : Person -> Json.Encode.Value
encodePerson record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("age",  Json.Encode.int <| record.age)
        , ("location",  encodePersonLocation <| record.location)
        ]

encodePersonLocation : PersonLocation -> Json.Encode.Value
encodePersonLocation record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("days",  Json.Encode.int <| record.days)
        ]

whereas I would expect it to compile to the latest version as follows:

import Json.Encode
import Json.Decode
import Json.Decode.Pipeline

type alias Person =
    { name : String
    , age : Int
    , location : PersonLocation
    }

type alias PersonLocation =
    { name : String
    , days : Int
    }

decodePerson : Json.Decode.Decoder Person
decodePerson =
    Json.Decode.Pipeline.decode Person
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "age" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "location" (decodePersonLocation)

decodePersonLocation : Json.Decode.Decoder PersonLocation
decodePersonLocation =
    Json.Decode.Pipeline.decode PersonLocation
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "days" (Json.Decode.int)

encodePerson : Person -> Json.Encode.Value
encodePerson record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("age",  Json.Encode.int <| record.age)
        , ("location",  encodePersonLocation <| record.location)
        ]

encodePersonLocation : PersonLocation -> Json.Encode.Value
encodePersonLocation record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("days",  Json.Encode.int <| record.days)
        ]

or at least add options to include imports and which version to compile to.

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

1 participant