From cd979754c7330c70664158068c2a04fe08456632 Mon Sep 17 00:00:00 2001 From: Javier Chavarri Date: Tue, 21 Jan 2025 10:01:48 +0000 Subject: [PATCH 1/6] use Json_error exn --- src/__tests__/errors_test.ml | 2 +- src/atdgen_codec_decode.ml | 67 ++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/__tests__/errors_test.ml b/src/__tests__/errors_test.ml index 61270d1..0fd1791 100644 --- a/src/__tests__/errors_test.ml +++ b/src/__tests__/errors_test.ml @@ -1,7 +1,7 @@ open Jest open Expect -let wrap_exn exp = try let _ = exp () in "not called" with Json.Decode.DecodeError str -> str +let wrap_exn exp = try let _ = exp () in "not called" with Json.Decode.DecodeError (Json_error str) -> str let () = describe "exceptions" (fun () -> diff --git a/src/atdgen_codec_decode.ml b/src/atdgen_codec_decode.ml index 31801ef..fc399bd 100644 --- a/src/atdgen_codec_decode.ml +++ b/src/atdgen_codec_decode.ml @@ -12,17 +12,17 @@ let decode f json = try f json with DecodeErrorPath (path, msg) -> let path = String.concat "." path in - raise (DecodeError {j|$path: $msg|j}) + raise (DecodeError (Json_error {j|$path: $msg|j})) let with_segment segment f json = try f json with - | DecodeError msg -> raise (DecodeErrorPath ([ segment ], msg)) + | DecodeError (Json_error msg) -> raise (DecodeErrorPath ([ segment ], msg)) | DecodeErrorPath (path, msg) -> raise (DecodeErrorPath (segment :: path, msg)) let unit j = if Js.Json.test j Null then () - else raise (DecodeError ("Expected null, got " ^ Js.Json.stringify j)) + else raise (DecodeError (Json_error ("Expected null, got " ^ Js.Json.stringify j))) let int32 j = Int32.of_string (string j) @@ -36,13 +36,13 @@ let array decode json = for i = 0 to length - 1 do let value = try with_segment (string_of_int i) decode (Array.unsafe_get source i) - with DecodeError msg -> - raise @@ DecodeError (msg ^ "\n\tin array at index " ^ string_of_int i) + with DecodeError (Json_error msg) -> + raise @@ DecodeError (Json_error (msg ^ "\n\tin array at index " ^ string_of_int i)) in Array.unsafe_set target i value done; target) - else raise @@ DecodeError ("Expected array, got " ^ Js.Json.stringify json) + else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) let list decode json = json |> array decode |> Array.to_list @@ -54,13 +54,13 @@ let pair decodeA decodeB json = try ( with_segment "0" decodeA (Array.unsafe_get source 0), with_segment "1" decodeB (Array.unsafe_get source 1) ) - with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin pair/tuple2") + with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin pair/tuple2")) else let length_str = string_of_int length in raise @@ DecodeError - {j|Expected array of length 2, got array of length $length_str|j} - else raise @@ DecodeError ("Expected array, got " ^ Js.Json.stringify json) + (Json_error {j|Expected array of length 2, got array of length $length_str|j}) + else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) let tuple2 = pair @@ -73,13 +73,13 @@ let tuple3 decodeA decodeB decodeC json = ( with_segment "0" decodeA (Array.unsafe_get source 0), with_segment "1" decodeB (Array.unsafe_get source 1), with_segment "2" decodeC (Array.unsafe_get source 2) ) - with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin tuple3") + with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin tuple3")) else let length_str = string_of_int length in raise @@ DecodeError - {j|Expected array of length 3, got array of length $length_str|j} - else raise @@ DecodeError ("Expected array, got " ^ Js.Json.stringify json) + (Json_error {j|Expected array of length 3, got array of length $length_str|j}) + else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) let tuple4 decodeA decodeB decodeC decodeD json = if Js.Array.isArray json then @@ -91,13 +91,13 @@ let tuple4 decodeA decodeB decodeC decodeD json = with_segment "2" decodeB (Array.unsafe_get source 1), with_segment "3" decodeC (Array.unsafe_get source 2), with_segment "4" decodeD (Array.unsafe_get source 3) ) - with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin tuple4") + with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin tuple4")) else let length_str = string_of_int length in raise @@ DecodeError - {j|Expected array of length 4, got array of length $length_str|j} - else raise @@ DecodeError ("Expected array, got " ^ Js.Json.stringify json) + (Json_error {j|Expected array of length 4, got array of length $length_str|j}) + else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) let dict decode json = if Js.Json.test json Object then ( @@ -109,12 +109,12 @@ let dict decode json = let key = Array.unsafe_get keys i in let value = try with_segment key decode (Js.Dict.unsafeGet source key) - with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin dict") + with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin dict")) in Js.Dict.set target key value done; target) - else raise @@ DecodeError ("Expected object, got " ^ Js.Json.stringify json) + else raise @@ DecodeError (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let field key decode json = if Js.Json.test json Object then @@ -122,10 +122,10 @@ let field key decode json = match Js.Dict.get dict key with | Some value -> ( try with_segment key decode value - with DecodeError msg -> - raise @@ DecodeError (msg ^ "\n\tat field '" ^ key ^ "'")) - | None -> raise @@ DecodeError {j|Expected field '$(key)'|j} - else raise @@ DecodeError ("Expected object, got " ^ Js.Json.stringify json) + with DecodeError (Json_error msg) -> + raise @@ DecodeError (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) + | None -> raise @@ DecodeError (Json_error {j|Expected field '$(key)'|j}) + else raise @@ DecodeError (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let obj_array f json = dict f json |> Js.Dict.entries @@ -144,9 +144,9 @@ let fieldOptional key decode json = | Some value when (Js.Json.test value Null) -> None | Some value -> ( try Some (with_segment key decode value) - with DecodeError msg -> - raise @@ DecodeError (msg ^ "\n\tat field '" ^ key ^ "'")) - else raise @@ DecodeError ("Expected object, got " ^ Js.Json.stringify json) + with DecodeError (Json_error msg) -> + raise @@ DecodeError (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) + else raise @@ DecodeError (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let fieldDefault s default f = fieldOptional s f |> map (function None -> default | Some s -> s) @@ -157,13 +157,12 @@ let tuple1 f x = let length = Js.Array.length source in if length = 1 then try with_segment "0" f (Array.unsafe_get source 0) - with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin tuple1") + with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin tuple1")) else let length_str = string_of_int length in raise - @@ DecodeError - {j|Expected array of length 1, got array of length $length_str|j} - else raise @@ DecodeError ("Expected array, got " ^ Js.Json.stringify x) + @@ DecodeError (Json_error {j|Expected array of length 1, got array of length $length_str|j}) + else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify x)) let enum l json = let constr0 j = @@ -180,20 +179,20 @@ let enum l json = (fun () -> match List.assoc s l with | exception Not_found -> - raise @@ DecodeError {j|unknown constructor "$s"|j} + raise @@ DecodeError (Json_error {j|unknown constructor "$s"|j}) | `Single a -> a | `Decode _ -> - raise @@ DecodeError {j|constructor "$s" expects arguments|j}) + raise @@ DecodeError (Json_error {j|constructor "$s" expects arguments|j})) () | `Constr (s, args) -> with_segment s (fun () -> match List.assoc s l with | exception Not_found -> - raise @@ DecodeError {j|unknown constructor "$s"|j} + raise @@ DecodeError (Json_error {j|unknown constructor "$s"|j}) | `Single _ -> raise - @@ DecodeError {j|constructor "$s" doesn't expect arguments|j} + @@ DecodeError (Json_error {j|constructor "$s" doesn't expect arguments|j}) | `Decode d -> decode' d args) () @@ -201,11 +200,11 @@ let option_as_constr f = either (fun x -> if string x = "None" then None - else raise (DecodeError ("Expected None, got " ^ Js.Json.stringify x))) + else raise (DecodeError (Json_error ("Expected None, got " ^ Js.Json.stringify x)))) (fun x -> match pair string f x with | "Some", v -> Some v - | _ -> raise (DecodeError ("Expected Some _, got " ^ Js.Json.stringify x))) + | _ -> raise (DecodeError (Json_error ("Expected Some _, got " ^ Js.Json.stringify x)))) let adapter (normalize : Js.Json.t -> Js.Json.t) (reader : 'a t) json = reader (normalize json) From 4f211606a010a2bb12b676f296b9b4c7b773f1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Ch=C3=A1varri?= Date: Sat, 15 Feb 2025 16:46:27 +0100 Subject: [PATCH 2/6] use latest melange-json --- CHANGES.md | 4 ++ README.md | 6 +-- example/src/cli.ml | 4 +- example/src/dune | 4 +- src/__tests__/decode_test.ml | 4 +- src/__tests__/dune | 4 +- src/__tests__/errors_test.ml | 52 ++++++++++---------- src/__tests__/roundtrip_test.ml | 84 ++++++++++++++++----------------- src/__tests__/test.atd | 2 +- src/atdgen_codec_decode.ml | 72 ++++++++++++++-------------- src/atdgen_codec_encode.ml | 16 ++++--- src/atdgen_json_adapter.ml | 2 +- 12 files changed, 131 insertions(+), 123 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 16b0fda..2d8485d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## Next + +- Update to latest melange-json with unified runtime, [#53](https://github.com/ahrefs/melange-atdgen-codec-runtime/pull/53) + ## 3.0.0 (2024-02-06) - Expose `DecodeErrorPath`, [#51](https://github.com/ahrefs/melange-atdgen-codec-runtime/pull/51) diff --git a/README.md b/README.md index 7967f05..0302117 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,10 @@ To generate `ml` files from `atd` ones, add a couple of rules to your `dune` fil ```clojure (rule - (targets test_bs.ml test_bs.mli) + (targets test_mel.ml test_mel.mli) (deps test.atd) (action - (run atdgen -bs %{deps}))) + (run atdgen -mel %{deps}))) (rule (targets test_t.ml test_t.mli) @@ -67,7 +67,7 @@ let get = (url, decode) => let v: Meetup_t.events = get( "http://localhost:8000/events", - Atdgen_codec_runtime.Decode.decode(Meetup_bs.read_events), + Atdgen_codec_runtime.Decode.decode(Meetup_mel.read_events), ); ``` diff --git a/example/src/cli.ml b/example/src/cli.ml index 2bbbd9d..ae9a30d 100644 --- a/example/src/cli.ml +++ b/example/src/cli.ml @@ -18,12 +18,12 @@ let read_events () = (* parse the json *) let json = Js.Json.parseExn file_content in (* turn it into a proper record *) - let events: Meetup_t.events = Atdgen_codec_runtime.Decode.decode Meetup_bs.read_events json in + let events: Meetup_t.events = Atdgen_codec_runtime.Decode.decode Meetup_mel.read_events json in events let write_events events = (* turn a list of records into json *) - let json = Atdgen_codec_runtime.Encode.encode Meetup_bs.write_events events in + let json = Atdgen_codec_runtime.Encode.encode Meetup_mel.write_events events in (* convert the json to string *) let file_content = Js.Json.stringifyWithSpace json 2 in (* write the json in our file *) diff --git a/example/src/dune b/example/src/dune index 4e361f5..9667de1 100644 --- a/example/src/dune +++ b/example/src/dune @@ -5,10 +5,10 @@ (promote (until-clean))) (rule - (targets meetup_bs.ml meetup_bs.mli) + (targets meetup_mel.ml meetup_mel.mli) (deps meetup.atd) (action - (run atdgen -bs %{deps}))) + (run atdgen -mel %{deps}))) (rule (targets meetup_t.ml meetup_t.mli) diff --git a/src/__tests__/decode_test.ml b/src/__tests__/decode_test.ml index 93027df..419bc32 100644 --- a/src/__tests__/decode_test.ml +++ b/src/__tests__/decode_test.ml @@ -14,12 +14,12 @@ let run_decode_test ~name ~read ~data ~expected = let () = describe "JSON decoding tests" (fun () -> run_decode_test ~name:"nullable field decoding a null" - ~read:Test_bs.read_optional_field + ~read:Test_mel.read_optional_field ~expected: { with_default = 9; no_default = None; no_default_nullable = None } ~data:[%raw {|{ no_default_nullable: null }|}]; run_decode_test ~name:"optional field decoding a null" - ~read:Test_bs.read_optional_field + ~read:Test_mel.read_optional_field ~expected: { with_default = 9; no_default = None; no_default_nullable = None } ~data:[%raw {|{ no_default: null}|}]) diff --git a/src/__tests__/dune b/src/__tests__/dune index 046a2e4..4ebc7c1 100644 --- a/src/__tests__/dune +++ b/src/__tests__/dune @@ -6,10 +6,10 @@ (pps melange.ppx))) (rule - (targets test_bs.ml test_bs.mli) + (targets test_mel.ml test_mel.mli) (deps test.atd) (action - (run atdgen -bs %{deps}))) + (run atdgen -mel %{deps}))) (rule (targets test_t.ml test_t.mli) diff --git a/src/__tests__/errors_test.ml b/src/__tests__/errors_test.ml index 0fd1791..ae85947 100644 --- a/src/__tests__/errors_test.ml +++ b/src/__tests__/errors_test.ml @@ -1,67 +1,67 @@ open Jest open Expect -let wrap_exn exp = try let _ = exp () in "not called" with Json.Decode.DecodeError (Json_error str) -> str +let wrap_exn exp = try let _ = exp () in "not called" with Json.Of_json_error (Json_error str) -> str let () = describe "exceptions" (fun () -> test "unit" (fun () -> - let j = Json.parseOrRaise {|{}|} in + let j = Json.of_string {|{}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.unit j)) |> toBe ("Expected null, got {}")); test "option_as_constr" (fun () -> - let j = Json.parseOrRaise {|{}|} in + let j = Json.of_string {|{}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.(option_as_constr int)j)) |> toBe ( "All decoders given to oneOf failed. Here are all the errors: \n\ - - Expected string, got {}\n\ - - Expected array, got {}\n\ + - expected a string but got {}\n\ + - expected an array but got {}\n\ And the JSON being decoded: {}")); test "enum" (fun () -> - let j = Json.parseOrRaise {|{}|} in + let j = Json.of_string {|{}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.(enum []) j)) |> toBe( "All decoders given to oneOf failed. Here are all the errors: \n\ - - Expected string, got {}\n\ - - Expected array, got {}\n\ + - expected a string but got {}\n\ + - expected an array but got {}\n\ And the JSON being decoded: {}")); test "missing field in record" (fun () -> - let j = Json.parseOrRaise {|{"o": 44}|} in - expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_bs.read_ro j)) + let j = Json.of_string {|{"o": 44}|} in + expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_ro j)) |> toBe("Expected field 'c'")); test "optional field with default: wrong type throws exception" (fun () -> - let j = Json.parseOrRaise {|{"with_default": "not right"}|} in - expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_bs.read_optional_field j)) - |> toBe({|with_default: Expected number, got "not right"|})); + let j = Json.of_string {|{"with_default": "not right"}|} in + expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_optional_field j)) + |> toBe({|with_default: expected an integer but got "not right"|})); test "optional field: wrong type throws exception" (fun () -> - let j = Json.parseOrRaise {|{"no_default": "not right"}|} in - expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_bs.read_optional_field j)) - |> toBe({|no_default: Expected number, got "not right"|})); + let j = Json.of_string {|{"no_default": "not right"}|} in + expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_optional_field j)) + |> toBe({|no_default: expected an integer but got "not right"|})); test "error in variant" (fun () -> - let j = Json.parseOrRaise {|["A", "not right"]|} in - expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_bs.read_v j)) - |> toBe({|A: Expected number, got "not right"|})); + let j = Json.of_string {|["A", "not right"]|} in + expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_v j)) + |> toBe({|A: expected an integer but got "not right"|})); test "deeply nested error (array element fails)" (fun () -> - let j = Json.parseOrRaise {|["A", [[1, "not right"], "Bool"]]|} in - expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_bs.read_deeply_nested j)) - |> toBe({|A.0.1: Expected number, got "not right"|})); + let j = Json.of_string {|["A", [[1, "not right"], "Bool"]]|} in + expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_deeply_nested j)) + |> toBe({|A.0.1: expected an integer but got "not right"|})); test "deeply nested error (tuple element fails)" (fun () -> - let j = Json.parseOrRaise {|["A", [[1, 2], "Boolean"]]|} in - expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_bs.read_deeply_nested j)) + let j = Json.of_string {|["A", [[1, 2], "Boolean"]]|} in + expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_deeply_nested j)) |> toBe({|A.1.Boolean: unknown constructor "Boolean"|})); test "deeply nested error (rec_list element fails deep enough)" (fun () -> - let j = Json.parseOrRaise {|["A", [[1, 2], ["List", ["Bool", "Fail"]]]]|} in - expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_bs.read_deeply_nested j)) + let j = Json.of_string {|["A", [[1, 2], ["List", ["Bool", "Fail"]]]]|} in + expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_deeply_nested j)) |> toBe({|A.1.List.1.Fail: unknown constructor "Fail"|})); diff --git a/src/__tests__/roundtrip_test.ml b/src/__tests__/roundtrip_test.ml index 7dcb002..8e9bc0e 100644 --- a/src/__tests__/roundtrip_test.ml +++ b/src/__tests__/roundtrip_test.ml @@ -17,58 +17,58 @@ let () = describe "roundtrip tests" (fun () -> run_test ~name:"record" - ~write:Test_bs.write_r - ~read:Test_bs.read_r + ~write:Test_mel.write_r + ~read:Test_mel.read_r ~data:{Test_t.a = 1; b = "string";}; run_test ~name:"record optional absent" - ~write:Test_bs.write_ro - ~read:Test_bs.read_ro + ~write:Test_mel.write_ro + ~read:Test_mel.read_ro ~data:{Test_t.c = "s"; o = None;}; run_test ~name:"record optional present" - ~write:Test_bs.write_ro - ~read:Test_bs.read_ro + ~write:Test_mel.write_ro + ~read:Test_mel.read_ro ~data:{Test_t.c = "s"; o = Some 3L;}; run_test ~name:"variant list" - ~write:Test_bs.write_vl - ~read:Test_bs.read_vl + ~write:Test_mel.write_vl + ~read:Test_mel.read_vl ~data:[Test_t.A 1; B "s"]; run_test ~name:"variant poly list" - ~write:Test_bs.write_vpl - ~read:Test_bs.read_vpl + ~write:Test_mel.write_vpl + ~read:Test_mel.read_vpl ~data:[`A 1; `B "s"]; run_test ~name:"tuple" - ~write:Test_bs.write_t - ~read:Test_bs.read_t + ~write:Test_mel.write_t + ~read:Test_mel.read_t ~data:(1, "s", 1.1); run_test ~name:"int nullable absent" - ~write:Test_bs.write_n - ~read:Test_bs.read_n + ~write:Test_mel.write_n + ~read:Test_mel.read_n ~data:None; run_test ~name:"int nullable present" - ~write:Test_bs.write_n - ~read:Test_bs.read_n + ~write:Test_mel.write_n + ~read:Test_mel.read_n ~data:(Some 1); run_test ~name:"int64" - ~write:Test_bs.write_myInt - ~read:Test_bs.read_myInt + ~write:Test_mel.write_myInt + ~read:Test_mel.read_myInt ~data:3L; run_test ~name:"recurse" - ~write:Test_bs.write_recurse - ~read:Test_bs.read_recurse + ~write:Test_mel.write_recurse + ~read:Test_mel.read_recurse ~data:{Test_t.recurse_items = [{ recurse_items = []}]}; run_test ~name:"mutual recurse" - ~write:Test_bs.write_mutual_recurse1 - ~read:Test_bs.read_mutual_recurse1 + ~write:Test_mel.write_mutual_recurse1 + ~read:Test_mel.read_mutual_recurse1 ~data:( let rec mutual_recurse1 = { Test_t.mutual_recurse2; } and mutual_recurse2 = [{ Test_t.mutual_recurse1 = [] }] @@ -76,52 +76,52 @@ let () = ); run_test ~name:"rec list" - ~write:Test_bs.write_rec_list - ~read:Test_bs.read_rec_list + ~write:Test_mel.write_rec_list + ~read:Test_mel.read_rec_list ~data:(`List [`Bool;`Bool;`List [`Bool]; `List []]); run_test ~name:"adapter variant 1" - ~write:Test_bs.write_adapted - ~read:Test_bs.read_adapted + ~write:Test_mel.write_adapted + ~read:Test_mel.read_adapted ~data:Test_t.(`A {thing = "thing"; other_thing = false;}); run_test ~name:"adapter variant 2" - ~write:Test_bs.write_adapted - ~read:Test_bs.read_adapted + ~write:Test_mel.write_adapted + ~read:Test_mel.read_adapted ~data:Test_t.(`B {thing = 1;}); run_test ~name:"adapter kind field - variant 1" - ~write:Test_bs.write_adapted_kind - ~read:Test_bs.read_adapted_kind + ~write:Test_mel.write_adapted_kind + ~read:Test_mel.read_adapted_kind ~data:Test_t.(`A {thing = "thing"; other_thing = false;}); run_test ~name:"adapter kind field - variant 2" - ~write:Test_bs.write_adapted_kind - ~read:Test_bs.read_adapted_kind + ~write:Test_mel.write_adapted_kind + ~read:Test_mel.read_adapted_kind ~data:Test_t.(`B {thing = 1;}); run_test ~name:"int array" - ~write:Test_bs.write_an_array - ~read:Test_bs.read_an_array + ~write:Test_mel.write_an_array + ~read:Test_mel.read_an_array ~data:[| 1;2;3;4;5 |]; run_test ~name:"record with optional fields" - ~write:Test_bs.write_optional_field - ~read:Test_bs.read_optional_field + ~write:Test_mel.write_optional_field + ~read:Test_mel.read_optional_field ~data:{with_default = 1; no_default = None; no_default_nullable = Some 11}; run_test ~name:"adapter scalar" - ~write:Test_bs.write_adapted_scalar - ~read:Test_bs.read_adapted_scalar + ~write:Test_mel.write_adapted_scalar + ~read:Test_mel.read_adapted_scalar ~data:(`A 1); run_test ~name:"adapter scalar - variant 2" - ~write:Test_bs.write_adapted_scalar - ~read:Test_bs.read_adapted_scalar + ~write:Test_mel.write_adapted_scalar + ~read:Test_mel.read_adapted_scalar ~data:(`B "thing"); run_test ~name:"adapter list" - ~write:Test_bs.write_adapted_list - ~read:Test_bs.read_adapted_list + ~write:Test_mel.write_adapted_list + ~read:Test_mel.read_adapted_list ~data:(`A [1]); ) diff --git a/src/__tests__/test.atd b/src/__tests__/test.atd index 425804c..c15b80c 100644 --- a/src/__tests__/test.atd +++ b/src/__tests__/test.atd @@ -1,6 +1,6 @@ (* atdgen -t test.atd - atdgen -bs test.atd -open Test_t + atdgen -mel test.atd -open Test_t *) type myInt = int diff --git a/src/atdgen_codec_decode.ml b/src/atdgen_codec_decode.ml index fc399bd..246f64d 100644 --- a/src/atdgen_codec_decode.ml +++ b/src/atdgen_codec_decode.ml @@ -1,8 +1,8 @@ -include Json.Decode +include Json.Of_json exception DecodeErrorPath of string list * string -type 'a t = 'a decoder +type 'a t = 'a Json.of_json let make f = f @@ -12,17 +12,19 @@ let decode f json = try f json with DecodeErrorPath (path, msg) -> let path = String.concat "." path in - raise (DecodeError (Json_error {j|$path: $msg|j})) + raise (Json.Of_json_error (Json_error {j|$path: $msg|j})) let with_segment segment f json = try f json with - | DecodeError (Json_error msg) -> raise (DecodeErrorPath ([ segment ], msg)) + | Json.Of_json_error (Json_error msg) -> raise (DecodeErrorPath ([ segment ], msg)) | DecodeErrorPath (path, msg) -> raise (DecodeErrorPath (segment :: path, msg)) let unit j = if Js.Json.test j Null then () - else raise (DecodeError (Json_error ("Expected null, got " ^ Js.Json.stringify j))) + else raise (Json.Of_json_error (Json_error ("Expected null, got " ^ Js.Json.stringify j))) + +let optional = try_or_none let int32 j = Int32.of_string (string j) @@ -36,13 +38,13 @@ let array decode json = for i = 0 to length - 1 do let value = try with_segment (string_of_int i) decode (Array.unsafe_get source i) - with DecodeError (Json_error msg) -> - raise @@ DecodeError (Json_error (msg ^ "\n\tin array at index " ^ string_of_int i)) + with Json.Of_json_error (Json_error msg) -> + raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin array at index " ^ string_of_int i)) in Array.unsafe_set target i value done; target) - else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) + else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let list decode json = json |> array decode |> Array.to_list @@ -54,13 +56,13 @@ let pair decodeA decodeB json = try ( with_segment "0" decodeA (Array.unsafe_get source 0), with_segment "1" decodeB (Array.unsafe_get source 1) ) - with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin pair/tuple2")) + with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin pair/tuple2")) else let length_str = string_of_int length in raise - @@ DecodeError + @@ Json.Of_json_error (Json_error {j|Expected array of length 2, got array of length $length_str|j}) - else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) + else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let tuple2 = pair @@ -73,13 +75,13 @@ let tuple3 decodeA decodeB decodeC json = ( with_segment "0" decodeA (Array.unsafe_get source 0), with_segment "1" decodeB (Array.unsafe_get source 1), with_segment "2" decodeC (Array.unsafe_get source 2) ) - with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin tuple3")) + with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin tuple3")) else let length_str = string_of_int length in raise - @@ DecodeError + @@ Json.Of_json_error (Json_error {j|Expected array of length 3, got array of length $length_str|j}) - else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) + else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let tuple4 decodeA decodeB decodeC decodeD json = if Js.Array.isArray json then @@ -91,13 +93,13 @@ let tuple4 decodeA decodeB decodeC decodeD json = with_segment "2" decodeB (Array.unsafe_get source 1), with_segment "3" decodeC (Array.unsafe_get source 2), with_segment "4" decodeD (Array.unsafe_get source 3) ) - with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin tuple4")) + with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin tuple4")) else let length_str = string_of_int length in raise - @@ DecodeError + @@ Json.Of_json_error (Json_error {j|Expected array of length 4, got array of length $length_str|j}) - else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify json)) + else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let dict decode json = if Js.Json.test json Object then ( @@ -109,12 +111,12 @@ let dict decode json = let key = Array.unsafe_get keys i in let value = try with_segment key decode (Js.Dict.unsafeGet source key) - with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin dict")) + with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin dict")) in Js.Dict.set target key value done; target) - else raise @@ DecodeError (Json_error ("Expected object, got " ^ Js.Json.stringify json)) + else raise @@ Json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let field key decode json = if Js.Json.test json Object then @@ -122,10 +124,10 @@ let field key decode json = match Js.Dict.get dict key with | Some value -> ( try with_segment key decode value - with DecodeError (Json_error msg) -> - raise @@ DecodeError (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) - | None -> raise @@ DecodeError (Json_error {j|Expected field '$(key)'|j}) - else raise @@ DecodeError (Json_error ("Expected object, got " ^ Js.Json.stringify json)) + with Json.Of_json_error (Json_error msg) -> + raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) + | None -> raise @@ Json.Of_json_error (Json_error {j|Expected field '$(key)'|j}) + else raise @@ Json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let obj_array f json = dict f json |> Js.Dict.entries @@ -144,9 +146,9 @@ let fieldOptional key decode json = | Some value when (Js.Json.test value Null) -> None | Some value -> ( try Some (with_segment key decode value) - with DecodeError (Json_error msg) -> - raise @@ DecodeError (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) - else raise @@ DecodeError (Json_error ("Expected object, got " ^ Js.Json.stringify json)) + with Json.Of_json_error (Json_error msg) -> + raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) + else raise @@ Json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let fieldDefault s default f = fieldOptional s f |> map (function None -> default | Some s -> s) @@ -157,12 +159,12 @@ let tuple1 f x = let length = Js.Array.length source in if length = 1 then try with_segment "0" f (Array.unsafe_get source 0) - with DecodeError (Json_error msg) -> raise @@ DecodeError (Json_error (msg ^ "\n\tin tuple1")) + with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin tuple1")) else let length_str = string_of_int length in raise - @@ DecodeError (Json_error {j|Expected array of length 1, got array of length $length_str|j}) - else raise @@ DecodeError (Json_error ("Expected array, got " ^ Js.Json.stringify x)) + @@ Json.Of_json_error (Json_error {j|Expected array of length 1, got array of length $length_str|j}) + else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify x)) let enum l json = let constr0 j = @@ -179,20 +181,20 @@ let enum l json = (fun () -> match List.assoc s l with | exception Not_found -> - raise @@ DecodeError (Json_error {j|unknown constructor "$s"|j}) + raise @@ Json.Of_json_error (Json_error {j|unknown constructor "$s"|j}) | `Single a -> a | `Decode _ -> - raise @@ DecodeError (Json_error {j|constructor "$s" expects arguments|j})) + raise @@ Json.Of_json_error (Json_error {j|constructor "$s" expects arguments|j})) () | `Constr (s, args) -> with_segment s (fun () -> match List.assoc s l with | exception Not_found -> - raise @@ DecodeError (Json_error {j|unknown constructor "$s"|j}) + raise @@ Json.Of_json_error (Json_error {j|unknown constructor "$s"|j}) | `Single _ -> raise - @@ DecodeError (Json_error {j|constructor "$s" doesn't expect arguments|j}) + @@ Json.Of_json_error (Json_error {j|constructor "$s" doesn't expect arguments|j}) | `Decode d -> decode' d args) () @@ -200,11 +202,11 @@ let option_as_constr f = either (fun x -> if string x = "None" then None - else raise (DecodeError (Json_error ("Expected None, got " ^ Js.Json.stringify x)))) + else raise (Json.Of_json_error (Json_error ("Expected None, got " ^ Js.Json.stringify x)))) (fun x -> match pair string f x with | "Some", v -> Some v - | _ -> raise (DecodeError (Json_error ("Expected Some _, got " ^ Js.Json.stringify x)))) + | _ -> raise (Json.Of_json_error (Json_error ("Expected Some _, got " ^ Js.Json.stringify x)))) let adapter (normalize : Js.Json.t -> Js.Json.t) (reader : 'a t) json = reader (normalize json) diff --git a/src/atdgen_codec_encode.ml b/src/atdgen_codec_encode.ml index e61b40c..785dbe7 100644 --- a/src/atdgen_codec_encode.ml +++ b/src/atdgen_codec_encode.ml @@ -1,12 +1,12 @@ -include Json.Encode +include Json.To_json -type 'a t = 'a encoder +type 'a t = 'a Json.to_json let make f = f let encode f x = f x -let unit () = null +let unit () = Js.Json.null let int32 s = string (Int32.to_string s) @@ -42,20 +42,22 @@ let obj fields = if s = default then acc else (name, encode s) :: acc | Some s, None -> (name, encode s) :: acc)) [] fields - |> object_ + |> Js.Dict.fromList |> json_dict -let tuple1 f x = jsonArray [| f x |] +let tuple1 f x = json_array [| f x |] let contramap f g b = g (f b) let constr0 = string -let constr1 s f x = pair string f (s, x) +let constr1 s f x = tuple2 string f (s, x) let option_as_constr f = function | None -> string "None" - | Some s -> pair string f ("Some", s) + | Some s -> tuple2 string f ("Some", s) let adapter (restore : Js.Json.t -> Js.Json.t) (writer : 'a t) x = let encoded = writer x in restore encoded + +let nullable = option \ No newline at end of file diff --git a/src/atdgen_json_adapter.ml b/src/atdgen_json_adapter.ml index e462d44..3ab3d6f 100644 --- a/src/atdgen_json_adapter.ml +++ b/src/atdgen_json_adapter.ml @@ -28,7 +28,7 @@ module Type_field = struct match o |> Js.Json.classify with | JSONObject obj -> Js.Dict.set obj type_field_name v; - Json.Encode.jsonDict obj + Json.To_json.json_dict obj | _ -> json) | _ -> json end From 61d27e3412d95eb4bb413dfd5310ea67e16fa78b Mon Sep 17 00:00:00 2001 From: Javier Chavarri Date: Fri, 21 Feb 2025 16:13:54 +0000 Subject: [PATCH 3/6] rename to melange_json --- CHANGES.md | 2 +- src/__tests__/errors_test.ml | 22 ++++++------ src/atdgen_codec_decode.ml | 70 ++++++++++++++++++------------------ src/atdgen_codec_encode.ml | 4 +-- src/atdgen_json_adapter.ml | 2 +- 5 files changed, 50 insertions(+), 50 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index fe26ca1..48d05eb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,6 @@ ## Next -- Update to latest melange-json with unified runtime, [#53](https://github.com/ahrefs/melange-atdgen-codec-runtime/pull/53) +- Update to latest `melange-json` with unified runtime, [#53](https://github.com/ahrefs/melange-atdgen-codec-runtime/pull/53) - Add `2.16.0` as lower bound version of `atdgen`, [#54](https://github.com/ahrefs/melange-atdgen-codec-runtime/pull/54) ## 3.0.0 (2024-02-06) diff --git a/src/__tests__/errors_test.ml b/src/__tests__/errors_test.ml index ae85947..217f2b2 100644 --- a/src/__tests__/errors_test.ml +++ b/src/__tests__/errors_test.ml @@ -1,18 +1,18 @@ open Jest open Expect -let wrap_exn exp = try let _ = exp () in "not called" with Json.Of_json_error (Json_error str) -> str +let wrap_exn exp = try let _ = exp () in "not called" with Melange_json.Of_json_error (Json_error str) -> str let () = describe "exceptions" (fun () -> test "unit" (fun () -> - let j = Json.of_string {|{}|} in + let j = Melange_json.of_string {|{}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.unit j)) |> toBe ("Expected null, got {}")); test "option_as_constr" (fun () -> - let j = Json.of_string {|{}|} in + let j = Melange_json.of_string {|{}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.(option_as_constr int)j)) |> toBe ( "All decoders given to oneOf failed. Here are all the errors: \n\ @@ -21,7 +21,7 @@ let () = And the JSON being decoded: {}")); test "enum" (fun () -> - let j = Json.of_string {|{}|} in + let j = Melange_json.of_string {|{}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.(enum []) j)) |> toBe( "All decoders given to oneOf failed. Here are all the errors: \n\ @@ -30,37 +30,37 @@ let () = And the JSON being decoded: {}")); test "missing field in record" (fun () -> - let j = Json.of_string {|{"o": 44}|} in + let j = Melange_json.of_string {|{"o": 44}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_ro j)) |> toBe("Expected field 'c'")); test "optional field with default: wrong type throws exception" (fun () -> - let j = Json.of_string {|{"with_default": "not right"}|} in + let j = Melange_json.of_string {|{"with_default": "not right"}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_optional_field j)) |> toBe({|with_default: expected an integer but got "not right"|})); test "optional field: wrong type throws exception" (fun () -> - let j = Json.of_string {|{"no_default": "not right"}|} in + let j = Melange_json.of_string {|{"no_default": "not right"}|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_optional_field j)) |> toBe({|no_default: expected an integer but got "not right"|})); test "error in variant" (fun () -> - let j = Json.of_string {|["A", "not right"]|} in + let j = Melange_json.of_string {|["A", "not right"]|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_v j)) |> toBe({|A: expected an integer but got "not right"|})); test "deeply nested error (array element fails)" (fun () -> - let j = Json.of_string {|["A", [[1, "not right"], "Bool"]]|} in + let j = Melange_json.of_string {|["A", [[1, "not right"], "Bool"]]|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_deeply_nested j)) |> toBe({|A.0.1: expected an integer but got "not right"|})); test "deeply nested error (tuple element fails)" (fun () -> - let j = Json.of_string {|["A", [[1, 2], "Boolean"]]|} in + let j = Melange_json.of_string {|["A", [[1, 2], "Boolean"]]|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_deeply_nested j)) |> toBe({|A.1.Boolean: unknown constructor "Boolean"|})); test "deeply nested error (rec_list element fails deep enough)" (fun () -> - let j = Json.of_string {|["A", [[1, 2], ["List", ["Bool", "Fail"]]]]|} in + let j = Melange_json.of_string {|["A", [[1, 2], ["List", ["Bool", "Fail"]]]]|} in expect (wrap_exn (fun () -> Atdgen_codec_runtime.Decode.decode Test_mel.read_deeply_nested j)) |> toBe({|A.1.List.1.Fail: unknown constructor "Fail"|})); diff --git a/src/atdgen_codec_decode.ml b/src/atdgen_codec_decode.ml index 246f64d..0f087d6 100644 --- a/src/atdgen_codec_decode.ml +++ b/src/atdgen_codec_decode.ml @@ -1,8 +1,8 @@ -include Json.Of_json +include Melange_json.Of_json exception DecodeErrorPath of string list * string -type 'a t = 'a Json.of_json +type 'a t = 'a Melange_json.of_json let make f = f @@ -12,17 +12,17 @@ let decode f json = try f json with DecodeErrorPath (path, msg) -> let path = String.concat "." path in - raise (Json.Of_json_error (Json_error {j|$path: $msg|j})) + raise (Melange_json.Of_json_error (Json_error {j|$path: $msg|j})) let with_segment segment f json = try f json with - | Json.Of_json_error (Json_error msg) -> raise (DecodeErrorPath ([ segment ], msg)) + | Melange_json.Of_json_error (Json_error msg) -> raise (DecodeErrorPath ([ segment ], msg)) | DecodeErrorPath (path, msg) -> raise (DecodeErrorPath (segment :: path, msg)) let unit j = if Js.Json.test j Null then () - else raise (Json.Of_json_error (Json_error ("Expected null, got " ^ Js.Json.stringify j))) + else raise (Melange_json.Of_json_error (Json_error ("Expected null, got " ^ Js.Json.stringify j))) let optional = try_or_none @@ -38,13 +38,13 @@ let array decode json = for i = 0 to length - 1 do let value = try with_segment (string_of_int i) decode (Array.unsafe_get source i) - with Json.Of_json_error (Json_error msg) -> - raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin array at index " ^ string_of_int i)) + with Melange_json.Of_json_error (Json_error msg) -> + raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tin array at index " ^ string_of_int i)) in Array.unsafe_set target i value done; target) - else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) + else raise @@ Melange_json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let list decode json = json |> array decode |> Array.to_list @@ -56,13 +56,13 @@ let pair decodeA decodeB json = try ( with_segment "0" decodeA (Array.unsafe_get source 0), with_segment "1" decodeB (Array.unsafe_get source 1) ) - with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin pair/tuple2")) + with Melange_json.Of_json_error (Json_error msg) -> raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tin pair/tuple2")) else let length_str = string_of_int length in raise - @@ Json.Of_json_error + @@ Melange_json.Of_json_error (Json_error {j|Expected array of length 2, got array of length $length_str|j}) - else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) + else raise @@ Melange_json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let tuple2 = pair @@ -75,13 +75,13 @@ let tuple3 decodeA decodeB decodeC json = ( with_segment "0" decodeA (Array.unsafe_get source 0), with_segment "1" decodeB (Array.unsafe_get source 1), with_segment "2" decodeC (Array.unsafe_get source 2) ) - with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin tuple3")) + with Melange_json.Of_json_error (Json_error msg) -> raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tin tuple3")) else let length_str = string_of_int length in raise - @@ Json.Of_json_error + @@ Melange_json.Of_json_error (Json_error {j|Expected array of length 3, got array of length $length_str|j}) - else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) + else raise @@ Melange_json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let tuple4 decodeA decodeB decodeC decodeD json = if Js.Array.isArray json then @@ -93,13 +93,13 @@ let tuple4 decodeA decodeB decodeC decodeD json = with_segment "2" decodeB (Array.unsafe_get source 1), with_segment "3" decodeC (Array.unsafe_get source 2), with_segment "4" decodeD (Array.unsafe_get source 3) ) - with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin tuple4")) + with Melange_json.Of_json_error (Json_error msg) -> raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tin tuple4")) else let length_str = string_of_int length in raise - @@ Json.Of_json_error + @@ Melange_json.Of_json_error (Json_error {j|Expected array of length 4, got array of length $length_str|j}) - else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) + else raise @@ Melange_json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify json)) let dict decode json = if Js.Json.test json Object then ( @@ -111,12 +111,12 @@ let dict decode json = let key = Array.unsafe_get keys i in let value = try with_segment key decode (Js.Dict.unsafeGet source key) - with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin dict")) + with Melange_json.Of_json_error (Json_error msg) -> raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tin dict")) in Js.Dict.set target key value done; target) - else raise @@ Json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) + else raise @@ Melange_json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let field key decode json = if Js.Json.test json Object then @@ -124,10 +124,10 @@ let field key decode json = match Js.Dict.get dict key with | Some value -> ( try with_segment key decode value - with Json.Of_json_error (Json_error msg) -> - raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) - | None -> raise @@ Json.Of_json_error (Json_error {j|Expected field '$(key)'|j}) - else raise @@ Json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) + with Melange_json.Of_json_error (Json_error msg) -> + raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) + | None -> raise @@ Melange_json.Of_json_error (Json_error {j|Expected field '$(key)'|j}) + else raise @@ Melange_json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let obj_array f json = dict f json |> Js.Dict.entries @@ -146,9 +146,9 @@ let fieldOptional key decode json = | Some value when (Js.Json.test value Null) -> None | Some value -> ( try Some (with_segment key decode value) - with Json.Of_json_error (Json_error msg) -> - raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) - else raise @@ Json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) + with Melange_json.Of_json_error (Json_error msg) -> + raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tat field '" ^ key ^ "'"))) + else raise @@ Melange_json.Of_json_error (Json_error ("Expected object, got " ^ Js.Json.stringify json)) let fieldDefault s default f = fieldOptional s f |> map (function None -> default | Some s -> s) @@ -159,12 +159,12 @@ let tuple1 f x = let length = Js.Array.length source in if length = 1 then try with_segment "0" f (Array.unsafe_get source 0) - with Json.Of_json_error (Json_error msg) -> raise @@ Json.Of_json_error (Json_error (msg ^ "\n\tin tuple1")) + with Melange_json.Of_json_error (Json_error msg) -> raise @@ Melange_json.Of_json_error (Json_error (msg ^ "\n\tin tuple1")) else let length_str = string_of_int length in raise - @@ Json.Of_json_error (Json_error {j|Expected array of length 1, got array of length $length_str|j}) - else raise @@ Json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify x)) + @@ Melange_json.Of_json_error (Json_error {j|Expected array of length 1, got array of length $length_str|j}) + else raise @@ Melange_json.Of_json_error (Json_error ("expected an array but got " ^ Js.Json.stringify x)) let enum l json = let constr0 j = @@ -181,20 +181,20 @@ let enum l json = (fun () -> match List.assoc s l with | exception Not_found -> - raise @@ Json.Of_json_error (Json_error {j|unknown constructor "$s"|j}) + raise @@ Melange_json.Of_json_error (Json_error {j|unknown constructor "$s"|j}) | `Single a -> a | `Decode _ -> - raise @@ Json.Of_json_error (Json_error {j|constructor "$s" expects arguments|j})) + raise @@ Melange_json.Of_json_error (Json_error {j|constructor "$s" expects arguments|j})) () | `Constr (s, args) -> with_segment s (fun () -> match List.assoc s l with | exception Not_found -> - raise @@ Json.Of_json_error (Json_error {j|unknown constructor "$s"|j}) + raise @@ Melange_json.Of_json_error (Json_error {j|unknown constructor "$s"|j}) | `Single _ -> raise - @@ Json.Of_json_error (Json_error {j|constructor "$s" doesn't expect arguments|j}) + @@ Melange_json.Of_json_error (Json_error {j|constructor "$s" doesn't expect arguments|j}) | `Decode d -> decode' d args) () @@ -202,11 +202,11 @@ let option_as_constr f = either (fun x -> if string x = "None" then None - else raise (Json.Of_json_error (Json_error ("Expected None, got " ^ Js.Json.stringify x)))) + else raise (Melange_json.Of_json_error (Json_error ("Expected None, got " ^ Js.Json.stringify x)))) (fun x -> match pair string f x with | "Some", v -> Some v - | _ -> raise (Json.Of_json_error (Json_error ("Expected Some _, got " ^ Js.Json.stringify x)))) + | _ -> raise (Melange_json.Of_json_error (Json_error ("Expected Some _, got " ^ Js.Json.stringify x)))) let adapter (normalize : Js.Json.t -> Js.Json.t) (reader : 'a t) json = reader (normalize json) diff --git a/src/atdgen_codec_encode.ml b/src/atdgen_codec_encode.ml index 785dbe7..fd937c6 100644 --- a/src/atdgen_codec_encode.ml +++ b/src/atdgen_codec_encode.ml @@ -1,6 +1,6 @@ -include Json.To_json +include Melange_json.To_json -type 'a t = 'a Json.to_json +type 'a t = 'a Melange_json.to_json let make f = f diff --git a/src/atdgen_json_adapter.ml b/src/atdgen_json_adapter.ml index 3ab3d6f..2f435b7 100644 --- a/src/atdgen_json_adapter.ml +++ b/src/atdgen_json_adapter.ml @@ -28,7 +28,7 @@ module Type_field = struct match o |> Js.Json.classify with | JSONObject obj -> Js.Dict.set obj type_field_name v; - Json.To_json.json_dict obj + Melange_json.To_json.json_dict obj | _ -> json) | _ -> json end From 54effadcf82124cade25825fdee3eef482d12636 Mon Sep 17 00:00:00 2001 From: Javier Chavarri Date: Fri, 14 Mar 2025 12:00:50 +0000 Subject: [PATCH 4/6] use melange-json v2 --- melange-atdgen-codec-runtime.opam | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/melange-atdgen-codec-runtime.opam b/melange-atdgen-codec-runtime.opam index d4162aa..1efbc25 100644 --- a/melange-atdgen-codec-runtime.opam +++ b/melange-atdgen-codec-runtime.opam @@ -14,7 +14,7 @@ depends: [ "melange" {>= "3.0.0"} "atd" "atdgen" {>= "2.16.0"} - "melange-json" + "melange-json" {>= "2.0.0"} "melange-jest" {with-test} "reason" {with-test} "opam-check-npm-deps" {with-test} From f6908c0083a850920e03a972993abd92554a1560 Mon Sep 17 00:00:00 2001 From: Javier Chavarri Date: Fri, 14 Mar 2025 12:04:13 +0000 Subject: [PATCH 5/6] bump setup-ocaml? --- .github/workflows/build-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index ee080d7..532de59 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -28,7 +28,7 @@ jobs: node-version: current - name: Use OCaml ${{ matrix.ocaml-compiler }} - uses: ocaml/setup-ocaml@v2 + uses: ocaml/setup-ocaml@v3 with: ocaml-compiler: ${{ matrix.ocaml-compiler }} opam-depext: false From 7da7b7ad12b2480e00e9d1fdfca0e9678e2bad18 Mon Sep 17 00:00:00 2001 From: Javier Chavarri Date: Fri, 14 Mar 2025 12:05:32 +0000 Subject: [PATCH 6/6] remove inexistent option --- .github/workflows/build-test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 532de59..ec4ed6d 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -31,7 +31,6 @@ jobs: uses: ocaml/setup-ocaml@v3 with: ocaml-compiler: ${{ matrix.ocaml-compiler }} - opam-depext: false - name: Install all deps run: make install