@kaiko.io/rescript-deser 6.0.1 → 7.0.0-alpha.2
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/README.md +16 -5
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/build.ninja +0 -27
- package/lib/bs/compiler-info.json +8 -0
- package/lib/bs/src/Deser.ast +0 -0
- package/lib/bs/src/Deser.cmi +0 -0
- package/lib/bs/src/Deser.cmj +0 -0
- package/lib/bs/src/Deser.cmt +0 -0
- package/lib/bs/src/Deser.res +341 -0
- package/lib/bs/tests/QUnit.ast +0 -0
- package/lib/bs/tests/QUnit.cmi +0 -0
- package/lib/bs/tests/QUnit.cmj +0 -0
- package/lib/bs/tests/QUnit.cmt +0 -0
- package/lib/bs/tests/QUnit.res +72 -0
- package/lib/bs/tests/index.ast +0 -0
- package/lib/bs/tests/index.cmi +0 -0
- package/lib/bs/tests/index.cmj +0 -0
- package/lib/bs/tests/index.cmt +0 -0
- package/lib/bs/tests/index.res +212 -0
- package/lib/es6/src/Deser.js +302 -477
- package/lib/es6/tests/index.js +260 -241
- package/lib/js/src/Deser.js +298 -473
- package/lib/js/tests/index.js +261 -242
- package/lib/ocaml/.compiler.log +2 -0
- package/lib/ocaml/Deser.ast +0 -0
- package/lib/ocaml/Deser.cmi +0 -0
- package/lib/ocaml/Deser.cmj +0 -0
- package/lib/ocaml/Deser.cmt +0 -0
- package/lib/ocaml/Deser.res +341 -0
- package/lib/ocaml/QUnit.ast +0 -0
- package/lib/ocaml/QUnit.cmi +0 -0
- package/lib/ocaml/QUnit.cmj +0 -0
- package/lib/ocaml/QUnit.cmt +0 -0
- package/lib/ocaml/QUnit.res +72 -0
- package/lib/ocaml/index.ast +0 -0
- package/lib/ocaml/index.cmi +0 -0
- package/lib/ocaml/index.cmj +0 -0
- package/lib/ocaml/index.cmt +0 -0
- package/lib/ocaml/index.res +212 -0
- package/lib/rescript.lock +1 -0
- package/package.json +5 -6
- package/rescript.json +4 -6
- package/src/Deser.res +86 -97
- package/tests/QUnit.res +4 -4
- package/tests/index.res +34 -11
- package/tests/run-tests.js +192 -0
- package/yarn.lock +683 -0
- package/lib/bs/.bsbuild +0 -0
- package/lib/bs/.bsdeps +0 -9
- package/lib/bs/.ninja_log +0 -93
- package/lib/bs/.project-files-cache +0 -0
- package/lib/bs/.sourcedirs.json +0 -1
- package/lib/bs/install.ninja +0 -10
- package/lib/bs/src/Deser.d +0 -0
- package/lib/bs/tests/QUnit.d +0 -0
- package/lib/bs/tests/index.d +0 -1
package/lib/js/tests/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
let Deser = require("../src/Deser.js");
|
|
5
|
+
let Qunit = require("qunit");
|
|
6
|
+
let Stdlib_Result = require("@rescript/runtime/lib/js/Stdlib_Result.js");
|
|
7
|
+
let Primitive_exceptions = require("@rescript/runtime/lib/js/Primitive_exceptions.js");
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
let fields = {
|
|
9
10
|
TAG: "Object",
|
|
10
11
|
_0: [
|
|
11
12
|
[
|
|
@@ -26,257 +27,275 @@ var fields = {
|
|
|
26
27
|
]
|
|
27
28
|
};
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
let Deserializer = Deser.MakeDeserializer({
|
|
31
|
+
fields: fields
|
|
32
|
+
});
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
let Appointment = {
|
|
34
35
|
Deserializer: Deserializer
|
|
35
36
|
};
|
|
36
37
|
|
|
37
|
-
Qunit.module("Basic deserializer",
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
Qunit.module("Basic deserializer", param => {
|
|
39
|
+
let valid = [
|
|
40
|
+
[
|
|
41
|
+
{"note": "Bicentennial doctor's appointment", "date": "2100-01-01"},
|
|
42
|
+
{
|
|
43
|
+
note: "Bicentennial doctor's appointment",
|
|
44
|
+
date: new Date("2100-01-01"),
|
|
45
|
+
extra: undefined
|
|
46
|
+
}
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
{
|
|
49
50
|
"note": "Bicentennial doctor's appointment",
|
|
50
51
|
"date": "2100-01-01",
|
|
51
52
|
"extra": "Don't take your stop-aging pills the night before the appointment",
|
|
52
53
|
},
|
|
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
|
-
|
|
54
|
+
{
|
|
55
|
+
note: "Bicentennial doctor's appointment",
|
|
56
|
+
date: new Date("2100-01-01"),
|
|
57
|
+
extra: "Don't take your stop-aging pills the night before the appointment"
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
];
|
|
61
|
+
Qunit.test("Correctly deserializes data", qunit => {
|
|
62
|
+
qunit.expect(valid.length);
|
|
63
|
+
valid.forEach(param => {
|
|
64
|
+
let data = param[0];
|
|
65
|
+
console.log("Running sample", data);
|
|
66
|
+
let result = Deserializer.fromJSON(data);
|
|
67
|
+
if (result.TAG === "Ok") {
|
|
68
|
+
qunit.deepEqual(result._0, param[1], "result == expected");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
console.error(result._0);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
let invalid = [[
|
|
75
|
+
"Missing non-optional field",
|
|
76
|
+
{"extra": "Bicentennial doctor's appointment", "date": "2100-01-01"}
|
|
77
|
+
]];
|
|
78
|
+
Qunit.test("Correctly catches invalid data", qunit => {
|
|
79
|
+
qunit.expect(invalid.length);
|
|
80
|
+
invalid.forEach(param => {
|
|
81
|
+
let data = param[1];
|
|
82
|
+
console.log("Running sample", param[0], data);
|
|
83
|
+
let result = Deserializer.fromJSON(data);
|
|
84
|
+
if (result.TAG === "Ok") {
|
|
85
|
+
console.error("Invalid being accepted: ", result._0);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
console.log("Correctly detected:", result._0);
|
|
89
|
+
qunit.ok(true, true);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
92
93
|
|
|
93
|
-
Qunit.module("Recursive deserializer",
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
94
|
+
Qunit.module("Recursive deserializer", param => {
|
|
95
|
+
let fields = {
|
|
96
|
+
TAG: "Object",
|
|
97
|
+
_0: [
|
|
98
|
+
[
|
|
99
|
+
"data",
|
|
100
|
+
"Any"
|
|
101
|
+
],
|
|
102
|
+
[
|
|
103
|
+
"children",
|
|
104
|
+
{
|
|
105
|
+
TAG: "Array",
|
|
106
|
+
_0: "Self"
|
|
107
|
+
}
|
|
108
|
+
]
|
|
109
|
+
]
|
|
110
|
+
};
|
|
111
|
+
let DeserializerImpl = Deser.MakeDeserializer({
|
|
112
|
+
fields: fields
|
|
113
|
+
});
|
|
114
|
+
let checkFieldsSanity = DeserializerImpl.checkFieldsSanity;
|
|
115
|
+
let valid = [[
|
|
116
|
+
{"data": "A", "children": [{"data": "A1", "children": []}, {"data": "B", "children": [{"data": "C", "children": []}, {"data": "D", "children": []}]}]},
|
|
117
|
+
{
|
|
118
|
+
data: "A",
|
|
119
|
+
children: [
|
|
120
|
+
{
|
|
121
|
+
data: "A1",
|
|
122
|
+
children: []
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
data: "B",
|
|
126
|
+
children: [
|
|
103
127
|
{
|
|
104
|
-
|
|
105
|
-
|
|
128
|
+
data: "C",
|
|
129
|
+
children: []
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
data: "D",
|
|
133
|
+
children: []
|
|
106
134
|
}
|
|
107
135
|
]
|
|
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
|
-
TAG: "Optional",
|
|
222
|
-
_0: "Self"
|
|
223
|
-
}
|
|
224
|
-
]
|
|
225
|
-
]
|
|
226
|
-
};
|
|
227
|
-
var List = Deser.MakeDeserializer({
|
|
228
|
-
fields: fields
|
|
229
|
-
});
|
|
230
|
-
var fields$1 = {
|
|
231
|
-
TAG: "Object",
|
|
232
|
-
_0: [
|
|
233
|
-
[
|
|
234
|
-
"records",
|
|
235
|
-
{
|
|
236
|
-
TAG: "Deserializer",
|
|
237
|
-
_0: List
|
|
238
|
-
}
|
|
239
|
-
],
|
|
240
|
-
[
|
|
241
|
-
"next",
|
|
242
|
-
{
|
|
243
|
-
TAG: "Optional",
|
|
244
|
-
_0: "Self"
|
|
245
|
-
}
|
|
246
|
-
]
|
|
247
|
-
]
|
|
248
|
-
};
|
|
249
|
-
var Ledger = Deser.MakeDeserializer({
|
|
250
|
-
fields: fields$1
|
|
251
|
-
});
|
|
252
|
-
var data = {"records": {"head": "A", "tail": {"head": "B"}},
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
]];
|
|
140
|
+
Qunit.test("Trivial recursion detection: Ok", qunit => {
|
|
141
|
+
qunit.expect(1);
|
|
142
|
+
qunit.deepEqual(checkFieldsSanity(), {
|
|
143
|
+
TAG: "Ok",
|
|
144
|
+
_0: undefined
|
|
145
|
+
}, "Ok");
|
|
146
|
+
});
|
|
147
|
+
Qunit.test("Infinite list", qunit => {
|
|
148
|
+
let fields = {
|
|
149
|
+
TAG: "Object",
|
|
150
|
+
_0: [
|
|
151
|
+
[
|
|
152
|
+
"head",
|
|
153
|
+
"String"
|
|
154
|
+
],
|
|
155
|
+
[
|
|
156
|
+
"tail",
|
|
157
|
+
"Self"
|
|
158
|
+
]
|
|
159
|
+
]
|
|
160
|
+
};
|
|
161
|
+
let InfiniteList = Deser.MakeDeserializer({
|
|
162
|
+
fields: fields
|
|
163
|
+
});
|
|
164
|
+
qunit.expect(1);
|
|
165
|
+
qunit.deepEqual(Stdlib_Result.isError(InfiniteList.checkFieldsSanity()), true, "Ok");
|
|
166
|
+
});
|
|
167
|
+
Qunit.test("Finite list", qunit => {
|
|
168
|
+
let fields = {
|
|
169
|
+
TAG: "Object",
|
|
170
|
+
_0: [
|
|
171
|
+
[
|
|
172
|
+
"head",
|
|
173
|
+
"String"
|
|
174
|
+
],
|
|
175
|
+
[
|
|
176
|
+
"tail",
|
|
177
|
+
{
|
|
178
|
+
TAG: "Optional",
|
|
179
|
+
_0: "Self"
|
|
180
|
+
}
|
|
181
|
+
]
|
|
182
|
+
]
|
|
183
|
+
};
|
|
184
|
+
let List = Deser.MakeDeserializer({
|
|
185
|
+
fields: fields
|
|
186
|
+
});
|
|
187
|
+
qunit.expect(1);
|
|
188
|
+
qunit.deepEqual(List.checkFieldsSanity(), {
|
|
189
|
+
TAG: "Ok",
|
|
190
|
+
_0: undefined
|
|
191
|
+
}, "Ok");
|
|
192
|
+
});
|
|
193
|
+
Qunit.test("Correctly deserializes recursive data", qunit => {
|
|
194
|
+
qunit.expect(valid.length);
|
|
195
|
+
valid.forEach(param => {
|
|
196
|
+
let data = param[0];
|
|
197
|
+
console.log("Running sample", data);
|
|
198
|
+
let result = Stdlib_Result.map(DeserializerImpl.fromJSON(data), x => x);
|
|
199
|
+
if (result.TAG === "Ok") {
|
|
200
|
+
qunit.deepEqual(result._0, param[1], "result == expected");
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
console.error(result._0);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
Qunit.test("Recursion in sub-deserializer", qunit => {
|
|
207
|
+
let fields = {
|
|
208
|
+
TAG: "Object",
|
|
209
|
+
_0: [
|
|
210
|
+
[
|
|
211
|
+
"head",
|
|
212
|
+
"String"
|
|
213
|
+
],
|
|
214
|
+
[
|
|
215
|
+
"tail",
|
|
216
|
+
{
|
|
217
|
+
TAG: "Optional",
|
|
218
|
+
_0: "Self"
|
|
219
|
+
}
|
|
220
|
+
]
|
|
221
|
+
]
|
|
222
|
+
};
|
|
223
|
+
let List = Deser.MakeDeserializer({
|
|
224
|
+
fields: fields
|
|
225
|
+
});
|
|
226
|
+
let fields$1 = {
|
|
227
|
+
TAG: "Object",
|
|
228
|
+
_0: [
|
|
229
|
+
[
|
|
230
|
+
"records",
|
|
231
|
+
{
|
|
232
|
+
TAG: "Deserializer",
|
|
233
|
+
_0: List
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
[
|
|
237
|
+
"next",
|
|
238
|
+
{
|
|
239
|
+
TAG: "Optional",
|
|
240
|
+
_0: "Self"
|
|
241
|
+
}
|
|
242
|
+
]
|
|
243
|
+
]
|
|
244
|
+
};
|
|
245
|
+
let Ledger = Deser.MakeDeserializer({
|
|
246
|
+
fields: fields$1
|
|
247
|
+
});
|
|
248
|
+
let data = {"records": {"head": "A", "tail": {"head": "B"}},
|
|
253
249
|
"next": {"records": {"head": "A", "tail": {"head": "B"}}}};
|
|
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
|
-
|
|
250
|
+
let expected = {
|
|
251
|
+
records: {
|
|
252
|
+
head: "A",
|
|
253
|
+
tail: {
|
|
254
|
+
head: "B",
|
|
255
|
+
tail: undefined
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
next: {
|
|
259
|
+
records: {
|
|
260
|
+
head: "A",
|
|
261
|
+
tail: {
|
|
262
|
+
head: "B",
|
|
263
|
+
tail: undefined
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
next: undefined
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
qunit.expect(1);
|
|
270
|
+
qunit.deepEqual(Ledger.fromJSON(data), {
|
|
271
|
+
TAG: "Ok",
|
|
272
|
+
_0: expected
|
|
273
|
+
}, "nice ledger");
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
Qunit.module("Type safety limits", param => {
|
|
278
|
+
Qunit.test("ill-defined deserializer can cheat the type system", qunit => {
|
|
279
|
+
let X = Deser.MakeDeserializer({
|
|
280
|
+
fields: {
|
|
281
|
+
TAG: "Array",
|
|
282
|
+
_0: "Int"
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
let data = [1];
|
|
286
|
+
qunit.throws(param => {
|
|
287
|
+
let illString = Stdlib_Result.getOr(X.fromJSON(data), "");
|
|
288
|
+
try {
|
|
289
|
+
console.info("Index", "This will fail with a type error", illString.charAt(0));
|
|
290
|
+
return;
|
|
291
|
+
} catch (raw_e) {
|
|
292
|
+
let e = Primitive_exceptions.internalToException(raw_e);
|
|
293
|
+
console.error(e);
|
|
294
|
+
throw e;
|
|
295
|
+
}
|
|
296
|
+
}, "Expected: TypeError: illString.charAt is not a function");
|
|
297
|
+
});
|
|
298
|
+
});
|
|
280
299
|
|
|
281
300
|
exports.Appointment = Appointment;
|
|
282
301
|
/* Deserializer Not a pure module */
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|