Holmusk / elm-street

:deciduous_tree: Crossing the road between Haskell and Elm
Mozilla Public License 2.0
88 stars 6 forks source link

Support Json.Decode.Value as primitive #114

Closed jhrcek closed 3 years ago

jhrcek commented 3 years ago

An attempt to address https://github.com/Holmusk/elm-street/issues/111

turboMaCk commented 3 years ago

are you sure this would work when used in declaration? Types module does not import Json.Decode/Encode so Value type is not defined within that module https://github.com/Holmusk/elm-street/blob/cd9df51812c6bba0b9a7fb2ec4c421bba0ccaf61/src/Elm/Generate.hs#L121-L126

jhrcek commented 3 years ago

@turboMaCk not sure it you're looking at all the commits in this PR. I extended that import list with appropriate import in https://github.com/Holmusk/elm-street/pull/114/files#diff-77e5a196b2b19636b3c11a33250006be287ea3155cc947a6866aba5583221d24R126

turboMaCk commented 3 years ago

ah sorry I indeed was looking on just the first commit - I had this tab opened for few days :face_with_head_bandage:

typ3c4t commented 3 years ago

this is the first elm street PR, I'm looking into, so maybe this question is silly---but where is the logic for how a Value term in .hs gets converted to a Value term in .elm?

jhrcek commented 3 years ago

There is no need for explicit logic for converting Aeson.Value to elm's Json.Decode.Value.

Aeson.Value is Haskell representation of json AST. Aeson encoding works like this: YourDataType -- toJson --> Aeson.Value -- encode --> ByteString.

The first arrow is taken care of by ToJSON instance for YourDataType. instance ToJSON Value is trivial.

The second arrow is about serializing Value to sequence of Bytes (ByteString) which are then sent over the network. In our case this is taken care of for us by servant's mimeRender which is just calling aeson's encode.

From the point of view of elm/json library, Value is an opaque data type. It's Elm representation of json AST. Elm gives you primitive decoder value which I'm using for any field with this type. Maybe looking at https://hackage.haskell.org/package/elm-street-0.1.0.4/docs/src/Elm.Ast.html will make things clearer. Basically data types are like trees with primitives at the leaves. On elm side primitives are decoded by decoders provided by elm/json, so they don't requires us to generate decoders.

Is that clear enough?

jhrcek commented 3 years ago

@copytypecat btw as a learning project I highly recommend going through/understanding all the source code of this project. It's relatively small (just ~1000 lines of code) with lots of interesting Haskell used. And feel free to spam me with questions about it.