Skip to content

Commit

Permalink
fixup! ppx: remove poly special case
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Nov 26, 2024
1 parent 1794441 commit 7973a7d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 45 deletions.
4 changes: 2 additions & 2 deletions ppx/browser/ppx_deriving_json_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ let of_string s =

type error = Json.Decode.error =
| Json_error of string
| Unpexpected_variant of string
| Unexpected_variant of string

exception Of_json_error = Json.Decode.DecodeError

let of_json_error msg = raise (Of_json_error (Json_error msg))

let unexpected_variant_error tag =
raise (Of_json_error (Unpexpected_variant tag))
raise (Of_json_error (Unexpected_variant tag))

module To_json = struct
external string_to_json : string -> t = "%identity"
Expand Down
2 changes: 1 addition & 1 deletion ppx/native/ppx_deriving_json_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let of_string s =
try Yojson.Basic.from_string s
with Yojson.Json_error msg -> raise (Of_string_error msg)

type error = Json_error of string | Unpexpected_variant of string
type error = Json_error of string | Unexpected_variant of string

exception Of_json_error of error

Expand Down
14 changes: 7 additions & 7 deletions ppx/test/ppx_deriving_json_js.t
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@
else
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -564,10 +564,10 @@
| e -> (e :> [ `A | `B of int | other ])
| exception
Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant _) ->
(Unexpected_variant _) ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -626,7 +626,7 @@
else
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -684,7 +684,7 @@
else
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a string"
Expand Down Expand Up @@ -798,7 +798,7 @@
else
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -915,7 +915,7 @@
else
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down
26 changes: 13 additions & 13 deletions ppx/test/ppx_deriving_json_native.t
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@
| _ ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> sum)

let _ = sum_of_json
Expand Down Expand Up @@ -430,7 +430,7 @@
| _ ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> sum2)

let _ = sum2_of_json
Expand Down Expand Up @@ -464,7 +464,7 @@
| x ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> other)

let _ = other_of_json
Expand Down Expand Up @@ -495,10 +495,10 @@
| x -> (x :> [ `A | `B of int | other ])
| exception
Ppx_deriving_json_runtime.Of_json_error
(Ppx_deriving_json_runtime.Unpexpected_variant _) ->
(Ppx_deriving_json_runtime.Unexpected_variant _) ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant")))
(Unexpected_variant "unexpected variant")))
: Yojson.Basic.t -> poly)

let _ = poly_of_json
Expand Down Expand Up @@ -534,7 +534,7 @@
| x ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> poly2)

let _ = poly2_of_json
Expand Down Expand Up @@ -568,7 +568,7 @@
| x ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))

let _ = c_of_json

Expand Down Expand Up @@ -598,7 +598,7 @@
| _ ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> recur)
let _ = recur_of_json
Expand Down Expand Up @@ -633,7 +633,7 @@
| x ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> polyrecur)
let _ = polyrecur_of_json
Expand Down Expand Up @@ -668,7 +668,7 @@
| _ ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> evar)
let _ = evar_of_json
Expand Down Expand Up @@ -703,7 +703,7 @@
| x ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> epoly)
let _ = epoly_of_json
Expand Down Expand Up @@ -738,7 +738,7 @@
| _ ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
let _ = p2_of_json
Expand Down Expand Up @@ -845,7 +845,7 @@
| _ ->
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))
(Unexpected_variant "unexpected variant"))
: Yojson.Basic.t -> allow_extra_fields2)

let _ = allow_extra_fields2_of_json
Expand Down
10 changes: 5 additions & 5 deletions ppx/tools/ppx_deriving_tools.ml
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ module Conv = struct
( [%expr
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))],
(Unexpected_variant "unexpected variant"))],
[] )
~f:(fun (next, cases) (c, r) ->
let ctx = Vcs_ctx_polyvariant c in
Expand Down Expand Up @@ -466,7 +466,7 @@ module Conv = struct
| e -> (e :> [%t t])
| exception
Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant _) ->
(Unexpected_variant _) ->
[%e next]]
in
next, cases)
Expand Down Expand Up @@ -510,7 +510,7 @@ module Conv = struct
--> [%expr
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))];
(Unexpected_variant "unexpected variant"))];
]
~f:(fun next (c : constructor_declaration) ->
let ctx = Vcs_ctx_variant c in
Expand Down Expand Up @@ -563,7 +563,7 @@ module Conv = struct
[%expr
raise
(Ppx_deriving_json_runtime.Of_json_error
(Unpexpected_variant "unexpected variant"))]
(Unexpected_variant "unexpected variant"))]
~f:(fun next (n, ts) ->
let maybe =
self#derive_type_ref ~loc self#name n ts x
Expand All @@ -574,7 +574,7 @@ module Conv = struct
| x -> (x :> [%t t])
| exception
Ppx_deriving_json_runtime.Of_json_error
(Ppx_deriving_json_runtime.Unpexpected_variant
(Ppx_deriving_json_runtime.Unexpected_variant
_) ->
[%e next]])
in
Expand Down
44 changes: 28 additions & 16 deletions src/Json_decode.ml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
external _unsafeCreateUninitializedArray : int -> 'a array = "Array" [@@mel.new]
external _unsafeCreateUninitializedArray : int -> 'a array = "Array"
[@@mel.new]

