Skip to content

Commit

Permalink
Adding encoders and decoders for json (finos#1198)
Browse files Browse the repository at this point in the history
* Adding encoders and decoders for json

* Adding tests for encoder decoders

* Adding tests for decoders and removing date

* Fix vulnerabilities

---------

Co-authored-by: Damian Reeves <[email protected]>
  • Loading branch information
michelchan and DamianReeves authored Nov 6, 2024
1 parent f71850a commit 8e77b36
Show file tree
Hide file tree
Showing 9 changed files with 2,015 additions and 17 deletions.
5 changes: 4 additions & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"Morphir.SDK.ResultList",
"Morphir.SDK.LocalTime",
"Morphir.SDK.UUID",
"Morphir.SDK.Json.Encode",
"Morphir.SDK.Json.Decode",
"Morphir.IR.Name",
"Morphir.IR.NodeId",
"Morphir.IR.Decoration",
Expand Down Expand Up @@ -86,7 +88,8 @@
"pzp1997/assoc-list": "1.0.0 <= v < 2.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.4 <= v < 2.0.0",
"rundis/elm-bootstrap": "5.2.0 <= v < 6.0.0",
"stil4m/elm-syntax": "7.2.1 <= v < 8.0.0"
"stil4m/elm-syntax": "7.2.1 <= v < 8.0.0",
"waratuman/json-extra": "1.0.0 <= v < 1.0.3"
},
"test-dependencies": {}
}
29 changes: 15 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 143 additions & 0 deletions src/Morphir/IR/SDK/Json/Decode.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
module Morphir.IR.SDK.Json.Decode exposing (..)

import Dict
import Morphir.IR.Documented exposing (Documented)
import Morphir.IR.Literal exposing (Literal(..))
import Morphir.IR.Module as Module exposing (ModuleName)
import Morphir.IR.Name as Name
import Morphir.IR.Path as Path
import Morphir.IR.SDK.Basics exposing (floatType, intType)
import Morphir.IR.SDK.Common exposing (tFun, tVar, toFQName, vSpec)
import Morphir.IR.SDK.Dict exposing (dictType)
import Morphir.IR.SDK.Json.Encode exposing (valueType)
import Morphir.IR.SDK.List exposing (listType)
import Morphir.IR.SDK.Maybe exposing (maybeType)
import Morphir.IR.SDK.Result exposing (resultType)
import Morphir.IR.SDK.String exposing (stringType)
import Morphir.IR.Type exposing (Specification(..), Type(..))


moduleName : ModuleName
moduleName =
Path.fromString "Decode"


