@@ -3,14 +3,17 @@ module Data.Argonaut.Decode.Generic.Rep (
3
3
class DecodeRepArgs ,
4
4
class DecodeLiteral ,
5
5
decodeRep ,
6
+ decodeRepWith ,
6
7
decodeRepArgs ,
7
8
genericDecodeJson ,
9
+ genericDecodeJsonWith ,
8
10
decodeLiteralSum ,
9
11
decodeLiteralSumWithTransform ,
10
12
decodeLiteral
11
13
) where
12
14
13
15
import Prelude
16
+ import Data.Argonaut.Types.Generic.Rep (Encoding , defaultEncoding )
14
17
15
18
import Control.Alt ((<|>))
16
19
import Data.Argonaut.Core (Json , toArray , toObject , toString )
@@ -26,28 +29,31 @@ import Partial.Unsafe (unsafeCrashWith)
26
29
import Prim.TypeError (class Fail , Text )
27
30
28
31
class DecodeRep r where
29
- decodeRep :: Json -> Either String r
32
+ decodeRepWith :: Encoding -> Json -> Either String r
33
+
34
+ decodeRep :: forall r . DecodeRep r => Json -> Either String r
35
+ decodeRep = decodeRepWith defaultEncoding
30
36
31
37
instance decodeRepNoConstructors :: DecodeRep Rep.NoConstructors where
32
- decodeRep _ = Left " Cannot decode empty data type"
38
+ decodeRepWith e _ = Left " Cannot decode empty data type"
33
39
34
40
instance decodeRepSum :: (DecodeRep a , DecodeRep b ) => DecodeRep (Rep.Sum a b ) where
35
- decodeRep j = Rep.Inl <$> decodeRep j <|> Rep.Inr <$> decodeRep j
41
+ decodeRepWith e j = Rep.Inl <$> decodeRepWith e j <|> Rep.Inr <$> decodeRepWith e j
36
42
37
43
instance decodeRepConstructor :: (IsSymbol name , DecodeRepArgs a ) => DecodeRep (Rep.Constructor name a ) where
38
- decodeRep j = do
44
+ decodeRepWith e j = do
39
45
let name = reflectSymbol (SProxy :: SProxy name )
40
46
let decodingErr msg = " When decoding a " <> name <> " : " <> msg
41
47
jObj <- mFail (decodingErr " expected an object" ) (toObject j)
42
- jTag <- mFail (decodingErr " 'tag ' property is missing" ) (FO .lookup " tag " jObj)
43
- tag <- mFail (decodingErr " 'tag ' property is not a string" ) (toString jTag)
48
+ jTag <- mFail (decodingErr $ " ' " <> e.tagKey <> " ' property is missing" ) (FO .lookup e.tagKey jObj)
49
+ tag <- mFail (decodingErr $ " ' " <> e.tagKey <> " ' property is not a string" ) (toString jTag)
44
50
when (tag /= name) $
45
- Left $ decodingErr " 'tag ' property has an incorrect value"
46
- jValues <- mFail (decodingErr " 'values ' property is missing" ) (FO .lookup " values " jObj)
47
- values <- mFail (decodingErr " 'values ' property is not an array" ) (toArray jValues)
51
+ Left $ decodingErr $ " ' " <> e.tagKey <> " ' property has an incorrect value"
52
+ jValues <- mFail (decodingErr $ " ' " <> e.valuesKey <> " ' property is missing" ) (FO .lookup e.valuesKey jObj)
53
+ values <- mFail (decodingErr $ " ' " <> e.valuesKey <> " ' property is not an array" ) (toArray jValues)
48
54
{init, rest} <- lmap decodingErr $ decodeRepArgs values
49
55
when (rest /= [] ) $
50
- Left $ decodingErr " 'values ' property had too many values"
56
+ Left $ decodingErr $ " ' " <> e.valuesKey <> " ' property had too many values"
51
57
pure $ Rep.Constructor init
52
58
53
59
class DecodeRepArgs r where
@@ -69,7 +75,11 @@ instance decodeRepArgsArgument :: (DecodeJson a) => DecodeRepArgs (Rep.Argument
69
75
70
76
-- | Decode `Json` representation of a value which has a `Generic` type.
71
77
genericDecodeJson :: forall a r . Rep.Generic a r => DecodeRep r => Json -> Either String a
72
- genericDecodeJson = map Rep .to <<< decodeRep
78
+ genericDecodeJson = genericDecodeJsonWith defaultEncoding
79
+
80
+ -- | Decode `Json` representation of a value which has a `Generic` type.
81
+ genericDecodeJsonWith :: forall a r . Rep.Generic a r => DecodeRep r => Encoding -> Json -> Either String a
82
+ genericDecodeJsonWith e = map Rep .to <<< decodeRepWith e
73
83
74
84
mFail :: forall a . String -> Maybe a -> Either String a
75
85
mFail msg = maybe (Left msg) Right
0 commit comments