Skip to content

Commit ae441e1

Browse files
committed
In order to support ReScript 11, I had to drop bs-json. The atdgen generated code was effectively built around it, rescript-json-combinators can't be made compatible.
But we weren't leveraging many of bs-json's features anyway; the vast majority of calls to bs-json were replaced with direct Js.Json API.
1 parent 31df634 commit ae441e1

13 files changed

+1091
-1637
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ yarn add rescript-atdgen-codec-runtime
1414
This package doesn't take care of running `atdgen` to derive code from type definitions. For that, you need to [`rescript-atdgen-generator`](https://github.com/TheSpyder/rescript-atdgen-generator) to generate the `.ml` and `.mli` files from `.atd` sources.
1515

1616
## A note about ReScript 11
17-
The `atd` code-generation project is an OCaml tool, and generates OCaml files. As such it does not support uncurried mode. The ahrefs team is open to a contribution that generates ReScript instead, but so long as this project only has one user that doesn't seem worth the effort. For more information and to add your voice to the discussion please follow the github issue (TODO).
17+
The `atd` code-generation project is an OCaml tool, and generates OCaml files. As such it does not support uncurried mode. The ahrefs team is open to a contribution that generates ReScript instead, but unless this project becomes popular that doesn't seem worth the effort. For more information and to add your voice to the discussion please follow the github issue (TODO).
1818

1919
## Usage
2020

__tests__/decode_test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let run_decode_test = (~name, ~read, ~data, ~expected) => {
44
open Expect
55
let decode = Atdgen_codec_runtime.Decode.decode(read)
66
let data' = decode(data)
7-
test(name, () => expect(data') |> toEqual(expected))
7+
test(name, () => expect(data')->toEqual(expected))
88
}
99

1010
@ocaml.doc("

__tests__/errors_test.res

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,91 @@ let wrap_exn = exp =>
66
let _ = exp()
77
"not called"
88
} catch {
9-
| Json_decode.DecodeError(str) => str
9+
| Atdgen_codec_runtime.Decode.DecodeError(str) => str
1010
}
1111

1212
let () = describe("exceptions", () => {
1313
test("unit", () => {
14-
let j = Json.parseOrRaise(`{}`)
15-
expect(wrap_exn(() => Atdgen_codec_runtime.Decode.unit(j))) |> toBe("Expected null, got {}")
14+
let j = Js.Json.parseExn(`{}`)
15+
expect(wrap_exn(() => Atdgen_codec_runtime.Decode.unit(j)))->toBe("Expected null, was {}")
1616
})
1717

1818
test("option_as_constr", () => {
19-
let j = Json.parseOrRaise(`{}`)
19+
let j = Js.Json.parseExn(`{}`)
2020
expect(
21-
wrap_exn(() =>
22-
{
23-
open Atdgen_codec_runtime.Decode
24-
option_as_constr(int)
25-
}(j)
21+
wrap_exn(
22+
() =>
23+
{
24+
open Atdgen_codec_runtime.Decode
25+
option_as_constr(int)
26+
}(j),
2627
),
27-
) |> toBe("All decoders given to oneOf failed. Here are all the errors:
28-
- Expected string, got {}
29-
- Expected array, got {}
30-
And the JSON being decoded: {}")
28+
)->toBe(`Both attempts to parse the value "{}" failed. Errors:
29+
1) Expected string, was {}
30+
2) Expected array, was {}`)
3131
})
3232

3333
test("enum", () => {
34-
let j = Json.parseOrRaise(`{}`)
34+
let j = Js.Json.parseExn(`{}`)
3535
expect(
36-
wrap_exn(() =>
37-
{
38-
open Atdgen_codec_runtime.Decode
39-
enum(list{})
40-
}(j)
36+
wrap_exn(
37+
() =>
38+
{
39+
open Atdgen_codec_runtime.Decode
40+
enum(list{})
41+
}(j),
4142
),
42-
) |> toBe("All decoders given to oneOf failed. Here are all the errors:
43-
- Expected string, got {}
44-
- Expected array, got {}
45-
And the JSON being decoded: {}")
43+
)->toBe(`Both attempts to parse the value "{}" failed. Errors:
44+
1) Expected string, was {}
45+
2) Expected array, was {}`)
4646
})
4747

4848
test("missing field in record", () => {
49-
let j = Json.parseOrRaise(`{"o": 44}`)
50-
expect(wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_ro, j))) |> toBe(
49+
let j = Js.Json.parseExn(`{"o": 44}`)
50+
expect(wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_ro, j)))->toBe(
5151
"Expected field 'c'",
5252
)
5353
})
5454

5555
test("optional field with default: wrong type throws exception", () => {
56-
let j = Json.parseOrRaise(`{"with_default": "not right"}`)
56+
let j = Js.Json.parseExn(`{"with_default": "not right"}`)
5757
expect(
5858
wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_optional_field, j)),
59-
) |> toBe(`with_default: Expected number, got "not right"`)
59+
)->toBe(`with_default: Expected int, was "not right"`)
6060
})
6161

6262
test("optional field: wrong type throws exception", () => {
63-
let j = Json.parseOrRaise(`{"no_default": "not right"}`)
63+
let j = Js.Json.parseExn(`{"no_default": "not right"}`)
6464
expect(
6565
wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_optional_field, j)),
66-
) |> toBe(`no_default: Expected number, got "not right"`)
66+
)->toBe(`no_default: Expected int, was "not right"`)
6767
})
6868

