@greenlabs/ppx-spice 0.1.15 → 0.2.0-next.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/CHANGELOG.md +6 -0
- package/package.json +2 -2
- package/ppx-linux.exe +0 -0
- package/ppx-osx.exe +0 -0
- package/ppx-windows.exe +0 -0
- package/src/rescript/Spice.res +57 -53
- package/src/rescript/Spice_Codecs.res +3 -3
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@greenlabs/ppx-spice",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-next.0",
|
|
4
4
|
"description": "ReScript PPX which generate JSON (de)serializer",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Greenlabs Dev <developer@greenlabs.co.kr>",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"postinstall": "node ./postInstall.js"
|
|
20
20
|
},
|
|
21
|
-
"
|
|
21
|
+
"devDependencies": {
|
|
22
22
|
"@changesets/cli": "^2.26.2"
|
|
23
23
|
}
|
|
24
24
|
}
|
package/ppx-linux.exe
CHANGED
|
Binary file
|
package/ppx-osx.exe
CHANGED
|
Binary file
|
package/ppx-windows.exe
CHANGED
|
Binary file
|
package/src/rescript/Spice.res
CHANGED
|
@@ -17,17 +17,17 @@ let error = (~path=?, message, value) => {
|
|
|
17
17
|
Belt.Result.Error({path, message, value})
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
let stringToJson = s => Js.Json.
|
|
20
|
+
let stringToJson = (s): Js.Json.t => Js.Json.String(s)
|
|
21
21
|
let stringFromJson = j =>
|
|
22
|
-
switch Js.Json.
|
|
23
|
-
|
|
|
24
|
-
|
|
|
22
|
+
switch (j: Js.Json.t) {
|
|
23
|
+
| Js.Json.String(s) => Belt.Result.Ok(s)
|
|
24
|
+
| _ => Belt.Result.Error({path: "", message: "Not a string", value: j})
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
let intToJson = i => Js.Json.
|
|
27
|
+
let intToJson = (i): Js.Json.t => Js.Json.Number(float_of_int(i))
|
|
28
28
|
let intFromJson = j =>
|
|
29
|
-
switch Js.Json.
|
|
30
|
-
|
|
|
29
|
+
switch (j: Js.Json.t) {
|
|
30
|
+
| Js.Json.Number(f) =>
|
|
31
31
|
float_of_int(Js.Math.floor(f)) == f
|
|
32
32
|
? Belt.Result.Ok(Js.Math.floor(f))
|
|
33
33
|
: Belt.Result.Error({path: "", message: "Not an integer", value: j})
|
|
@@ -35,44 +35,49 @@ let intFromJson = j =>
|
|
|
35
35
|
| _ => Belt.Result.Error({path: "", message: "Not a number", value: j})
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
let int64ToJson = i => Js.Json.
|
|
38
|
+
let int64ToJson = (i): Js.Json.t => Js.Json.Number(Int64.float_of_bits(i))
|
|
39
39
|
|
|
40
40
|
let int64FromJson = j =>
|
|
41
|
-
switch Js.Json.
|
|
42
|
-
|
|
|
43
|
-
|
|
|
41
|
+
switch (j: Js.Json.t) {
|
|
42
|
+
| Js.Json.Number(n) => Belt.Result.Ok(Int64.bits_of_float(n))
|
|
43
|
+
| _ => error("Not a number", j)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
let int64ToJsonUnsafe = i => Js.Json.number(Int64.to_float(i))
|
|
46
|
+
let int64ToJsonUnsafe = (i): Js.Json.t => Js.Json.number(Int64.to_float(i))
|
|
47
47
|
|
|
48
48
|
let int64FromJsonUnsafe = j =>
|
|
49
|
-
switch Js.Json.
|
|
50
|
-
|
|
|
51
|
-
|
|
|
49
|
+
switch (j: Js.Json.t) {
|
|
50
|
+
| Js.Json.Number(n) => Belt.Result.Ok(Int64.of_float(n))
|
|
51
|
+
| _ => error("Not a number", j)
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
let floatToJson = v => Js.Json.
|
|
54
|
+
let floatToJson = (v): Js.Json.t => Js.Json.Number(v)
|
|
55
55
|
let floatFromJson = j =>
|
|
56
|
-
switch Js.Json.
|
|
57
|
-
|
|
|
58
|
-
|
|
|
56
|
+
switch (j: Js.Json.t) {
|
|
57
|
+
| Js.Json.Number(f) => Belt.Result.Ok(f)
|
|
58
|
+
| _ => Belt.Result.Error({path: "", message: "Not a number", value: j})
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
let boolToJson = v
|
|
61
|
+
let boolToJson = (v): Js.Json.t =>
|
|
62
|
+
switch v {
|
|
63
|
+
| true => Js.Json.True
|
|
64
|
+
| false => Js.Json.False
|
|
65
|
+
}
|
|
62
66
|
let boolFromJson = j =>
|
|
63
|
-
switch Js.Json.
|
|
64
|
-
|
|
|
65
|
-
|
|
|
67
|
+
switch (j: Js.Json.t) {
|
|
68
|
+
| Js.Json.True => Belt.Result.Ok(true)
|
|
69
|
+
| Js.Json.False => Belt.Result.Ok(false)
|
|
70
|
+
| _ => Belt.Result.Error({path: "", message: "Not a boolean", value: j})
|
|
66
71
|
}
|
|
67
72
|
|
|
68
|
-
let unitToJson = () => Js.Json.
|
|
73
|
+
let unitToJson = (): Js.Json.t => Js.Json.Number(0.0)
|
|
69
74
|
let unitFromJson = _ => Belt.Result.Ok()
|
|
70
75
|
|
|
71
|
-
let arrayToJson = (encoder, arr) => Js.Json.
|
|
76
|
+
let arrayToJson = (encoder, arr): Js.Json.t => Js.Json.Array(Js.Array.map(encoder, arr))
|
|
72
77
|
|
|
73
78
|
let arrayFromJson = (decoder, json) =>
|
|
74
|
-
switch Js.Json.
|
|
75
|
-
|
|
|
79
|
+
switch (json: Js.Json.t) {
|
|
80
|
+
| Js.Json.Array(arr) => Js.Array.reducei((acc, jsonI, i) =>
|
|
76
81
|
switch (acc, decoder(jsonI)) {
|
|
77
82
|
| (Belt.Result.Error(_), _) => acc
|
|
78
83
|
|
|
@@ -84,7 +89,7 @@ let arrayFromJson = (decoder, json) =>
|
|
|
84
89
|
}
|
|
85
90
|
, Belt.Result.Ok([]), arr)
|
|
86
91
|
|
|
87
|
-
|
|
|
92
|
+
| _ => Belt.Result.Error({path: "", message: "Not an array", value: json})
|
|
88
93
|
}
|
|
89
94
|
|
|
90
95
|
let listToJson = (encoder, list) => arrayToJson(encoder, Belt.List.toArray(list))
|
|
@@ -98,50 +103,49 @@ let filterOptional = arr =>
|
|
|
98
103
|
((k, _, v)) => (k, v),
|
|
99
104
|
)
|
|
100
105
|
|
|
101
|
-
let optionToJson = (encoder, opt) =>
|
|
106
|
+
let optionToJson = (encoder, opt): Js.Json.t =>
|
|
102
107
|
switch opt {
|
|
103
108
|
| Some(x) => encoder(x)
|
|
104
|
-
| None => Js.Json.
|
|
109
|
+
| None => Js.Json.Null
|
|
105
110
|
}
|
|
106
111
|
|
|
107
112
|
let optionFromJson = (decoder, json) =>
|
|
108
|
-
switch Js.Json.
|
|
109
|
-
|
|
|
110
|
-
|
|
|
113
|
+
switch (json: Js.Json.t) {
|
|
114
|
+
| Js.Json.Null => Belt.Result.Ok(None)
|
|
115
|
+
| _ => Belt.Result.map(decoder(json), v => Some(v))
|
|
111
116
|
}
|
|
112
117
|
|
|
113
|
-
let resultToJson = (okEncoder, errorEncoder, result) =>
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
)
|
|
118
|
+
let resultToJson = (okEncoder, errorEncoder, result): Js.Json.t => Js.Json.Array(
|
|
119
|
+
switch result {
|
|
120
|
+
| Belt.Result.Ok(v) => [Js.Json.String("Ok"), okEncoder(v)]
|
|
121
|
+
| Belt.Result.Error(e) => [Js.Json.String("Error"), errorEncoder(e)]
|
|
122
|
+
},
|
|
123
|
+
)
|
|
120
124
|
|
|
121
125
|
let resultFromJson = (okDecoder, errorDecoder, json) =>
|
|
122
|
-
switch Js.Json.
|
|
123
|
-
|
|
|
124
|
-
switch
|
|
125
|
-
|
|
|
126
|
+
switch (json: Js.Json.t) {
|
|
127
|
+
| Js.Json.Array([variantConstructorId, payload]) =>
|
|
128
|
+
switch variantConstructorId {
|
|
129
|
+
| Js.Json.String("Ok") => okDecoder(payload)->Belt.Result.map(v => Belt.Result.Ok(v))
|
|
126
130
|
|
|
127
|
-
|
|
|
131
|
+
| Js.Json.String("Error") =>
|
|
128
132
|
switch errorDecoder(payload) {
|
|
129
133
|
| Belt.Result.Ok(v) => Belt.Result.Ok(Belt.Result.Error(v))
|
|
130
134
|
| Belt.Result.Error(e) => Belt.Result.Error(e)
|
|
131
135
|
}
|
|
132
136
|
|
|
133
|
-
|
|
|
134
|
-
|
|
|
137
|
+
| Js.Json.String(_) => error("Expected either \"Ok\" or \"Error\"", variantConstructorId)
|
|
138
|
+
| _ => error("Not a string", variantConstructorId)
|
|
135
139
|
}
|
|
136
|
-
|
|
|
137
|
-
|
|
|
140
|
+
| Js.Json.Array(_) => error("Expected exactly 2 values in array", json)
|
|
141
|
+
| _ => error("Not an array", json)
|
|
138
142
|
}
|
|
139
143
|
|
|
140
|
-
let dictToJson = (encoder, dict) => Js.Json.
|
|
144
|
+
let dictToJson = (encoder, dict): Js.Json.t => Js.Json.Object(Js.Dict.map((. a) => encoder(a), dict))
|
|
141
145
|
|
|
142
146
|
let dictFromJson = (decoder, json) =>
|
|
143
|
-
switch Js.Json.
|
|
144
|
-
|
|
|
147
|
+
switch (json: Js.Json.t) {
|
|
148
|
+
| Js.Json.Object(dict) =>
|
|
145
149
|
dict
|
|
146
150
|
->Js.Dict.entries
|
|
147
151
|
->Belt.Array.reduce(Ok(Js.Dict.empty()), (acc, (key, value)) =>
|
|
@@ -155,7 +159,7 @@ let dictFromJson = (decoder, json) =>
|
|
|
155
159
|
Ok(prev)
|
|
156
160
|
}
|
|
157
161
|
)
|
|
158
|
-
|
|
|
162
|
+
| _ => Error({path: "", message: "Not a dict", value: json})
|
|
159
163
|
}
|
|
160
164
|
|
|
161
165
|
module Codecs = {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
let falseableEncode = (encoder, opt) =>
|
|
2
2
|
switch opt {
|
|
3
|
-
| None => Js.Json.
|
|
3
|
+
| None => Js.Json.False
|
|
4
4
|
| Some(v) => encoder(v)
|
|
5
5
|
}
|
|
6
6
|
let falseableDecode = (decoder, json) =>
|
|
7
|
-
switch
|
|
8
|
-
|
|
|
7
|
+
switch json {
|
|
8
|
+
| Js.Json.False => Belt.Result.Ok(None)
|
|
9
9
|
| _ => Belt.Result.map(decoder(json), v => Some(v))
|
|
10
10
|
}
|
|
11
11
|
let falseable = (falseableEncode, falseableDecode)
|
package/.changeset/README.md
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
# Changesets
|
|
2
|
-
|
|
3
|
-
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
|
|
4
|
-
with multi-package repos, or single-package repos to help you version and publish your code. You can
|
|
5
|
-
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
|
|
6
|
-
|
|
7
|
-
We have a quick list of common questions to get you started engaging with this project in
|
|
8
|
-
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
|
package/.changeset/config.json
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://unpkg.com/@changesets/config@2.3.1/schema.json",
|
|
3
|
-
"changelog": "@changesets/cli/changelog",
|
|
4
|
-
"commit": false,
|
|
5
|
-
"fixed": [],
|
|
6
|
-
"linked": [],
|
|
7
|
-
"access": "restricted",
|
|
8
|
-
"baseBranch": "main",
|
|
9
|
-
"updateInternalDependencies": "patch",
|
|
10
|
-
"ignore": []
|
|
11
|
-
}
|