external _stringify : Js.Json.t -> string = "JSON.stringify"

let _isInteger value =
Js.Float.isFinite value && Js.Math.floor_float value == value

type 'a decoder = Js.Json.t -> 'a

type error = Json_error of string | Unpexpected_variant of string
type error = Json_error of string | Unexpected_variant of string

let error_to_string = function
| Json_error msg -> msg
| Unpexpected_variant tag -> "unexpected variant: " ^ tag
| Unexpected_variant tag -> "unexpected variant: " ^ tag

exception DecodeError of error

let error msg = raise (DecodeError (Json_error msg))

let id json = json

let bool json =
Expand All @@ -32,7 +32,8 @@ let int json =
else error ("Expected integer, got " ^ _stringify json)

let string json =
if Js.typeof json = "string" then (Obj.magic (json : Js.Json.t) : string)
if Js.typeof json = "string" then
(Obj.magic (json : Js.Json.t) : string)
else error ("Expected string, got " ^ _stringify json)

let char json =
Expand Down Expand Up @@ -60,7 +61,10 @@ let array decode json =
let value =
try decode (Array.unsafe_get source i)
with DecodeError err ->
error (error_to_string err ^ "\n\tin array at index " ^ string_of_int i)
error
(error_to_string err
^ "\n\tin array at index "
^ string_of_int i)
in
Array.unsafe_set target i value
done;
Expand All @@ -77,7 +81,8 @@ let pair decodeA decodeB json =
try
( decodeA (Array.unsafe_get source 0),
decodeB (Array.unsafe_get source 1) )
with DecodeError err -> error (error_to_string err ^ "\n\tin pair/tuple2")
with DecodeError err ->
error (error_to_string err ^ "\n\tin pair/tuple2")
else
let length = Js.String.make length in
error {j|Expected array of length 2, got array of length $length|j}
Expand All @@ -94,7 +99,8 @@ let tuple3 decodeA decodeB decodeC json =
( decodeA (Array.unsafe_get source 0),
decodeB (Array.unsafe_get source 1),
decodeC (Array.unsafe_get source 2) )
with DecodeError err -> error (error_to_string err ^ "\n\tin tuple3")
with DecodeError err ->
error (error_to_string err ^ "\n\tin tuple3")
else
let length = Js.String.make length in
error {j|Expected array of length 3, got array of length $length|j}
Expand All @@ -110,7 +116,8 @@ let tuple4 decodeA decodeB decodeC decodeD json =
decodeB (Array.unsafe_get source 1),
decodeC (Array.unsafe_get source 2),
decodeD (Array.unsafe_get source 3) )
with DecodeError err -> error (error_to_string err ^ "\n\tin tuple4")
with DecodeError err ->
error (error_to_string err ^ "\n\tin tuple4")
else
let length = Js.String.make length in
error {j|Expected array of length 4, got array of length $length|j}
Expand All @@ -130,7 +137,8 @@ let dict decode json =
let key = Array.unsafe_get keys i in
let value =
try decode (Js.Dict.unsafeGet source key)
with DecodeError err -> error (error_to_string err ^ "\n\tin dict")
with DecodeError err ->
error (error_to_string err ^ "\n\tin dict")
in
Js.Dict.set target key value
done;
Expand Down Expand Up @@ -158,19 +166,23 @@ let rec at key_path decoder =
| first :: rest -> field first (at rest decoder)
| [] ->
raise
@@ Invalid_argument "Expected key_path to contain at least one element"
@@ Invalid_argument
"Expected key_path to contain at least one element"

let optional decode json = try Some (decode json) with DecodeError _ -> None
let optional decode json =
try Some (decode json) with DecodeError _ -> None

let oneOf decoders json =
let rec inner decoders errors =
match decoders with
| [] ->
let formattedErrors =
"\n- " ^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev errors))
"\n- "
^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev errors))
in
error ({j|All decoders given to oneOf failed. Here are all the errors: $formattedErrors\nAnd the JSON being decoded: |j}
^ _stringify json)
error
({j|All decoders given to oneOf failed. Here are all the errors: $formattedErrors\nAnd the JSON being decoded: |j}
^ _stringify json)
| decode :: rest -> (
try decode json with DecodeError e -> inner rest (e :: errors))
in
Expand Down
2 changes: 1 addition & 1 deletion src/Json_decode.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ third-party libraries.
type 'a decoder = Js.Json.t -> 'a
(** The type of a decoder combinator *)

type error = Json_error of string | Unpexpected_variant of string
type error = Json_error of string | Unexpected_variant of string

val error_to_string : error -> string

Expand Down

0 comments on commit 7973a7d

Please sign in to comment.