6969
test("error in variant", () => {
70-
let j = Json.parseOrRaise(`["A", "not right"]`)
70+
let j = Js.Json.parseExn(`["A", "not right"]`)
7171
expect(
7272
wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_v, j)),
73-
) |> toBe(`A: Expected number, got "not right"`)
73+
)->toBe(`A: Expected int, was "not right"`)
7474
})
7575

7676
test("deeply nested error (array element fails)", () => {
77-
let j = Json.parseOrRaise(`["A", [[1, "not right"], "Bool"]]`)
77+
let j = Js.Json.parseExn(`["A", [[1, "not right"], "Bool"]]`)
7878
expect(
7979
wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_deeply_nested, j)),
80-
) |> toBe(`A.0.1: Expected number, got "not right"`)
80+
)->toBe(`A.0.1: Expected int, was "not right"`)
8181
})
8282

8383
test("deeply nested error (tuple element fails)", () => {
84-
let j = Json.parseOrRaise(`["A", [[1, 2], "Boolean"]]`)
84+
let j = Js.Json.parseExn(`["A", [[1, 2], "Boolean"]]`)
8585
expect(
8686
wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_deeply_nested, j)),
87-
) |> toBe(`A.1.Boolean: unknown constructor "Boolean"`)
87+
)->toBe(`A.1.Boolean: unknown constructor "Boolean"`)
8888
})
8989

9090
test("deeply nested error (rec_list element fails deep enough)", () => {
91-
let j = Json.parseOrRaise(`["A", [[1, 2], ["List", ["Bool", "Fail"]]]]`)
91+
let j = Js.Json.parseExn(`["A", [[1, 2], ["List", ["Bool", "Fail"]]]]`)
9292
expect(
9393
wrap_exn(() => Atdgen_codec_runtime.Decode.decode(Test_bs.read_deeply_nested, j)),
94-
) |> toBe(`A.1.List.1.Fail: unknown constructor "Fail"`)
94+
)->toBe(`A.1.List.1.Fail: unknown constructor "Fail"`)
9595
})
9696
})

__tests__/roundtrip_test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let run_test = (~name, ~write, ~read, ~data) => {
88
let decode = Atdgen_codec_runtime.Decode.decode(read)
99
let json = encode(data)
1010
let data' = decode(json)
11-
test(name, () => expect(data) |> toEqual(data'))
11+
test(name, () => expect(data)->toEqual(data'))
1212
}
1313

1414
let () = describe("roundtrip tests", () => {

bsconfig.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"name": "rescript-atdgen-codec-runtime",
33
"namespace": false,
4-
"version": "2.0.1",
5-
"bsc-flags": [
6-
"-bs-no-version-header",
7-
"-bs-super-errors"
8-
],
4+
"version": "0.1.0",
95
"sources": [
106
{
117
"dir": "src"
@@ -20,13 +16,11 @@
2016
"in-source": false
2117
},
2218
"suffix": ".bs.js",
23-
"bs-dependencies": [
24-
"@glennsl/bs-json"
25-
],
2619
"bs-dev-dependencies": [
27-
"@glennsl/bs-jest"
20+
"@glennsl/rescript-jest"
2821
],
2922
"warnings": {
30-
"error": "+101"
31-
}
32-
}
23+
"number": "A-4-40-41-42-43+101-102"
24+
},
25+
"uncurried": false
26+
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
"generate-tests": "cd __tests__ && npx atdgen -t test.atd && npx atdgen -bs test.atd"
2424
},
2525
"devDependencies": {
26-
"@glennsl/bs-jest": "^0.7.0",
27-
"rescript": "^9.1.4",
26+
"@glennsl/rescript-jest": "^0.10.0",
27+
"rescript": "^11.0.1",
2828
"rescript-atdgen-generator": "^0.1.1"
2929
},
30-
"dependencies": {
31-
"@glennsl/bs-json": "^5.0.0"
32-
}
30+
"contributors": [
31+
"Ahrefs <[email protected]>"
32+
]
3333
}

0 commit comments

Comments
 (0)