forked from finos/morphir-elm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding encoders and decoders for json (finos#1198)
* 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
1 parent
f71850a
commit 8e77b36
Showing
9 changed files
with
2,015 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") [] |
Oops, something went wrong.