Skip to content

Commit

Permalink
Utilities for mapping custom type constructors to JSON (#28)
Browse files Browse the repository at this point in the history
* Utilities to make the JSON mapping easier. #25

* Fix compile error. #25
  • Loading branch information
AttilaMihaly committed Mar 23, 2020
1 parent 0cdd2d3 commit 431e38b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Morphir/IR/Type.elm
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ matchUnit =
-}
field : Name -> Type -> Field
field fieldName fieldType =
Advanced.field fieldName fieldType
Advanced.Field fieldName fieldType


{-| Matches a field.
Expand Down
47 changes: 47 additions & 0 deletions src/Morphir/JsonExtra.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Morphir.JsonExtra exposing (..)

import Json.Decode as Decode
import Json.Encode as Encode


encodeConstructor : String -> List Encode.Value -> Encode.Value
encodeConstructor typeTag args =
Encode.string typeTag
:: args
|> Encode.list identity


decodeCustomType : (String -> Decode.Decoder a) -> Decode.Decoder a
decodeCustomType f =
Decode.index 0 Decode.string
|> Decode.andThen f


decodeConstructor : (a -> r) -> Decode.Decoder a -> Decode.Decoder r
decodeConstructor f a =
Decode.map f
(Decode.index 1 a)


decodeConstructor2 : (a -> b -> r) -> Decode.Decoder a -> Decode.Decoder b -> Decode.Decoder r
decodeConstructor2 f a b =
Decode.map2 f
(Decode.index 1 a)
(Decode.index 2 b)


decodeConstructor3 : (a -> b -> c -> r) -> Decode.Decoder a -> Decode.Decoder b -> Decode.Decoder c -> Decode.Decoder r
decodeConstructor3 f a b c =
Decode.map3 f
(Decode.index 1 a)
(Decode.index 2 b)
(Decode.index 3 c)


decodeConstructor4 : (a -> b -> c -> d -> r) -> Decode.Decoder a -> Decode.Decoder b -> Decode.Decoder c -> Decode.Decoder d -> Decode.Decoder r
decodeConstructor4 f a b c d =
Decode.map4 f
(Decode.index 1 a)
(Decode.index 2 b)
(Decode.index 3 c)
(Decode.index 4 d)

0 comments on commit 431e38b

Please sign in to comment.