moduleSpec : Module.Specification ()
moduleSpec =
{ types =
Dict.fromList
[ ( Name.fromString "Decode", OpaqueTypeSpecification [] |> Documented "Type that represents a JSON Decoder" )
, ( Name.fromString "Error", OpaqueTypeSpecification [] |> Documented "Type that represents a JSON Decoding Error" )
]
, values =
Dict.fromList
[ vSpec "string" [ ( "d", decoderType () (tVar "a") ) ] (stringType ())
, vSpec "bool" [ ( "d", decoderType () (tVar "a") ) ] (intType ())
, vSpec "int" [ ( "d", decoderType () (tVar "a") ) ] (intType ())
, vSpec "float" [ ( "d", decoderType () (tVar "a") ) ] (floatType ())
, vSpec "nullable" [ ( "d", decoderType () (tVar "a") ) ] (maybeType () (tVar "a"))
, vSpec "list"
[ ( "d", decoderType () (tVar "a") ) ]
(decoderType () (listType () (tVar "a")))
, vSpec "dict"
[ ( "d", decoderType () (tVar "a") ) ]
(decoderType () (dictType () (stringType ()) (tVar "a")))
, vSpec "keyValuePairs"
[ ( "d", decoderType () (tVar "a") ) ]
(decoderType () (listType () (Tuple () [ tVar "a", tVar "b" ])))
, vSpec "oneOrMore"
[ ( "f", tFun [ tVar "a", listType () (tVar "a") ] (tVar "value") )
, ( "d", decoderType () (tVar "a") )
]
(decoderType () (tVar "value"))
, vSpec "field" [ ( "s", stringType () ), ( "d", decoderType () (tVar "a") ) ] (decoderType () (tVar "a"))
, vSpec "at" [ ( "l", listType () (stringType ()) ), ( "d", decoderType () (tVar "a") ) ] (decoderType () (tVar "a"))
, vSpec "index" [ ( "i", intType () ), ( "d", decoderType () (tVar "a") ) ] (decoderType () (tVar "a"))
, vSpec "maybe" [ ( "d", decoderType () (tVar "a") ) ] (decoderType () (maybeType () (tVar "a")))
, vSpec "oneOf" [ ( "l", listType () (decoderType () (tVar "a")) ) ] (decoderType () (tVar "a"))
, vSpec "map"
[ ( "f", tFun [ tVar "a" ] (tVar "value") )
, ( "d", decoderType () (tVar "a") )
]
(decoderType () (tVar "value"))
, vSpec "map2"
[ ( "f", tFun [ tVar "a", tVar "b" ] (tVar "value") )
, ( "d1", decoderType () (tVar "a") )
, ( "d2", decoderType () (tVar "b") )
]
(decoderType () (tVar "value"))
, vSpec "map3"
[ ( "f", tFun [ tVar "a", tVar "b", tVar "c" ] (tVar "value") )
, ( "d1", decoderType () (tVar "a") )
, ( "d2", decoderType () (tVar "b") )
, ( "d3", decoderType () (tVar "c") )
]
(decoderType () (tVar "value"))
, vSpec "map4"
[ ( "f", tFun [ tVar "a", tVar "b", tVar "c", tVar "d" ] (tVar "value") )
, ( "d1", decoderType () (tVar "a") )
, ( "d2", decoderType () (tVar "b") )
, ( "d3", decoderType () (tVar "c") )
, ( "d4", decoderType () (tVar "d") )
]
(decoderType () (tVar "value"))
, vSpec "map5"
[ ( "f", tFun [ tVar "a", tVar "b", tVar "c", tVar "d", tVar "e" ] (tVar "value") )
, ( "d1", decoderType () (tVar "a") )
, ( "d2", decoderType () (tVar "b") )
, ( "d3", decoderType () (tVar "c") )
, ( "d4", decoderType () (tVar "d") )
, ( "d5", decoderType () (tVar "e") )
]
(decoderType () (tVar "value"))
, vSpec "map6"
[ ( "f", tFun [ tVar "a", tVar "b", tVar "c", tVar "d", tVar "e", tVar "f" ] (tVar "value") )
, ( "d1", decoderType () (tVar "a") )
, ( "d2", decoderType () (tVar "b") )
, ( "d3", decoderType () (tVar "c") )
, ( "d4", decoderType () (tVar "d") )
, ( "d5", decoderType () (tVar "e") )
, ( "d6", decoderType () (tVar "f") )
]
(decoderType () (tVar "value"))
, vSpec "map7"
[ ( "f", tFun [ tVar "a", tVar "b", tVar "c", tVar "d", tVar "e", tVar "f", tVar "g" ] (tVar "value") )
, ( "d1", decoderType () (tVar "a") )
, ( "d2", decoderType () (tVar "b") )
, ( "d3", decoderType () (tVar "c") )
, ( "d4", decoderType () (tVar "d") )
, ( "d5", decoderType () (tVar "e") )
, ( "d6", decoderType () (tVar "f") )
, ( "d7", decoderType () (tVar "g") )
]
(decoderType () (tVar "value"))
, vSpec "map8"
[ ( "f", tFun [ tVar "a", tVar "b", tVar "c", tVar "d", tVar "e", tVar "f", tVar "g", tVar "h" ] (tVar "value") )
, ( "d1", decoderType () (tVar "a") )
, ( "d2", decoderType () (tVar "b") )
, ( "d3", decoderType () (tVar "c") )
, ( "d4", decoderType () (tVar "d") )
, ( "d5", decoderType () (tVar "e") )
, ( "d6", decoderType () (tVar "f") )
, ( "d7", decoderType () (tVar "g") )
, ( "d8", decoderType () (tVar "h") )
]
(decoderType () (tVar "value"))
, vSpec "decodeString" [ ( "d", decoderType () (tVar "a") ), ( "s", stringType () ) ] (resultType () (errorType ()) (tVar "a"))
, vSpec "decodeValue" [ ( "d", decoderType () (tVar "a") ), ( "v", valueType () ) ] (resultType () (errorType ()) (tVar "a"))
, vSpec "errorToString" [ ( "e", errorType () ) ] (stringType ())
, vSpec "succeed" [ ( "a", tVar "a" ) ] (decoderType () (tVar "a"))
, vSpec "fail" [ ( "s", stringType () ) ] (decoderType () (tVar "a"))
]
, doc = Just "The Decode type and associated functions"
}


