/
GDecodeJson.purs
173 lines (160 loc) · 4.8 KB
/
GDecodeJson.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
module Data.Argonaut.Decode.Struct.Tolerant.GDecodeJson
( class GDecodeJson
, gDecodeJson
) where
import Prelude (class Category, class Semigroupoid, bind, identity, ($), (<<<))
import Control.Plus (class Plus, empty)
import Data.Argonaut.Core (Json)
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson) as D
import Data.Argonaut.Decode.Struct.Utils (getMissingFieldErrorMessage)
import Data.Either (Either)
import Data.Maybe (Maybe(Just, Nothing))
import Data.Operator.Bottom (bottom2)
import Data.Operator.Top (class Top1_, top1_)
import Data.Struct (class RInsert, rinsert)
import Data.Symbol (class IsSymbol, SProxy(SProxy), reflectSymbol)
import Foreign.Object (Object, lookup)
import Type.Data.RowList (RLProxy(RLProxy))
import Type.Proxying (class RLProxying)
import Type.Row (class Cons, class Lacks, Cons, Nil, kind RowList)
class GDecodeJson
(p :: Type -> Type -> Type)
(f :: Type -> Type)
(g :: # Type -> Type)
(l0 :: RowList)
(r0 :: # Type)
(l1 :: RowList)
(r1 :: # Type)
| l0 -> r0
, l1 -> r1
where
gDecodeJson
:: forall h
. RLProxying h l0
=> RLProxying h l1
=> h l0
-> h l1
-> Object Json
-> f (p (g r0) (g r1))
instance gDecodeJson_NilNilNil
:: ( Category p
, Top1_ f
)
=> GDecodeJson p f g Nil () Nil ()
where
gDecodeJson _ _ _ = top1_ identity
instance gDecodeJson_ConsNilCons_Plus
:: ( Cons s (f v) r' r
, D.DecodeJson (f v)
, GDecodeJson p (Either String) g Nil () l' r'
, IsSymbol s
, Lacks s r'
, Plus f
, RInsert p g SProxy s l' r' l r
, Semigroupoid p
)
=> GDecodeJson p (Either String) g Nil () (Cons s (f v) l') r
where
gDecodeJson _ _ object = do
doRest <- gDecodeJson nil l' object
case lookup fieldName object of
Just jsonVal -> do
(val :: f v) <- D.decodeJson jsonVal
top1_ $ rinsert l' l s val <<< doRest
Nothing ->
top1_ $ rinsert l' l s (empty :: f v) <<< doRest
where
fieldName :: String
fieldName = reflectSymbol s
l' = RLProxy :: RLProxy l'
l = RLProxy :: RLProxy l
nil = RLProxy :: RLProxy Nil
s = SProxy :: SProxy s
else instance gDecodeJson_ConsNilCons_nonPlus
:: ( Cons s v r' r
, D.DecodeJson v
, GDecodeJson p (Either String) g Nil () l' r'
, IsSymbol s
, Lacks s r'
, RInsert p g SProxy s l' r' l r
, Semigroupoid p
)
=> GDecodeJson p (Either String) g Nil () (Cons s v l') r
where
gDecodeJson _ _ object = do
case lookup fieldName object of
Just jsonVal -> do
val <- D.decodeJson jsonVal
doRest <- gDecodeJson nil l' object
top1_ $ rinsert l' l s val <<< doRest
Nothing ->
bottom2 $ getMissingFieldErrorMessage fieldName
where
fieldName :: String
fieldName = reflectSymbol s
l' = RLProxy :: RLProxy l'
l = RLProxy :: RLProxy l
nil = RLProxy :: RLProxy Nil
s = SProxy :: SProxy s
instance gDecodeJson_NilConsCons
:: ( Category p
, Top1_ f
)
=> GDecodeJson p f g (Cons s v l') r (Cons s v l') r
where
gDecodeJson _ _ _ = top1_ identity
else instance gDecodeJson_ConsConsCons_Plus
:: ( Cons s (f v) r1' r1
, D.DecodeJson (f v)
, GDecodeJson p (Either String) g (Cons s1 v1 l0') r0 l1' r1'
, IsSymbol s
, Lacks s r0
, Lacks s r1'
, Plus f
, RInsert p g SProxy s l1' r1' l1 r1
, Semigroupoid p
)
=> GDecodeJson p (Either String) g (Cons s1 v1 l0') r0 (Cons s (f v) l1') r1
where
gDecodeJson _ _ object = do
doRest <- gDecodeJson l0 l1' object
case lookup fieldName object of
Just jsonVal -> do
(val :: f v) <- D.decodeJson jsonVal
top1_ $ rinsert l1' l1 s val <<< doRest
Nothing ->
top1_ $ rinsert l1' l1 s (empty :: f v) <<< doRest
where
fieldName :: String
fieldName = reflectSymbol s
l0 = RLProxy :: RLProxy (Cons s1 v1 l0')
l1' = RLProxy :: RLProxy l1'
l1 = RLProxy :: RLProxy l1
s = SProxy :: SProxy s
else instance gDecodeJson_ConsConsCons_nonPlus
:: ( Cons s v r1' r1
, D.DecodeJson v
, GDecodeJson p (Either String) g (Cons s1 v1 l0') r0 l1' r1'
, IsSymbol s
, Lacks s r0
, Lacks s r1'
, RInsert p g SProxy s l1' r1' l1 r1
, Semigroupoid p
)
=> GDecodeJson p (Either String) g (Cons s1 v1 l0') r0 (Cons s v l1') r1
where
gDecodeJson _ _ object = do
case lookup fieldName object of
Just jsonVal -> do
val <- D.decodeJson jsonVal
doRest <- gDecodeJson l0 l1' object
top1_ $ rinsert l1' l1 s val <<< doRest
Nothing ->
bottom2 $ getMissingFieldErrorMessage fieldName
where
fieldName :: String
fieldName = reflectSymbol s
l0 = RLProxy :: RLProxy (Cons s1 v1 l0')
l1' = RLProxy :: RLProxy l1'
l1 = RLProxy :: RLProxy l1
s = SProxy :: SProxy s