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