decoderType : a -> Type a -> Type a
decoderType attributes itemType =
Reference attributes (toFQName moduleName "Decoder") [ itemType ]


errorType : a -> Type a
errorType attributes =
Reference attributes (toFQName moduleName "Error") []
69 changes: 69 additions & 0 deletions src/Morphir/IR/SDK/Json/Encode.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Morphir.IR.SDK.Json.Encode exposing (..)

import Dict
import Morphir.IR.Documented exposing (Documented)
import Morphir.IR.Literal exposing (Literal(..))
import Morphir.IR.Module as Module exposing (ModuleName)
import Morphir.IR.Name as Name
import Morphir.IR.Path as Path
import Morphir.IR.SDK.Basics exposing (boolType, floatType, intType)
import Morphir.IR.SDK.Common exposing (tFun, tVar, toFQName, vSpec)
import Morphir.IR.SDK.Dict exposing (dictType)
import Morphir.IR.SDK.List exposing (listType)
import Morphir.IR.SDK.LocalTime exposing (localTimeType)
import Morphir.IR.SDK.Maybe exposing (maybeType)
import Morphir.IR.SDK.Set exposing (setType)
import Morphir.IR.SDK.String exposing (stringType)
import Morphir.IR.Type exposing (Specification(..), Type(..))


moduleName : ModuleName
moduleName =
Path.fromString "Encode"


moduleSpec : Module.Specification ()
moduleSpec =
{ types =
Dict.fromList
[ ( Name.fromString "Encode", OpaqueTypeSpecification [] |> Documented "Type that represents a JSON Encoder" )
]
, values =
Dict.fromList
[ vSpec "encode" [ ( "i", intType () ), ( "v", valueType () ) ] (stringType ())
, vSpec "string" [ ( "s", stringType () ) ] (valueType ())
, vSpec "int" [ ( "i", intType () ) ] (valueType ())
, vSpec "float" [ ( "f", floatType () ) ] (valueType ())
, vSpec "bool" [ ( "f", boolType () ) ] (valueType ())
, vSpec "null" [] (valueType ())
, vSpec "list"
[ ( "f", tFun [ tVar "a" ] (tVar "Value") )
, ( "list", listType () (tVar "a") )
]
(valueType ())
, vSpec "set"
[ ( "f", tFun [ tVar "a" ] (tVar "Value") )
, ( "set", setType () (tVar "a") )
]
(valueType ())
, vSpec "object" [ ( "list", listType () (Tuple () [ tVar "a", tVar "b" ]) ) ] (valueType ())
, vSpec "dict"
[ ( "f", tFun [ tVar "k" ] (stringType ()) )
, ( "f", tFun [ tVar "v" ] (valueType ()) )
, ( "dict", dictType () (tVar "k") (tVar "v") )
]
(dictType () (tVar "comparable") (tVar "v"))
, vSpec "localTime" [ ( "t", localTimeType () ) ] (valueType ())
, vSpec "maybe"
[ ( "f", tFun [ tVar "a" ] (tVar "Value") )
, ( "maybe", maybeType () (tVar "a") )
]
(valueType ())
]
, doc = Just "The Encode type and associated functions"
}


valueType : a -> Type a
valueType attributes =
Reference attributes (toFQName moduleName "Encode") []
Loading

0 comments on commit 8e77b36

Please sign in to comment.