From 3bb100347eadf16c81c12c989e885a0dda23ea16 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Thu, 30 Nov 2023 19:19:10 -0800 Subject: [PATCH 1/3] Melange v3 long-lived branch --- examples/encode.ml | 4 ++-- src/Json_decode.ml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/encode.ml b/examples/encode.ml index 075d9c0..9a37b6e 100644 --- a/examples/encode.ml +++ b/examples/encode.ml @@ -10,7 +10,7 @@ let _ = (* prints ["foo", "bar"] *) let _ = [| "foo"; "bar" |] - |> Js.Array.map Json.Encode.string + |> Js.Array.map ~f:Json.Encode.string |> Json.Encode.jsonArray |> Json.stringify |> Js.log @@ -63,4 +63,4 @@ let data = { let _ = data |> Encode.line - |> Js.log \ No newline at end of file + |> Js.log diff --git a/src/Json_decode.ml b/src/Json_decode.ml index d6718c3..d09cba1 100644 --- a/src/Json_decode.ml +++ b/src/Json_decode.ml @@ -164,7 +164,7 @@ let oneOf decoders json = match decoders with | [] -> let formattedErrors = - "\n- " ^ Js.Array.joinWith "\n- " (Array.of_list (List.rev errors)) + "\n- " ^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev errors)) in raise @@ DecodeError From 0039ac6c88a87c71c8bb54f38c31d67c032d2c8f Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 17 Jan 2024 09:25:57 -0800 Subject: [PATCH 2/3] update --- examples/decode.ml | 2 +- examples/encode.ml | 57 ++++++++++++++++------------------------------ src/Json_decode.ml | 15 +++++++----- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/examples/decode.ml b/examples/decode.ml index 307b9af..11c8e73 100644 --- a/examples/decode.ml +++ b/examples/decode.ml @@ -2,7 +2,7 @@ let mapJsonObjectString f decoder (encoder : int -> Js.Json.t) str = let json = Json.parseOrRaise str in Json.Decode.(dict decoder json) - |> Js.Dict.map (fun [@u] v -> f v) + |> Js.Dict.map ~f:(fun [@u] v -> f v) |> Json.Encode.dict encoder |> Json.stringify let sum = Array.fold_left ( + ) 0 diff --git a/examples/encode.ml b/examples/encode.ml index 9a37b6e..bfd6a01 100644 --- a/examples/encode.ml +++ b/examples/encode.ml @@ -2,10 +2,7 @@ (* prints ["foo", "bar"] *) let _ = - [| "foo"; "bar" |] - |> Json.Encode.stringArray - |> Json.stringify - |> Js.log + [| "foo"; "bar" |] |> Json.Encode.stringArray |> Json.stringify |> Js.log (* prints ["foo", "bar"] *) let _ = @@ -16,49 +13,33 @@ let _ = |> Js.log (* prints { x: 42, foo: 'bar' } *) -let _ = - Json.Encode.( - object_ [ - ("x", int 42); - ("foo", string "bar") - ] - |> Js.log - ) +let _ = Json.Encode.(object_ [ ("x", int 42); ("foo", string "bar") ] |> Js.log) (* Advanced example: encode a record *) -type line = { - start: point; - end_: point; - thickness: int option -} -and point = { - x: float; - y: float -} +type line = { start : point; end_ : point; thickness : int option } +and point = { x : float; y : float } module Encode = struct let point r = - let open! Json.Encode in ( - object_ [ - ("x", float r.x); - ("y", float r.y) - ] - ) + let open! Json.Encode in + object_ [ ("x", float r.x); ("y", float r.y) ] + let line r = Json.Encode.( - object_ [ - ("start", point r.start); - ("end", point r.end_); - ("thickness", match r.thickness with Some x -> int x | None -> null) - ] - ) + object_ + [ + ("start", point r.start); + ("end", point r.end_); + ("thickness", match r.thickness with Some x -> int x | None -> null); + ]) end -let data = { - start = { x = 1.1; y = -0.4 }; - end_ = { x = 5.3; y = 3.8 }; - thickness = Some 2 -} +let data = + { + start = { x = 1.1; y = -0.4 }; + end_ = { x = 5.3; y = 3.8 }; + thickness = Some 2; + } let _ = data diff --git a/src/Json_decode.ml b/src/Json_decode.ml index d09cba1..0595322 100644 --- a/src/Json_decode.ml +++ b/src/Json_decode.ml @@ -73,9 +73,10 @@ let pair decodeA decodeB json = decodeB (Array.unsafe_get source 1) ) with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin pair/tuple2") else + let length = Js.String.make length in raise - @@ DecodeError - {j|Expected array of length 2, got array of length $length|j} + (DecodeError + {j|Expected array of length 2, got array of length $length|j}) else raise @@ DecodeError ("Expected array, got " ^ _stringify json) let tuple2 = pair @@ -91,9 +92,10 @@ let tuple3 decodeA decodeB decodeC json = decodeC (Array.unsafe_get source 2) ) with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin tuple3") else + let length = Js.String.make length in raise - @@ DecodeError - {j|Expected array of length 3, got array of length $length|j} + (DecodeError + {j|Expected array of length 3, got array of length $length|j}) else raise @@ DecodeError ("Expected array, got " ^ _stringify json) let tuple4 decodeA decodeB decodeC decodeD json = @@ -108,9 +110,10 @@ let tuple4 decodeA decodeB decodeC decodeD json = decodeD (Array.unsafe_get source 3) ) with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin tuple4") else + let length = Js.String.make length in raise - @@ DecodeError - {j|Expected array of length 4, got array of length $length|j} + (DecodeError + {j|Expected array of length 4, got array of length $length|j}) else raise @@ DecodeError ("Expected array, got " ^ _stringify json) let dict decode json = From dba6894d3adb526e15b76ecb3d82de15067e1aee Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Thu, 1 Feb 2024 15:03:44 -0800 Subject: [PATCH 3/3] chore: update nix flakes --- flake.lock | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/flake.lock b/flake.lock index 6bc1ed1..d591fde 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1694529238, - "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "owner": "numtide", "repo": "flake-utils", - "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "type": "github" }, "original": { @@ -30,11 +30,11 @@ ] }, "locked": { - "lastModified": 1695779252, - "narHash": "sha256-/tlUGkM4jXEF+FG0xe0I51qY7XLBd3db2m4kpBGg264=", + "lastModified": 1705466801, + "narHash": "sha256-1dub8rJZkAjZiKR0EopdXVP3JCbSq/8fAw2Fei0Hh+E=", "owner": "melange-re", "repo": "melange-compiler-libs", - "rev": "eb8207c1b6744180095184194dfd843e06f9fad1", + "rev": "87bb98024b1669766f9c8aec3bbbf73ef332dcb6", "type": "github" }, "original": { @@ -57,11 +57,11 @@ ] }, "locked": { - "lastModified": 1696756708, - "narHash": "sha256-+4l14c4cYdYVt5WfMRU66vdj2wUL66r/lg9ZXHhbZJg=", + "lastModified": 1706823522, + "narHash": "sha256-LsqXADoVR4T8V1xH2z6rwz/DPJ1epI5u1/eRElwNHKg=", "owner": "melange-re", "repo": "melange", - "rev": "2ff08be262f113fc8d28b66c272502c6f403399c", + "rev": "09aa67bdc69e92610b25cef7386965deb21df00b", "type": "github" }, "original": { @@ -72,11 +72,11 @@ }, "nix-filter": { "locked": { - "lastModified": 1694857738, - "narHash": "sha256-bxxNyLHjhu0N8T3REINXQ2ZkJco0ABFPn6PIe2QUfqo=", + "lastModified": 1705332318, + "narHash": "sha256-kcw1yFeJe9N4PjQji9ZeX47jg0p9A0DuU4djKvg1a7I=", "owner": "numtide", "repo": "nix-filter", - "rev": "41fd48e00c22b4ced525af521ead8792402de0ea", + "rev": "3449dc925982ad46246cfc36469baf66e1b64f17", "type": "github" }, "original": { @@ -93,11 +93,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1696894742, - "narHash": "sha256-Ee9sHSkAVWbuHUSRP4MVyefGlyxxEzIf62cbfujTQ8I=", + "lastModified": 1706823728, + "narHash": "sha256-xvW0ernpGeJZgLcvln+xwPZ+OYzda0U+JqSqJwDjCgw=", "owner": "nix-ocaml", "repo": "nix-overlays", - "rev": "a6a66ed0edfd8ecc39bb3a049197d598d7c8052e", + "rev": "31d09298120b184279e03a8849e7dcd1adffc42b", "type": "github" }, "original": { @@ -108,17 +108,17 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1696826630, - "narHash": "sha256-oGU94vo6pkzGbaSsPHjpHtOUg6b7nL8v3xATnrcw3cQ=", + "lastModified": 1706768163, + "narHash": "sha256-mSQ/t2+AriQCxsHHDJ/2uJGMnUzjZLKFVYImln05JPs=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "5a9c737c587d2c34d63c5b3cb53c6ab0705bdf4f", + "rev": "32cf02a2607143d94c565c068b73fe45fd57c3a0", "type": "github" }, "original": { "owner": "NixOS", "repo": "nixpkgs", - "rev": "5a9c737c587d2c34d63c5b3cb53c6ab0705bdf4f", + "rev": "32cf02a2607143d94c565c068b73fe45fd57c3a0", "type": "github" } },