@kaiko.io/rescript-deser 4.0.0-beta.1 → 4.0.0-beta.3

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/lib/bs/.bsdeps CHANGED
@@ -2,7 +2,7 @@
2
2
  /home/manu/src/kaiko/rescript-deser
3
3
  0
4
4
  bsconfig.json 0x1.92beacc6fcbbbp+30
5
- tests 0x1.92be24d20cc42p+30
6
- src/ 0x1.92be2684bc9cdp+30
5
+ tests 0x1.936c441d4e1fap+30
6
+ src/ 0x1.936c42e78597ap+30
7
7
  ===
8
8
  /home/manu/src/kaiko/rescript-deser/node_modules/rescript/linux/rescript.exe 0x1.92e953b26353bp+30
@@ -1,2 +1,2 @@
1
- #Start(1690472987373)
2
- #Done(1690472987373)
1
+ #Start(1692078836285)
2
+ #Done(1692078836285)
package/lib/bs/.ninja_log CHANGED
@@ -17,3 +17,67 @@
17
17
  30 42 1690472977014278294 tests/index.cmi bfa81ebb23ddb1d3
18
18
  30 42 1690472977014278294 ../es6/tests/index.js bfa81ebb23ddb1d3
19
19
  30 42 1690472977014278294 ../js/tests/index.js bfa81ebb23ddb1d3
