-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJson.hs
More file actions
343 lines (288 loc) · 11.1 KB
/
Json.hs
File metadata and controls
343 lines (288 loc) · 11.1 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
-- | lbf-prelude.Prelude.Json class implementations
module LambdaBuffers.Codegen.LamVal.Json (deriveToJsonImpl, deriveFromJsonImpl) where
import Control.Lens (view)
import Data.List (sortOn)
import Data.Map.Ordered qualified as OMap
import Data.Text (Text)
import Data.Text qualified as Text
import LambdaBuffers.Codegen.LamVal (QProduct, QRecord, QSum, ValueE (CaseE, CaseListE, CtorE, ErrorE, FieldE, LamE, LetE, ListE, ProductE, RecordE, RefE, TextE, TupleE), (@))
import LambdaBuffers.Codegen.LamVal.Derive (deriveImpl)
import LambdaBuffers.Compiler.LamTy qualified as LT
import LambdaBuffers.ProtoCompat qualified as PC
import Proto.Codegen qualified as P
-- | Domain functions
-- | `toJson :: a -> Json`
toJsonRef :: LT.Ty -> ValueE
toJsonRef ty = RefE ([ty], "toJson")
-- | `fromJson :: Json -> Parser a`
fromJsonRef :: LT.Ty -> ValueE
fromJsonRef ty = RefE ([ty], "fromJson")
-- | `bindParse :: Parser a -> (a -> Parser b) -> Parser b`
bindParseRef :: LT.Ty -> LT.Ty -> ValueE
bindParseRef tyIn tyOut = RefE ([tyIn, tyOut], "bindParse")
-- | `failParse :: Text -> Parser a`
failParseRef :: LT.Ty -> ValueE
failParseRef ty = RefE ([ty], "failParse")
-- | `succeedParse :: a -> Parser a`
succeedParseRef :: LT.Ty -> ValueE
succeedParseRef ty = RefE ([ty], "succeedParse")
-- | `jsonConstructor :: Text -> [Json] -> Json`
jsonConstructorRef :: ValueE
jsonConstructorRef = RefE ([], "jsonConstructor")
-- | `caseJsonConstructor :: Text -> [(Text, [Json] -> Parser a)] -> Json -> Parser a`
caseJsonConstructor :: LT.Ty -> ValueE
caseJsonConstructor ty = RefE ([ty], "caseJsonConstructor")
-- | `jsonArray :: [Json] -> Json`
jsonArrayRef :: ValueE
jsonArrayRef = RefE ([], "jsonArray")
-- | `caseJsonArray :: Text -> ([Json] -> Parser a) -> Json -> Parser a
caseJsonArrayRef :: LT.Ty -> ValueE
caseJsonArrayRef ty = RefE ([ty], "caseJsonArray")
-- | `jsonObject :: [(Text, Json)] -> Json`
jsonObjRef :: ValueE
jsonObjRef = RefE ([], "jsonObject")
-- | `caseJsonObject` :: (JsonObj -> Parser a) -> Json -> Parser a
caseJsonObjectRef :: LT.Ty -> ValueE
caseJsonObjectRef ty = RefE ([ty], "caseJsonObject")
-- | `jsonField` :: Text -> JsonObj -> (Json -> Parser a) -> Parser a
jsonFieldRef :: LT.Ty -> ValueE
jsonFieldRef ty = RefE ([ty], "jsonField")
{- | `toJsonSum qsum` makes a `LamVal` function specification for encoding sum type values into their JSON representation `toJson :: <qsum> -> Json`.
```
module Example
sum Foo a b = Bar | Baz a | Qax a b
```
Generated code in some functional language:
```
fooToJson :: (Json a, Json b) => Foo a b -> Json
fooToJson =
\foo -> case foo of
Bar -> jsonConstructor "Bar" []
Baz x1 -> jsonConstructor "Baz" [toJson @a x1]
Qax x1 x2 -> jsonConstructor "Baz" [toJson @a x1, toJson @b x2]
```
TODO(bladyjoker): For completeness add `ty` as an argument?
-}
toJsonSum :: QSum -> ValueE
toJsonSum qsum@(_qtyn, _sumTy) =
LamE
( \sumVal ->
CaseE
qsum
sumVal
( \((ctorN, ctorTy), ctorVal) ->
jsonConstructorRef
@ ctorNameVal ctorN
@ ListE [toJsonRef fieldTy @ fieldVal | (fieldVal, fieldTy) <- zip ctorVal ctorTy]
)
)
{- | `fromJsonSum ty qsum` makes a `LamVal` function for decoding sum type values from their JSON representation `fromJson :: Json -> Parser <ty>`.
```
module Example
sum Foo a b = Bar | Baz a | Qax a b
```
Generated code in some functional language:
```
fooFromJson :: (Json a, Json b) => Json -> Parser (Foo a b)
fooFromJson =
caseJsonConstructor "Foo" [
("Bar",
\jsons -> case jsons of
[] -> succeedParse @(Foo a b) Bar)
_ -> fail "Expected a JSON Array with 0 elements"
),
("Baz",
\jsons -> case jsons of
[x1] -> fromJson @a x1 >>= \y1 -> succeedParse @(Foo a b) (Baz y1)
_ -> fail "Expected a JSON Array with 1 elements"
),
("Qax",
\jsons -> case jsons of
[x1, x2] -> fromJson @a x1 >>= \y1 -> fromJson @b x2 >>= \y2 -> succeedParse @(Foo a b) (Qax y1 y2)
_ -> fail "Expected a JSON Array with 2 elements"
)
]
```
-}
fromJsonSum :: LT.Ty -> QSum -> ValueE
fromJsonSum ty (qtyN, sumTy) =
caseJsonConstructor ty
@ TextE (showQTyName qtyN)
@ ListE
( [ TupleE (ctorNameVal ctorN) (LamE $ \jsons -> fromJsonCtor ctorN ctorTy jsons)
| (ctorN, LT.TyProduct ctorTy _) <- OMap.assocs sumTy
]
)
where
fromJsonCtor ctorN ctorTy jsons =
CaseListE
jsons
[
( length ctorTy
, \fields' -> fromJsonTys (zip ctorTy fields') ty (\xs -> succeedParseRef ty @ CtorE (qtyN, (ctorN, ctorTy)) xs)
)
]
(\_ -> failParseRef ty @ TextE ("Expected a JSON Array of " <> (Text.pack . show . length $ ctorTy) <> " elements"))
showQTyName :: PC.QTyName -> Text
showQTyName (_mn, _tyn) = "TODO(bladyjoker): Print qualified type name"
{- | `toJsonProduct qprod` makes a `LamVal` function for encoding product type values into their JSON representation `toJson :: <qprod> -> Json`.
```
module Example
product Foo a b = a b
product Bar a = a
```
Generated code in some functional language:
```
fooToJson :: (Json a, Json b) => Foo a b -> Json
fooToJson =
\foo -> let foo (\x1 x2 -> jsonArray [toJson @a x1, toJson @b x2])
barToJson :: Json a => Bar a -> Json
barToJson =
\bar -> let bar (\x1 -> toJson @a x1)
```
-}
toJsonProduct :: QProduct -> ValueE
toJsonProduct qprod@(_, prodTy) =
LamE
( \prodVal ->
LetE
qprod
prodVal
( \fieldVals ->
case (fieldVals, prodTy) of
([], []) -> ErrorE "Got an empty Product type to print in `toJsonProduct`"
([fieldVal], [fieldTy]) -> toJsonRef fieldTy @ fieldVal
(fieldVals', fieldTys)
| length fieldVals' == length fieldTys ->
jsonArrayRef
@ ListE
[ toJsonRef fieldTy @ fieldVal
| (fieldVal, fieldTy) <- zip fieldVals' fieldTys
]
_ -> ErrorE "Got mismatching number of variables in `toJsonProduct`"
)
)
{- | `fromJsonProduct qprod` makes a `LamVal` function for decoding product type values from their JSON representation `fromJson :: Json -> Parser <qprod>`.
```
module Example
product Foo a b = a b
product Bar a = a
```
Generated code in some functional language:
```
fooFromJson :: (Json a, Json b) => Json -> Parser (Foo a b)
fooFromJson =
caseJsonArray "Foo"
(\jsons -> case jsons of
[x1, x2] -> fromJson @a x1 >>= \y1 -> fromJson @b x2 >>= \y2 -> succeedParse @(Foo a b) (Foo y1 y2))
_ -> (failParse @(Foo a b) "Expected a JSON Array of 2 elements")
)
barToJson :: Json a => Bar a -> Json
barToJson =
\json -> fromJson @a json >>= \y1 -> succeedParse @(Bar a) (Bar y1)
```
-}
fromJsonProduct :: LT.Ty -> QProduct -> ValueE
fromJsonProduct ty qprod@(_, [fieldTy]) = LamE $ \json ->
bindParseRef fieldTy ty
@ (fromJsonRef fieldTy @ json)
@ LamE (\fieldVal -> succeedParseRef ty @ ProductE qprod [fieldVal])
fromJsonProduct ty qprod@(qtyN, prodTy) =
caseJsonArrayRef ty
@ TextE (showQTyName qtyN)
@ LamE
( \jsons ->
CaseListE
jsons
[
( length prodTy
, \jsons' -> fromJsonTys (zip prodTy jsons') ty (\xs -> succeedParseRef ty @ ProductE qprod xs)
)
]
(\_ -> failParseRef ty @ TextE ("Expected a JSON Array of " <> (Text.pack . show . length $ prodTy) <> " elements"))
)
{- | `toJsonRecord qrec` makes a `LamVal` function for encoding record type values into their JSON representation `toJson :: <qrec> -> Json`.
```
module Example
record Foo a b = { fooA : a, fooB : b }
record Bar a = { barA: a }
```
Generated code in some functional language:
```
fooToJson :: (Json a, Json b) => Foo a b -> Json
fooToJson =
\foo -> jsonObject [ ("fooA", toJson @a foo.fooA), ("fooB", toJson @b foo.fooB) ]
barToJson :: Json a => Bar a -> Json
barToJson =
\bar -> jsonObject [ ("barA", toJson @a bar.barA) ]
```
-}
toJsonRecord :: QRecord -> ValueE
toJsonRecord (qtyN, recTy) =
LamE
( \recVal ->
jsonObjRef
@ ListE
[ TupleE (fieldNameVal fieldName) (toJsonRef fieldTy @ FieldE (qtyN, fieldName) recVal)
| (fieldName, fieldTy) <- sortOn fst $ OMap.assocs recTy
]
)
{- | `fromJsonRecord ty qrec` makes a `LamVal` function for decoding record type values from their JSON representation `fromJson :: Json -> Parser <ty>`.
```
```
module Example
record Foo a b = { fooA : a, fooB : b }
record Bar a = { barA: a }
```
Generated code in some functional language:
```
fooFromJson :: (Json a, Json b) => Json -> Parser (Foo a b)
fooFromJson =
caseJsonObject \jsonObj ->
jsonField "fooA" jsonObj
\json'fooA -> fromJson @a json'fooA >>=
\fooA -> jsonObjField "fooB" jsonObj
\json'fooB -> fromJson @a json'fooB >>=
\fooB -> succeedParse @(Foo a b) (Foo {fooA, fooB})
barToJson :: Json a => Bar a -> Json
barToJson =
caseJsonObj \jsonObj ->
jsonField "barA" jsonObj
\json'barA -> fromJson @a json'barA >>=
\barA -> succeedParseRef @(Bar a) (Bar {barA})
```
NOTE(bladyjoker): Infinitely complicated --.-- I wanna puke
-}
fromJsonRecord :: LT.Ty -> QRecord -> ValueE
fromJsonRecord ty qrec@(_, recTy) =
caseJsonObjectRef ty @ LamE (\jsonObj -> parseRecordFromJson jsonObj (OMap.assocs recTy) [])
where
parseRecordFromJson _jsonObj [] parsedFields = succeedParseRef ty @ RecordE qrec parsedFields
parseRecordFromJson jsonObj (field@(fieldN, fieldTy) : rest) parsedFields =
jsonFieldRef fieldTy
@ fieldNameVal fieldN
@ jsonObj
@ LamE
( \fieldJson ->
bindParseRef fieldTy ty
@ (fromJsonRef fieldTy @ fieldJson)
@ LamE (\fieldVal -> parseRecordFromJson jsonObj rest ((field, fieldVal) : parsedFields))
)
-- | Hooks
deriveToJsonImpl :: PC.ModuleName -> PC.TyDefs -> PC.Ty -> Either P.InternalError ValueE
deriveToJsonImpl mn tydefs = deriveImpl mn tydefs toJsonSum toJsonProduct toJsonRecord
deriveFromJsonImpl :: PC.ModuleName -> PC.TyDefs -> PC.Ty -> Either P.InternalError ValueE
deriveFromJsonImpl mn tydefs ty = deriveImpl mn tydefs (fromJsonSum (LT.fromTy ty)) (fromJsonProduct (LT.fromTy ty)) (fromJsonRecord $ LT.fromTy ty) ty
-- | Helpers
ctorNameVal :: PC.InfoLess PC.ConstrName -> ValueE
ctorNameVal cn = PC.withInfoLess cn (TextE . view #name)
fieldNameVal :: PC.InfoLess PC.FieldName -> ValueE
fieldNameVal fn = PC.withInfoLess fn (TextE . view #name)
fromJsonTys :: [(LT.Ty, ValueE)] -> LT.Ty -> ([ValueE] -> ValueE) -> ValueE
fromJsonTys pdsToParse tyOut = go pdsToParse []
where
go [] tot cont = cont (reverse tot)
go ((tyX, pdX) : rest) tot cont =
bindParseRef tyX tyOut
@ (fromJsonRef tyX @ pdX)
@ LamE (\x -> go rest (x : tot) cont)