@greenlabs/ppx-spice 0.1.13 → 0.1.15-rc0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/ppx-linux.exe +0 -0
- package/ppx-osx.exe +0 -0
- package/ppx-windows.exe +0 -0
- package/src/rescript/Spice.js +446 -0
- package/src/rescript/Spice.res +22 -19
- package/src/rescript/Spice_Codecs.js +55 -0
- package/src/rescript/Spice_Codecs.res +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
# 0.1.14
|
|
4
|
+
|
|
5
|
+
- Support both `ns.optional` and `res.optional` for backward compatability
|
|
6
|
+
|
|
7
|
+
# 0.1.13
|
|
8
|
+
|
|
9
|
+
- Rename the attribute used for optional records from `ns.optional` to `res.optional`.
|
|
10
|
+
|
|
3
11
|
# 0.1.12
|
|
4
12
|
|
|
5
13
|
- Fix build error where `@spice.encode`, `@spice.decode` are used
|
package/package.json
CHANGED
package/ppx-linux.exe
CHANGED
|
Binary file
|
package/ppx-osx.exe
CHANGED
|
Binary file
|
package/ppx-windows.exe
CHANGED
|
Binary file
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var Js_dict = require("rescript/lib/js/js_dict.js");
|
|
5
|
+
var Js_json = require("rescript/lib/js/js_json.js");
|
|
6
|
+
var Js_math = require("rescript/lib/js/js_math.js");
|
|
7
|
+
var Caml_obj = require("rescript/lib/js/caml_obj.js");
|
|
8
|
+
var Js_array = require("rescript/lib/js/js_array.js");
|
|
9
|
+
var Belt_List = require("rescript/lib/js/belt_List.js");
|
|
10
|
+
var Belt_Array = require("rescript/lib/js/belt_Array.js");
|
|
11
|
+
var Caml_int64 = require("rescript/lib/js/caml_int64.js");
|
|
12
|
+
var Belt_Result = require("rescript/lib/js/belt_Result.js");
|
|
13
|
+
var Caml_option = require("rescript/lib/js/caml_option.js");
|
|
14
|
+
var Spice_Codecs = require("./Spice_Codecs.js");
|
|
15
|
+
|
|
16
|
+
function error(path, message, value) {
|
|
17
|
+
var path$1 = path !== undefined ? path : "";
|
|
18
|
+
return {
|
|
19
|
+
TAG: "Error",
|
|
20
|
+
_0: {
|
|
21
|
+
path: path$1,
|
|
22
|
+
message: message,
|
|
23
|
+
value: value
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function stringToJson(s) {
|
|
29
|
+
return s;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function stringFromJson(j) {
|
|
33
|
+
var s = Js_json.decodeString(j);
|
|
34
|
+
if (s !== undefined) {
|
|
35
|
+
return {
|
|
36
|
+
TAG: "Ok",
|
|
37
|
+
_0: s
|
|
38
|
+
};
|
|
39
|
+
} else {
|
|
40
|
+
return {
|
|
41
|
+
TAG: "Error",
|
|
42
|
+
_0: {
|
|
43
|
+
path: "",
|
|
44
|
+
message: "Not a string",
|
|
45
|
+
value: j
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function intToJson(i) {
|
|
52
|
+
return i;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function intFromJson(j) {
|
|
56
|
+
var f = Js_json.decodeNumber(j);
|
|
57
|
+
if (f !== undefined) {
|
|
58
|
+
if (Js_math.floor(f) === f) {
|
|
59
|
+
return {
|
|
60
|
+
TAG: "Ok",
|
|
61
|
+
_0: Js_math.floor(f)
|
|
62
|
+
};
|
|
63
|
+
} else {
|
|
64
|
+
return {
|
|
65
|
+
TAG: "Error",
|
|
66
|
+
_0: {
|
|
67
|
+
path: "",
|
|
68
|
+
message: "Not an integer",
|
|
69
|
+
value: j
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
return {
|
|
75
|
+
TAG: "Error",
|
|
76
|
+
_0: {
|
|
77
|
+
path: "",
|
|
78
|
+
message: "Not a number",
|
|
79
|
+
value: j
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
var int64ToJson = Caml_int64.float_of_bits;
|
|
86
|
+
|
|
87
|
+
function int64FromJson(j) {
|
|
88
|
+
var n = Js_json.decodeNumber(j);
|
|
89
|
+
if (n !== undefined) {
|
|
90
|
+
return {
|
|
91
|
+
TAG: "Ok",
|
|
92
|
+
_0: Caml_int64.bits_of_float(n)
|
|
93
|
+
};
|
|
94
|
+
} else {
|
|
95
|
+
return error(undefined, "Not a number", j);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
var int64ToJsonUnsafe = Caml_int64.to_float;
|
|
100
|
+
|
|
101
|
+
function int64FromJsonUnsafe(j) {
|
|
102
|
+
var n = Js_json.decodeNumber(j);
|
|
103
|
+
if (n !== undefined) {
|
|
104
|
+
return {
|
|
105
|
+
TAG: "Ok",
|
|
106
|
+
_0: Caml_int64.of_float(n)
|
|
107
|
+
};
|
|
108
|
+
} else {
|
|
109
|
+
return error(undefined, "Not a number", j);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function floatToJson(v) {
|
|
114
|
+
return v;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function floatFromJson(j) {
|
|
118
|
+
var f = Js_json.decodeNumber(j);
|
|
119
|
+
if (f !== undefined) {
|
|
120
|
+
return {
|
|
121
|
+
TAG: "Ok",
|
|
122
|
+
_0: f
|
|
123
|
+
};
|
|
124
|
+
} else {
|
|
125
|
+
return {
|
|
126
|
+
TAG: "Error",
|
|
127
|
+
_0: {
|
|
128
|
+
path: "",
|
|
129
|
+
message: "Not a number",
|
|
130
|
+
value: j
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function boolToJson(v) {
|
|
137
|
+
return v;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function boolFromJson(j) {
|
|
141
|
+
var b = Js_json.decodeBoolean(j);
|
|
142
|
+
if (b !== undefined) {
|
|
143
|
+
return {
|
|
144
|
+
TAG: "Ok",
|
|
145
|
+
_0: b
|
|
146
|
+
};
|
|
147
|
+
} else {
|
|
148
|
+
return {
|
|
149
|
+
TAG: "Error",
|
|
150
|
+
_0: {
|
|
151
|
+
path: "",
|
|
152
|
+
message: "Not a boolean",
|
|
153
|
+
value: j
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function unitToJson() {
|
|
160
|
+
return 0.0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function unitFromJson(param) {
|
|
164
|
+
return {
|
|
165
|
+
TAG: "Ok",
|
|
166
|
+
_0: undefined
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
var arrayToJson = Js_array.map;
|
|
171
|
+
|
|
172
|
+
function arrayFromJson(decoder, json) {
|
|
173
|
+
var arr = Js_json.decodeArray(json);
|
|
174
|
+
if (arr !== undefined) {
|
|
175
|
+
return Js_array.reducei((function (acc, jsonI, i) {
|
|
176
|
+
var match = decoder(jsonI);
|
|
177
|
+
if (acc.TAG !== "Ok") {
|
|
178
|
+
return acc;
|
|
179
|
+
}
|
|
180
|
+
if (match.TAG === "Ok") {
|
|
181
|
+
return {
|
|
182
|
+
TAG: "Ok",
|
|
183
|
+
_0: Js_array.concat([match._0], acc._0)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
var error = match._0;
|
|
187
|
+
return {
|
|
188
|
+
TAG: "Error",
|
|
189
|
+
_0: {
|
|
190
|
+
path: "[" + (String(i) + ("]" + error.path)),
|
|
191
|
+
message: error.message,
|
|
192
|
+
value: error.value
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}), {
|
|
196
|
+
TAG: "Ok",
|
|
197
|
+
_0: []
|
|
198
|
+
}, arr);
|
|
199
|
+
} else {
|
|
200
|
+
return {
|
|
201
|
+
TAG: "Error",
|
|
202
|
+
_0: {
|
|
203
|
+
path: "",
|
|
204
|
+
message: "Not an array",
|
|
205
|
+
value: json
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function listToJson(encoder, list) {
|
|
212
|
+
return Js_array.map(encoder, Belt_List.toArray(list));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function listFromJson(decoder, json) {
|
|
216
|
+
return Belt_Result.map(arrayFromJson(decoder, json), Belt_List.fromArray);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function filterOptional(arr) {
|
|
220
|
+
return Belt_Array.map(Belt_Array.keep(arr, (function (param) {
|
|
221
|
+
return !(param[1] && Caml_obj.equal(param[2], null));
|
|
222
|
+
})), (function (param) {
|
|
223
|
+
return [
|
|
224
|
+
param[0],
|
|
225
|
+
param[2]
|
|
226
|
+
];
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function optionToJson(encoder, opt) {
|
|
231
|
+
if (opt !== undefined) {
|
|
232
|
+
return encoder(Caml_option.valFromOption(opt));
|
|
233
|
+
} else {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function optionFromJson(decoder, json) {
|
|
239
|
+
var match = Js_json.decodeNull(json);
|
|
240
|
+
if (match !== undefined) {
|
|
241
|
+
return {
|
|
242
|
+
TAG: "Ok",
|
|
243
|
+
_0: undefined
|
|
244
|
+
};
|
|
245
|
+
} else {
|
|
246
|
+
return Belt_Result.map(decoder(json), (function (v) {
|
|
247
|
+
return Caml_option.some(v);
|
|
248
|
+
}));
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function resultToJson(okEncoder, errorEncoder, result) {
|
|
253
|
+
if (result.TAG === "Ok") {
|
|
254
|
+
return [
|
|
255
|
+
"Ok",
|
|
256
|
+
okEncoder(result._0)
|
|
257
|
+
];
|
|
258
|
+
} else {
|
|
259
|
+
return [
|
|
260
|
+
"Error",
|
|
261
|
+
errorEncoder(result._0)
|
|
262
|
+
];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function resultFromJson(okDecoder, errorDecoder, json) {
|
|
267
|
+
var match = Js_json.decodeArray(json);
|
|
268
|
+
if (match === undefined) {
|
|
269
|
+
return error(undefined, "Not an array", json);
|
|
270
|
+
}
|
|
271
|
+
if (match.length !== 2) {
|
|
272
|
+
return error(undefined, "Expected exactly 2 values in array", json);
|
|
273
|
+
}
|
|
274
|
+
var variantConstructorId = match[0];
|
|
275
|
+
var payload = match[1];
|
|
276
|
+
var match$1 = Js_json.decodeString(variantConstructorId);
|
|
277
|
+
if (match$1 === undefined) {
|
|
278
|
+
return error(undefined, "Not a string", variantConstructorId);
|
|
279
|
+
}
|
|
280
|
+
switch (match$1) {
|
|
281
|
+
case "Error" :
|
|
282
|
+
var v = errorDecoder(payload);
|
|
283
|
+
if (v.TAG === "Ok") {
|
|
284
|
+
return {
|
|
285
|
+
TAG: "Ok",
|
|
286
|
+
_0: {
|
|
287
|
+
TAG: "Error",
|
|
288
|
+
_0: v._0
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
} else {
|
|
292
|
+
return {
|
|
293
|
+
TAG: "Error",
|
|
294
|
+
_0: v._0
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
case "Ok" :
|
|
298
|
+
return Belt_Result.map(okDecoder(payload), (function (v) {
|
|
299
|
+
return {
|
|
300
|
+
TAG: "Ok",
|
|
301
|
+
_0: v
|
|
302
|
+
};
|
|
303
|
+
}));
|
|
304
|
+
default:
|
|
305
|
+
return error(undefined, "Expected either \"Ok\" or \"Error\"", variantConstructorId);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function dictToJson(encoder, dict) {
|
|
310
|
+
return Js_dict.map((function (a) {
|
|
311
|
+
return encoder(a);
|
|
312
|
+
}), dict);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function dictFromJson(decoder, json) {
|
|
316
|
+
var dict = Js_json.decodeObject(json);
|
|
317
|
+
if (dict !== undefined) {
|
|
318
|
+
return Belt_Array.reduce(Js_dict.entries(Caml_option.valFromOption(dict)), {
|
|
319
|
+
TAG: "Ok",
|
|
320
|
+
_0: {}
|
|
321
|
+
}, (function (acc, param) {
|
|
322
|
+
var key = param[0];
|
|
323
|
+
var match = decoder(param[1]);
|
|
324
|
+
if (acc.TAG !== "Ok") {
|
|
325
|
+
return acc;
|
|
326
|
+
}
|
|
327
|
+
var prev = acc._0;
|
|
328
|
+
if (match.TAG === "Ok") {
|
|
329
|
+
prev[key] = match._0;
|
|
330
|
+
return {
|
|
331
|
+
TAG: "Ok",
|
|
332
|
+
_0: prev
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
var error = match._0;
|
|
336
|
+
return {
|
|
337
|
+
TAG: "Error",
|
|
338
|
+
_0: {
|
|
339
|
+
path: "." + (key + error.path),
|
|
340
|
+
message: error.message,
|
|
341
|
+
value: error.value
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
}));
|
|
345
|
+
} else {
|
|
346
|
+
return {
|
|
347
|
+
TAG: "Error",
|
|
348
|
+
_0: {
|
|
349
|
+
path: "",
|
|
350
|
+
message: "Not a dict",
|
|
351
|
+
value: json
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
var string = [
|
|
358
|
+
stringToJson,
|
|
359
|
+
stringFromJson
|
|
360
|
+
];
|
|
361
|
+
|
|
362
|
+
var $$int = [
|
|
363
|
+
intToJson,
|
|
364
|
+
intFromJson
|
|
365
|
+
];
|
|
366
|
+
|
|
367
|
+
var int64Unsafe = [
|
|
368
|
+
int64ToJsonUnsafe,
|
|
369
|
+
int64FromJsonUnsafe
|
|
370
|
+
];
|
|
371
|
+
|
|
372
|
+
var $$float = [
|
|
373
|
+
floatToJson,
|
|
374
|
+
floatFromJson
|
|
375
|
+
];
|
|
376
|
+
|
|
377
|
+
var bool = [
|
|
378
|
+
boolToJson,
|
|
379
|
+
boolFromJson
|
|
380
|
+
];
|
|
381
|
+
|
|
382
|
+
var array = [
|
|
383
|
+
arrayToJson,
|
|
384
|
+
arrayFromJson
|
|
385
|
+
];
|
|
386
|
+
|
|
387
|
+
var list = [
|
|
388
|
+
listToJson,
|
|
389
|
+
listFromJson
|
|
390
|
+
];
|
|
391
|
+
|
|
392
|
+
var option = [
|
|
393
|
+
optionToJson,
|
|
394
|
+
optionFromJson
|
|
395
|
+
];
|
|
396
|
+
|
|
397
|
+
var unit = [
|
|
398
|
+
unitToJson,
|
|
399
|
+
unitFromJson
|
|
400
|
+
];
|
|
401
|
+
|
|
402
|
+
var Codecs = {
|
|
403
|
+
falseableEncode: Spice_Codecs.falseableEncode,
|
|
404
|
+
falseableDecode: Spice_Codecs.falseableDecode,
|
|
405
|
+
falseable: Spice_Codecs.falseable,
|
|
406
|
+
magicDecode: Spice_Codecs.magicDecode,
|
|
407
|
+
magic: Spice_Codecs.magic,
|
|
408
|
+
string: string,
|
|
409
|
+
$$int: $$int,
|
|
410
|
+
int64Unsafe: int64Unsafe,
|
|
411
|
+
$$float: $$float,
|
|
412
|
+
bool: bool,
|
|
413
|
+
array: array,
|
|
414
|
+
list: list,
|
|
415
|
+
option: option,
|
|
416
|
+
unit: unit
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
exports.error = error;
|
|
420
|
+
exports.stringToJson = stringToJson;
|
|
421
|
+
exports.stringFromJson = stringFromJson;
|
|
422
|
+
exports.intToJson = intToJson;
|
|
423
|
+
exports.intFromJson = intFromJson;
|
|
424
|
+
exports.int64ToJson = int64ToJson;
|
|
425
|
+
exports.int64FromJson = int64FromJson;
|
|
426
|
+
exports.int64ToJsonUnsafe = int64ToJsonUnsafe;
|
|
427
|
+
exports.int64FromJsonUnsafe = int64FromJsonUnsafe;
|
|
428
|
+
exports.floatToJson = floatToJson;
|
|
429
|
+
exports.floatFromJson = floatFromJson;
|
|
430
|
+
exports.boolToJson = boolToJson;
|
|
431
|
+
exports.boolFromJson = boolFromJson;
|
|
432
|
+
exports.unitToJson = unitToJson;
|
|
433
|
+
exports.unitFromJson = unitFromJson;
|
|
434
|
+
exports.arrayToJson = arrayToJson;
|
|
435
|
+
exports.arrayFromJson = arrayFromJson;
|
|
436
|
+
exports.listToJson = listToJson;
|
|
437
|
+
exports.listFromJson = listFromJson;
|
|
438
|
+
exports.filterOptional = filterOptional;
|
|
439
|
+
exports.optionToJson = optionToJson;
|
|
440
|
+
exports.optionFromJson = optionFromJson;
|
|
441
|
+
exports.resultToJson = resultToJson;
|
|
442
|
+
exports.resultFromJson = resultFromJson;
|
|
443
|
+
exports.dictToJson = dictToJson;
|
|
444
|
+
exports.dictFromJson = dictFromJson;
|
|
445
|
+
exports.Codecs = Codecs;
|
|
446
|
+
/* No side effect */
|
package/src/rescript/Spice.res
CHANGED
|
@@ -24,7 +24,7 @@ let stringFromJson = j =>
|
|
|
24
24
|
| None => Belt.Result.Error({path: "", message: "Not a string", value: j})
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
let intToJson = i =>
|
|
27
|
+
let intToJson = i => Js.Json.number(float_of_int(i))
|
|
28
28
|
let intFromJson = j =>
|
|
29
29
|
switch Js.Json.decodeNumber(j) {
|
|
30
30
|
| Some(f) =>
|
|
@@ -35,7 +35,7 @@ let intFromJson = j =>
|
|
|
35
35
|
| _ => Belt.Result.Error({path: "", message: "Not a number", value: j})
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
let int64ToJson = i =>
|
|
38
|
+
let int64ToJson = i => Js.Json.number(Int64.float_of_bits(i))
|
|
39
39
|
|
|
40
40
|
let int64FromJson = j =>
|
|
41
41
|
switch Js.Json.decodeNumber(j) {
|
|
@@ -43,7 +43,7 @@ let int64FromJson = j =>
|
|
|
43
43
|
| None => error("Not a number", j)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
let int64ToJsonUnsafe = i =>
|
|
46
|
+
let int64ToJsonUnsafe = i => Js.Json.number(Int64.to_float(i))
|
|
47
47
|
|
|
48
48
|
let int64FromJsonUnsafe = j =>
|
|
49
49
|
switch Js.Json.decodeNumber(j) {
|
|
@@ -51,14 +51,14 @@ let int64FromJsonUnsafe = j =>
|
|
|
51
51
|
| None => error("Not a number", j)
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
let floatToJson = v =>
|
|
54
|
+
let floatToJson = v => Js.Json.number(v)
|
|
55
55
|
let floatFromJson = j =>
|
|
56
56
|
switch Js.Json.decodeNumber(j) {
|
|
57
57
|
| Some(f) => Belt.Result.Ok(f)
|
|
58
58
|
| None => Belt.Result.Error({path: "", message: "Not a number", value: j})
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
let boolToJson = v =>
|
|
61
|
+
let boolToJson = v => Js.Json.boolean(v)
|
|
62
62
|
let boolFromJson = j =>
|
|
63
63
|
switch Js.Json.decodeBoolean(j) {
|
|
64
64
|
| Some(b) => Belt.Result.Ok(b)
|
|
@@ -68,7 +68,7 @@ let boolFromJson = j =>
|
|
|
68
68
|
let unitToJson = () => Js.Json.number(0.0)
|
|
69
69
|
let unitFromJson = _ => Belt.Result.Ok()
|
|
70
70
|
|
|
71
|
-
let arrayToJson = (encoder, arr) =>
|
|
71
|
+
let arrayToJson = (encoder, arr) => Js.Json.array(Js.Array.map(encoder, arr))
|
|
72
72
|
|
|
73
73
|
let arrayFromJson = (decoder, json) =>
|
|
74
74
|
switch Js.Json.decodeArray(json) {
|
|
@@ -87,10 +87,16 @@ let arrayFromJson = (decoder, json) =>
|
|
|
87
87
|
| None => Belt.Result.Error({path: "", message: "Not an array", value: json})
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
let listToJson = (encoder, list) =>
|
|
90
|
+
let listToJson = (encoder, list) => arrayToJson(encoder, Belt.List.toArray(list))
|
|
91
91
|
|
|
92
92
|
let listFromJson = (decoder, json) =>
|
|
93
|
-
|
|
93
|
+
Belt.Result.map(arrayFromJson(decoder, json), Belt.List.fromArray)
|
|
94
|
+
|
|
95
|
+
let filterOptional = arr =>
|
|
96
|
+
Belt.Array.map(
|
|
97
|
+
Belt.Array.keep(arr, ((_, isOptional, x)) => !(isOptional && x == Js.Json.null)),
|
|
98
|
+
((k, _, v)) => (k, v),
|
|
99
|
+
)
|
|
94
100
|
|
|
95
101
|
let optionToJson = (encoder, opt) =>
|
|
96
102
|
switch opt {
|
|
@@ -98,22 +104,19 @@ let optionToJson = (encoder, opt) =>
|
|
|
98
104
|
| None => Js.Json.null
|
|
99
105
|
}
|
|
100
106
|
|
|
101
|
-
let filterOptional = arr =>
|
|
102
|
-
arr
|
|
103
|
-
|> Belt.Array.keep(_, ((_, isOptional, x)) => !(isOptional && x == Js.Json.null))
|
|
104
|
-
|> Belt.Array.map(_, ((k, _, v)) => (k, v))
|
|
105
|
-
|
|
106
107
|
let optionFromJson = (decoder, json) =>
|
|
107
108
|
switch Js.Json.decodeNull(json) {
|
|
108
109
|
| Some(_) => Belt.Result.Ok(None)
|
|
109
|
-
| None =>
|
|
110
|
+
| None => Belt.Result.map(decoder(json), v => Some(v))
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
let resultToJson = (okEncoder, errorEncoder, result) =>
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
Js.Json.array(
|
|
115
|
+
switch result {
|
|
116
|
+
| Belt.Result.Ok(v) => [Js.Json.string("Ok"), okEncoder(v)]
|
|
117
|
+
| Belt.Result.Error(e) => [Js.Json.string("Error"), errorEncoder(e)]
|
|
118
|
+
},
|
|
119
|
+
)
|
|
117
120
|
|
|
118
121
|
let resultFromJson = (okDecoder, errorDecoder, json) =>
|
|
119
122
|
switch Js.Json.decodeArray(json) {
|
|
@@ -134,7 +137,7 @@ let resultFromJson = (okDecoder, errorDecoder, json) =>
|
|
|
134
137
|
| None => error("Not an array", json)
|
|
135
138
|
}
|
|
136
139
|
|
|
137
|
-
let dictToJson = (encoder, dict) =>
|
|
140
|
+
let dictToJson = (encoder, dict) => Js.Json.object_(Js.Dict.map(a => encoder(a), dict))
|
|
138
141
|
|
|
139
142
|
let dictFromJson = (decoder, json) =>
|
|
140
143
|
switch Js.Json.decodeObject(json) {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var Js_json = require("rescript/lib/js/js_json.js");
|
|
5
|
+
var Belt_Result = require("rescript/lib/js/belt_Result.js");
|
|
6
|
+
var Caml_option = require("rescript/lib/js/caml_option.js");
|
|
7
|
+
|
|
8
|
+
function falseableEncode(encoder, opt) {
|
|
9
|
+
if (opt !== undefined) {
|
|
10
|
+
return encoder(Caml_option.valFromOption(opt));
|
|
11
|
+
} else {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function falseableDecode(decoder, json) {
|
|
17
|
+
var match = Js_json.decodeBoolean(json);
|
|
18
|
+
if (match !== undefined && !match) {
|
|
19
|
+
return {
|
|
20
|
+
TAG: "Ok",
|
|
21
|
+
_0: undefined
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return Belt_Result.map(decoder(json), (function (v) {
|
|
25
|
+
return Caml_option.some(v);
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var falseable = [
|
|
30
|
+
falseableEncode,
|
|
31
|
+
falseableDecode
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
function magicDecode(j) {
|
|
35
|
+
return {
|
|
36
|
+
TAG: "Ok",
|
|
37
|
+
_0: j
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function magic_0(prim) {
|
|
42
|
+
return prim;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
var magic = [
|
|
46
|
+
magic_0,
|
|
47
|
+
magicDecode
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
exports.falseableEncode = falseableEncode;
|
|
51
|
+
exports.falseableDecode = falseableDecode;
|
|
52
|
+
exports.falseable = falseable;
|
|
53
|
+
exports.magicDecode = magicDecode;
|
|
54
|
+
exports.magic = magic;
|
|
55
|
+
/* No side effect */
|
|
@@ -6,7 +6,7 @@ let falseableEncode = (encoder, opt) =>
|
|
|
6
6
|
let falseableDecode = (decoder, json) =>
|
|
7
7
|
switch Js.Json.decodeBoolean(json) {
|
|
8
8
|
| Some(false) => Belt.Result.Ok(None)
|
|
9
|
-
| _ =>
|
|
9
|
+
| _ => Belt.Result.map(decoder(json), v => Some(v))
|
|
10
10
|
}
|
|
11
11
|
let falseable = (falseableEncode, falseableDecode)
|
|
12
12
|
|