20
+ 0 8 1690472976984278051 tests/QUnit.cmj 2ec5fa040bfd2d9d
21
+ 0 8 1690472976984278051 tests/QUnit.cmi 2ec5fa040bfd2d9d
22
+ 0 8 1690472976984278051 ../es6/tests/QUnit.js 2ec5fa040bfd2d9d
23
+ 0 8 1690472976984278051 ../js/tests/QUnit.js 2ec5fa040bfd2d9d
24
+ 0 21 1690472976984278051 src/JSON.cmj 72e6767661d939a8
25
+ 0 21 1690472976984278051 src/JSON.cmi 72e6767661d939a8
26
+ 0 21 1690472976984278051 ../es6/src/JSON.js 72e6767661d939a8
27
+ 0 21 1690472976984278051 ../js/src/JSON.js 72e6767661d939a8
28
+ 21 34 1690472977004278213 tests/index.cmj 99dcd7620c5e344f
29
+ 21 34 1690472977004278213 tests/index.cmi 99dcd7620c5e344f
30
+ 21 34 1690472977004278213 ../es6/tests/index.js 99dcd7620c5e344f
31
+ 21 34 1690472977004278213 ../js/tests/index.js 99dcd7620c5e344f
32
+ 1 8 1692077881863146346 src/JSON.ast ec417eedaf9813d7
33
+ 8 14 1692077881863146346 src/JSON.d cd2d6773c781340f
34
+ 0 9 1692077894949942101 src/JSON.ast ec417eedaf9813d7
35
+ 9 11 1692077894949942101 src/JSON.d cd2d6773c781340f
36
+ 0 8 1692078180322757231 src/JSON.ast ec417eedaf9813d7
37
+ 8 10 1692078180322757231 src/JSON.d cd2d6773c781340f
38
+ 0 7 1692078195156236902 src/JSON.ast ec417eedaf9813d7
39
+ 7 10 1692078195156236902 src/JSON.d cd2d6773c781340f
40
+ 10 35 1692078195156236902 src/JSON.cmj 72e6767661d939a8
41
+ 10 35 1692078195156236902 src/JSON.cmi 72e6767661d939a8
42
+ 10 35 1692078195156236902 ../es6/src/JSON.js 72e6767661d939a8
43
+ 10 35 1692078195156236902 ../js/src/JSON.js 72e6767661d939a8
44
+ 35 50 1692078195176237099 tests/index.cmj 99dcd7620c5e344f
45
+ 35 50 1692078195176237099 tests/index.cmi 99dcd7620c5e344f
46
+ 35 50 1692078195176237099 ../es6/tests/index.js 99dcd7620c5e344f
47
+ 35 50 1692078195176237099 ../js/tests/index.js 99dcd7620c5e344f
48
+ 0 7 1692078280717274299 src/JSON.ast ec417eedaf9813d7
49
+ 7 9 1692078280717274299 src/JSON.d cd2d6773c781340f
50
+ 9 30 1692078280717274299 src/JSON.cmj 72e6767661d939a8
51
+ 9 30 1692078280717274299 src/JSON.cmi 72e6767661d939a8
52
+ 9 30 1692078280717274299 ../es6/src/JSON.js 72e6767661d939a8
53
+ 9 30 1692078280717274299 ../js/src/JSON.js 72e6767661d939a8
54
+ 30 42 1692078280733941130 tests/index.cmj 99dcd7620c5e344f
55
+ 30 42 1692078280733941130 tests/index.cmi 99dcd7620c5e344f
56
+ 30 42 1692078280733941130 ../es6/tests/index.js 99dcd7620c5e344f
57
+ 30 42 1692078280733941130 ../js/tests/index.js 99dcd7620c5e344f
58
+ 0 6 1692078378979358674 tests/index.ast af1b9dde75b669ce
59
+ 6 7 1692078378979358674 tests/index.d 5a4db9cdecd186b3
60
+ 8 20 1692078378979358674 tests/index.cmj 99dcd7620c5e344f
61
+ 8 20 1692078378979358674 tests/index.cmi 99dcd7620c5e344f
62
+ 8 20 1692078378979358674 ../es6/tests/index.js 99dcd7620c5e344f
63
+ 8 20 1692078378979358674 ../js/tests/index.js 99dcd7620c5e344f
64
+ 0 8 1692078483192900791 src/JSON.ast ec417eedaf9813d7
65
+ 8 10 1692078483192900791 src/JSON.d cd2d6773c781340f
66
+ 0 7 1692078491420312038 src/JSON.ast ec417eedaf9813d7
67
+ 7 10 1692078491420312038 src/JSON.d cd2d6773c781340f
68
+ 0 7 1692078495960690503 src/JSON.ast ec417eedaf9813d7
69
+ 7 10 1692078495960690503 src/JSON.d cd2d6773c781340f
70
+ 0 7 1692078499500970674 src/JSON.ast ec417eedaf9813d7
71
+ 7 9 1692078499500970674 src/JSON.d cd2d6773c781340f
72
+ 0 8 1692078506824846034 src/JSON.ast ec417eedaf9813d7
73
+ 8 10 1692078506824846034 src/JSON.d cd2d6773c781340f
74
+ 0 8 1692078511601840838 src/JSON.ast ec417eedaf9813d7
75
+ 8 10 1692078511601840838 src/JSON.d cd2d6773c781340f
76
+ 10 30 1692078511601840838 src/JSON.cmj 72e6767661d939a8
77
+ 10 30 1692078511601840838 src/JSON.cmi 72e6767661d939a8
78
+ 10 30 1692078511601840838 ../es6/src/JSON.js 72e6767661d939a8
79
+ 10 30 1692078511601840838 ../js/src/JSON.js 72e6767661d939a8
80
+ 30 43 1692078511615175063 tests/index.cmj 99dcd7620c5e344f
81
+ 30 43 1692078511615175063 tests/index.cmi 99dcd7620c5e344f
82
+ 30 43 1692078511615175063 ../es6/tests/index.js 99dcd7620c5e344f
83
+ 30 43 1692078511615175063 ../js/tests/index.js 99dcd7620c5e344f
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -606,7 +606,7 @@ var Field = {
606
606
 
607
607
  function MakeDeserializer(S) {
608
608
  var fields = S.fields;
609
- var name = "Deserializer " + "JSON" + ", " + "File \"JSON.res\", line 320, characters 39-47";
609
+ var name = "Deserializer " + "JSON" + ", " + "File \"JSON.res\", line 329, characters 39-47";
610
610
  var checkFieldsSanity$1 = function () {
611
611
  return checkFieldsSanity(name, fields, false);
612
612
  };
@@ -630,9 +630,35 @@ function MakeDeserializer(S) {
630
630
  _0: res
631
631
  };
632
632
  };
633
+ var fromAny = function (any) {
634
+ var match = typeof any;
635
+ if (match === "string") {
636
+ var parsed;
637
+ try {
638
+ parsed = JSON.parse(any);
639
+ }
640
+ catch (exn){
641
+ return {
642
+ TAG: "Error",
643
+ _0: "Could parse string as JSON"
644
+ };
645
+ }
646
+ return fromJSON(parsed);
647
+ }
648
+ try {
649
+ return fromJSON(any);
650
+ }
651
+ catch (exn$1){
652
+ return {
653
+ TAG: "Error",
654
+ _0: "Could not deserialize the data"
655
+ };
656
+ }
657
+ };
633
658
  return {
634
659
  name: name,
635
660
  fromJSON: fromJSON,
661
+ fromAny: fromAny,
636
662
  checkFieldsSanity: checkFieldsSanity$1
637
663
  };
638
664
  }
