@greenlabs/ppx-spice 0.2.0 → 0.2.1-rc.1
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/README.md +9 -2
- package/docs/GUIDE.md +2 -2
- package/greenlabs-ppx-spice-0.2.1-rc.1.tgz +0 -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 +30 -30
- package/src/rescript/Spice_Codecs.res +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 0.2.1 (unreleased)
|
|
4
|
+
|
|
5
|
+
- a190663 Utilize Js.Json.Boolean(bool) instead oif Js.Json.True, False https://github.com/green-labs/ppx_spice/pull/58
|
|
6
|
+
- a190663 Add support of uncurried mode for interface(*.resi) https://github.com/green-labs/ppx_spice/pull/58
|
|
7
|
+
- Support the compiler v11-rc.5 https://github.com/green-labs/ppx_spice/pull/61
|
|
8
|
+
|
|
3
9
|
## 0.2.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -92,7 +92,7 @@ let t_decode = ...
|
|
|
92
92
|
|
|
93
93
|
let encoded = One->t_encode // Js.Json.string(`하나`)
|
|
94
94
|
|
|
95
|
-
let decoded = Js.Json.string(`second`)->t_decode //
|
|
95
|
+
let decoded = Js.Json.string(`second`)->t_decode // Ok(Two)
|
|
96
96
|
```
|
|
97
97
|
|
|
98
98
|
2. Record
|
|
@@ -117,7 +117,7 @@ let sampleRecord: t = {
|
|
|
117
117
|
|
|
118
118
|
let encoded = sampleRecord->Records.t_encode // sampleJson
|
|
119
119
|
|
|
120
|
-
let decoded = sampleJson->Records.t_decode //
|
|
120
|
+
let decoded = sampleJson->Records.t_decode // Ok(sampleRecord)
|
|
121
121
|
```
|
|
122
122
|
|
|
123
123
|
## Getting Started
|
|
@@ -126,6 +126,13 @@ Read our [Guide with examples](docs/GUIDE.md)
|
|
|
126
126
|
|
|
127
127
|
## Install
|
|
128
128
|
|
|
129
|
+
### Versions
|
|
130
|
+
|
|
131
|
+
|Compiler|Ppx_spice |
|
|
132
|
+
|--------|-----------|
|
|
133
|
+
|v10 |~<= v0.1.15|
|
|
134
|
+
|v11 |>= v0.2.0 |
|
|
135
|
+
|
|
129
136
|
```sh
|
|
130
137
|
yarn add -D @greenlabs/ppx-spice
|
|
131
138
|
```
|
package/docs/GUIDE.md
CHANGED
|
@@ -26,7 +26,7 @@ type user = {
|
|
|
26
26
|
nickname?: string,
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
let user:
|
|
29
|
+
let user: result<user, Spice.decodeError> = data->user_decode // user_decode is generated by ppx_spice
|
|
30
30
|
|
|
31
31
|
let json: Js.Json.t = user->Belt.Result.getExn->user_encode // user_encode is generated by ppx_spice
|
|
32
32
|
```
|
|
@@ -58,7 +58,7 @@ type user = {
|
|
|
58
58
|
language: language,
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
let user:
|
|
61
|
+
let user: result<user, Spice.decodeError> = json->user_decode
|
|
62
62
|
|
|
63
63
|
let json: Js.Json.t = user->Result.getExn->user_encode
|
|
64
64
|
```
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@greenlabs/ppx-spice",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1-rc.1",
|
|
4
4
|
"description": "ReScript PPX which generate JSON (de)serializer",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Greenlabs Dev <developer@greenlabs.co.kr>",
|
|
@@ -21,4 +21,4 @@
|
|
|
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
|
@@ -4,7 +4,7 @@ type decodeError = {
|
|
|
4
4
|
value: Js.Json.t,
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
type result<'a> =
|
|
7
|
+
type result<'a> = result<'a, decodeError>
|
|
8
8
|
type decoder<'a> = Js.Json.t => result<'a>
|
|
9
9
|
type encoder<'a> = 'a => Js.Json.t
|
|
10
10
|
type codec<'a> = (encoder<'a>, decoder<'a>)
|
|
@@ -14,14 +14,14 @@ let error = (~path=?, message, value) => {
|
|
|
14
14
|
| None => ""
|
|
15
15
|
| Some(s) => s
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
Error({path, message, value})
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
let stringToJson = (s): Js.Json.t => Js.Json.String(s)
|
|
21
21
|
let stringFromJson = j =>
|
|
22
22
|
switch (j: Js.Json.t) {
|
|
23
|
-
| Js.Json.String(s) =>
|
|
24
|
-
| _ =>
|
|
23
|
+
| Js.Json.String(s) => Ok(s)
|
|
24
|
+
| _ => Error({path: "", message: "Not a string", value: j})
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
let intToJson = (i): Js.Json.t => Js.Json.Number(float_of_int(i))
|
|
@@ -29,17 +29,17 @@ let intFromJson = j =>
|
|
|
29
29
|
switch (j: Js.Json.t) {
|
|
30
30
|
| Js.Json.Number(f) =>
|
|
31
31
|
float_of_int(Js.Math.floor(f)) == f
|
|
32
|
-
?
|
|
33
|
-
:
|
|
32
|
+
? Ok(Js.Math.floor(f))
|
|
33
|
+
: Error({path: "", message: "Not an integer", value: j})
|
|
34
34
|
|
|
35
|
-
| _ =>
|
|
35
|
+
| _ => Error({path: "", message: "Not a number", value: j})
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
let int64ToJson = (i): Js.Json.t => Js.Json.Number(Int64.float_of_bits(i))
|
|
39
39
|
|
|
40
40
|
let int64FromJson = j =>
|
|
41
41
|
switch (j: Js.Json.t) {
|
|
42
|
-
| Js.Json.Number(n) =>
|
|
42
|
+
| Js.Json.Number(n) => Ok(Int64.bits_of_float(n))
|
|
43
43
|
| _ => error("Not a number", j)
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -47,31 +47,31 @@ let int64ToJsonUnsafe = (i): Js.Json.t => Js.Json.number(Int64.to_float(i))
|
|
|
47
47
|
|
|
48
48
|
let int64FromJsonUnsafe = j =>
|
|
49
49
|
switch (j: Js.Json.t) {
|
|
50
|
-
| Js.Json.Number(n) =>
|
|
50
|
+
| Js.Json.Number(n) => Ok(Int64.of_float(n))
|
|
51
51
|
| _ => error("Not a number", j)
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
let floatToJson = (v): Js.Json.t => Js.Json.Number(v)
|
|
55
55
|
let floatFromJson = j =>
|
|
56
56
|
switch (j: Js.Json.t) {
|
|
57
|
-
| Js.Json.Number(f) =>
|
|
58
|
-
| _ =>
|
|
57
|
+
| Js.Json.Number(f) => Ok(f)
|
|
58
|
+
| _ => Error({path: "", message: "Not a number", value: j})
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
let boolToJson = (v): Js.Json.t =>
|
|
62
62
|
switch v {
|
|
63
|
-
| true => Js.Json.
|
|
64
|
-
| false => Js.Json.
|
|
63
|
+
| true => Js.Json.Boolean(true)
|
|
64
|
+
| false => Js.Json.Boolean(false)
|
|
65
65
|
}
|
|
66
66
|
let boolFromJson = j =>
|
|
67
67
|
switch (j: Js.Json.t) {
|
|
68
|
-
| Js.Json.
|
|
69
|
-
| Js.Json.
|
|
70
|
-
| _ =>
|
|
68
|
+
| Js.Json.Boolean(true) => Ok(true)
|
|
69
|
+
| Js.Json.Boolean(false) => Ok(false)
|
|
70
|
+
| _ => Error({path: "", message: "Not a boolean", value: j})
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
let unitToJson = (): Js.Json.t => Js.Json.Number(0.0)
|
|
74
|
-
let unitFromJson = _ =>
|
|
74
|
+
let unitFromJson = _ => Ok()
|
|
75
75
|
|
|
76
76
|
let arrayToJson = (encoder, arr): Js.Json.t => Js.Json.Array(Js.Array.map(encoder, arr))
|
|
77
77
|
|
|
@@ -79,17 +79,17 @@ let arrayFromJson = (decoder, json) =>
|
|
|
79
79
|
switch (json: Js.Json.t) {
|
|
80
80
|
| Js.Json.Array(arr) => Js.Array.reducei((acc, jsonI, i) =>
|
|
81
81
|
switch (acc, decoder(jsonI)) {
|
|
82
|
-
| (
|
|
82
|
+
| (Error(_), _) => acc
|
|
83
83
|
|
|
84
|
-
| (_,
|
|
85
|
-
|
|
84
|
+
| (_, Error({path} as error)) =>
|
|
85
|
+
Error({...error, path: "[" ++ (string_of_int(i) ++ ("]" ++ path))})
|
|
86
86
|
|
|
87
|
-
| (
|
|
88
|
-
|
|
87
|
+
| (Ok(prev), Ok(newVal)) =>
|
|
88
|
+
Ok(Js.Array.concat([newVal], prev))
|
|
89
89
|
}
|
|
90
|
-
,
|
|
90
|
+
, Ok([]), arr)
|
|
91
91
|
|
|
92
|
-
| _ =>
|
|
92
|
+
| _ => Error({path: "", message: "Not an array", value: json})
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
let listToJson = (encoder, list) => arrayToJson(encoder, Belt.List.toArray(list))
|
|
@@ -111,14 +111,14 @@ let optionToJson = (encoder, opt): Js.Json.t =>
|
|
|
111
111
|
|
|
112
112
|
let optionFromJson = (decoder, json) =>
|
|
113
113
|
switch (json: Js.Json.t) {
|
|
114
|
-
| Js.Json.Null =>
|
|
114
|
+
| Js.Json.Null => Ok(None)
|
|
115
115
|
| _ => Belt.Result.map(decoder(json), v => Some(v))
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
let resultToJson = (okEncoder, errorEncoder, result): Js.Json.t => Js.Json.Array(
|
|
119
119
|
switch result {
|
|
120
|
-
|
|
|
121
|
-
|
|
|
120
|
+
| Ok(v) => [Js.Json.String("Ok"), okEncoder(v)]
|
|
121
|
+
| Error(e) => [Js.Json.String("Error"), errorEncoder(e)]
|
|
122
122
|
},
|
|
123
123
|
)
|
|
124
124
|
|
|
@@ -126,12 +126,12 @@ let resultFromJson = (okDecoder, errorDecoder, json) =>
|
|
|
126
126
|
switch (json: Js.Json.t) {
|
|
127
127
|
| Js.Json.Array([variantConstructorId, payload]) =>
|
|
128
128
|
switch variantConstructorId {
|
|
129
|
-
| Js.Json.String("Ok") => okDecoder(payload)->Belt.Result.map(v =>
|
|
129
|
+
| Js.Json.String("Ok") => okDecoder(payload)->Belt.Result.map(v => Ok(v))
|
|
130
130
|
|
|
131
131
|
| Js.Json.String("Error") =>
|
|
132
132
|
switch errorDecoder(payload) {
|
|
133
|
-
|
|
|
134
|
-
|
|
|
133
|
+
| Ok(v) => Ok(Error(v))
|
|
134
|
+
| Error(e) => Error(e)
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
| Js.Json.String(_) => error("Expected either \"Ok\" or \"Error\"", variantConstructorId)
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
let falseableEncode = (encoder, opt) =>
|
|
2
2
|
switch opt {
|
|
3
|
-
| None => Js.Json.
|
|
3
|
+
| None => Js.Json.Boolean(false)
|
|
4
4
|
| Some(v) => encoder(v)
|
|
5
5
|
}
|
|
6
6
|
let falseableDecode = (decoder, json) =>
|
|
7
7
|
switch json {
|
|
8
|
-
| Js.Json.
|
|
8
|
+
| Js.Json.Boolean(false) => Ok(None)
|
|
9
9
|
| _ => Belt.Result.map(decoder(json), v => Some(v))
|
|
10
10
|
}
|
|
11
11
|
let falseable = (falseableEncode, falseableDecode)
|
|
12
12
|
|
|
13
|
-
let magicDecode = j =>
|
|
13
|
+
let magicDecode = j => Ok(Obj.magic(j))
|
|
14
14
|
let magic = (Obj.magic, magicDecode)
|