@@ -234,7 +234,11 @@ Qunit.module("Recursive deserializer", (function (param) {
234
234
  "records",
235
235
  {
236
236
  TAG: "Deserializer",
237
- _0: List
237
+ _0: {
238
+ name: List.name,
239
+ fromJSON: List.fromJSON,
240
+ checkFieldsSanity: List.checkFieldsSanity
241
+ }
238
242
  }
239
243
  ],
240
244
  [
@@ -607,7 +607,7 @@ var Field = {
607
607
 
608
608
  function MakeDeserializer(S) {
609
609
  var fields = S.fields;
610
- var name = "Deserializer " + "JSON" + ", " + "File \"JSON.res\", line 320, characters 39-47";
610
+ var name = "Deserializer " + "JSON" + ", " + "File \"JSON.res\", line 329, characters 39-47";
611
611
  var checkFieldsSanity$1 = function () {
612
612
  return checkFieldsSanity(name, fields, false);
613
613
  };
@@ -631,9 +631,35 @@ function MakeDeserializer(S) {
631
631
  _0: res
632
632
  };
633
633
  };
634
+ var fromAny = function (any) {
635
+ var match = typeof any;
636
+ if (match === "string") {
637
+ var parsed;
638
+ try {
639
+ parsed = JSON.parse(any);
640
+ }
641
+ catch (exn){
642
+ return {
643
+ TAG: "Error",
644
+ _0: "Could parse string as JSON"
645
+ };
646
+ }
647
+ return fromJSON(parsed);
648
+ }
649
+ try {
650
+ return fromJSON(any);
651
+ }
652
+ catch (exn$1){
653
+ return {
654
+ TAG: "Error",
655
+ _0: "Could not deserialize the data"
656
+ };
657
+ }
658
+ };
634
659
  return {
635
660
  name: name,
636
661
  fromJSON: fromJSON,
662
+ fromAny: fromAny,
637
663
  checkFieldsSanity: checkFieldsSanity$1
638
664
  };
639
665
  }
@@ -235,7 +235,11 @@ Qunit.module("Recursive deserializer", (function (param) {
235
235
  "records",
236
236
  {
237
237
  TAG: "Deserializer",
238
- _0: List
238
+ _0: {
239
+ name: List.name,
240
+ fromJSON: List.fromJSON,
241
+ checkFieldsSanity: List.checkFieldsSanity
242
+ }
239
243
  }
240
244
  ],
241
245
  [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaiko.io/rescript-deser",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.3",
4
4
  "keywords": [
5
5
  "json",
6
6
  "deserializer",
@@ -20,7 +20,7 @@
20
20
  "README.md"
21
21
  ],
22
22
  "dependencies": {
23
- "@kaiko.io/rescript-prelude": "7.0.0-alpha.3",
23
+ "@kaiko.io/rescript-prelude": "7.0.0-beta.1",
24
24
  "rescript": "11.0.0-beta.4"
25
25
  },
26
26
  "devDependencies": {
package/src/JSON.res CHANGED
@@ -23,11 +23,20 @@ module FieldValue = {
23
23
 
24
24
  exception TypeError(string)
25
25
 
26
- @doc("The module type of a built deserializer which is suitable to add as a subparser.")
26
+ @doc("The module type of a deserializer which is suitable to add as a subparser.")
27
+ module type ArgDeserializer = {
28
+ type t
29
+ let name: string
30
+ let fromJSON: Js.Json.t => result<t, string>
31
+ let checkFieldsSanity: unit => result<unit, string>
32
+ }
33
+
34
+ @doc("The module type of a built deserializer.")
27
35
  module type Deserializer = {
28
36
  type t
29
37
  let name: string
30
38
  let fromJSON: Js.Json.t => result<t, string>
39
+ let fromAny: 'a => result<t, string>
31
40
  let checkFieldsSanity: unit => result<unit, string>
32
41
  }
33
42
 
@@ -57,13 +66,13 @@ module Field = {
57
66
  /// expected entries.
58
67
  | Mapping(t)
59
68
 
60
- | Deserializer(module(Deserializer))
69
+ | Deserializer(module(ArgDeserializer))
61
70
 
62
71
  /// A specialized Array of deserialized items that ignores unparsable
63
72
  /// items and returns the valid collection. This saves the user from
64
73
  /// writing 'Array(DefaultWhenInvalid(Optional(Deserializer(module(M)))))'
65
74
  /// and then post-process the list of items with 'Array.keepSome'
66
- | Collection(module(Deserializer))
75
+ | Collection(module(ArgDeserializer))
67
76
  | DefaultWhenInvalid(t, FieldValue.t)
68
77
 
69
78
  // FIXME: this is used to add additional restrictions like variadictInt or
@@ -112,7 +121,7 @@ module Field = {
112
121
  | Date => "Date"
113
122
  | Self => "Self (recursive)"
114
123
  | Collection(m) => {
115
- module M = unpack(m: Deserializer)
124
+ module M = unpack(m: ArgDeserializer)
116
125
  "Collection of " ++ M.name
117
126
  }
118
127
 
@@ -129,7 +138,7 @@ module Field = {
129
138
  | Mapping(t) => `Mapping of ${t->toString}`
130
139
  | Morphism(t, _) => t->toString ++ " to apply a morphism"
131
140
  | Deserializer(m) => {
132
- module M = unpack(m: Deserializer)
141
+ module M = unpack(m: ArgDeserializer)
133
142
  M.name
134
143
  }
135
144
 
@@ -234,7 +243,7 @@ module Field = {
234
243
  | (Morphism(shape, f), _) => untagged->fromUntagged(shape, self)->f->FieldValue.any
235
244
 
236
245
  | (Collection(m), Js.Json.JSONArray(items)) => {
237
- module M = unpack(m: Deserializer)
246
+ module M = unpack(m: ArgDeserializer)
238
247
  items
239
248
  ->Array.map(M.fromJSON)
240
249
  ->Array.map(Result.warn)
@@ -244,7 +253,7 @@ module Field = {
244
253
  }
245
254
 
246
255
  | (Deserializer(m), _) => {
247
- module M = unpack(m: Deserializer)
256
+ module M = unpack(m: ArgDeserializer)
248
257
  switch untagged->M.fromJSON {
249
258
  | Ok(res) => res->FieldValue.any
250
259
  | Error(msg) => raise(TypeError(msg))
@@ -277,7 +286,7 @@ module Field = {
277
286
 
278
287
  | (Collection(mod), _)
279
288
  | (Deserializer(mod), _) => {
280
- module M = unpack(mod: Deserializer)
289
+ module M = unpack(mod: ArgDeserializer)
281
290
  switch M.checkFieldsSanity() {
282
291
  | Ok() => Ok()
283
292
  | Error(msg) => Error(`${name}/ ${msg}`)
@@ -320,7 +329,10 @@ module MakeDeserializer = (S: Serializable): (Deserializer with type t = S.t) =>
320
329
  %%private(let (loc, _f) = __LOC_OF__(module(S: Serializable)))
321
330
  let name = `Deserializer ${__MODULE__}, ${loc}`
322
331
 
323
- %%private(external _toNativeType: FieldValue.t => t = "%identity")
332
+ %%private(
333
+ external _toNativeType: FieldValue.t => t = "%identity"
334
+ external cast: 'a => 'b = "%identity"
335
+ )
324
336
 
325
337
  @doc("Checks for trivial infinite-recursion in the fields of the module.
326
338
 
@@ -340,4 +352,20 @@ module MakeDeserializer = (S: Serializable): (Deserializer with type t = S.t) =>
340
352
  | exception TypeError(e) => Error(e)
341
353
  }
342
354
  }
355
+
356
+ @doc("Try to parse anything")
357
+ let fromAny = any => {
358
+ switch Js.typeof(any) {
359
+ | "string" =>
360
+ switch any->cast->Js.Json.parseExn {
361
+ | parsed => fromJSON(parsed)
362
+ | exception _ => Error("Could parse string as JSON")
363
+ }
364
+ | _ =>
365
+ switch any->cast->fromJSON {
366
+ | result => result
367
+ | exception _ => Error("Could not deserialize the data")
368
+ }
369
+ }
370
+ }
343
371
  }