Skip to content

Commit

Permalink
fix unused tag warning
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavarri committed Nov 21, 2024
1 parent 23a2466 commit e889fdc
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 77 deletions.
3 changes: 2 additions & 1 deletion ppx/browser/ppx_deriving_json_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ module Of_json = struct
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Stdlib.( = ) (Js.typeof tag) "string" then
let tag = (Obj.magic tag : string) in
[%e body]
else
Ppx_deriving_json_runtime.of_json_error
Expand All @@ -127,6 +126,7 @@ module Of_json = struct
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_name r.rcd_ctx) in
[%expr
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then (
[%e ensure_json_array_len ~loc 2 [%expr len]];
let fs = Js.Array.unsafe_get array 1 in
Expand All @@ -140,6 +140,7 @@ module Of_json = struct
let n = Option.value ~default:n (vcs_attr_json_name t.tpl_ctx) in
let arity = List.length t.tpl_types in
[%expr
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then (
[%e ensure_json_array_len ~loc (arity + 1) [%expr len]];
[%e
Expand Down
170 changes: 94 additions & 76 deletions ppx/test/ppx_deriving_json_js.t
Original file line number Diff line number Diff line change
Expand Up @@ -351,42 +351,48 @@
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
A)
else if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
B (int_of_json (Js.Array.unsafe_get array 1)))
else if Stdlib.( = ) tag "C" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
let fs = Js.Array.unsafe_get array 1 in
if
Stdlib.not
(Stdlib.( && )
(Stdlib.( = ) (Js.typeof fs) "object")
(Stdlib.( && )
(Stdlib.not (Js.Array.isArray fs))
(Stdlib.not
(Stdlib.( == )
(Obj.magic fs : 'a Js.null)
Js.null))))
then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON object";
let fs =
(Obj.magic fs : < name : Js.Json.t Js.undefined > Js.t)
in
C
{
name =
(match Js.Undefined.toOption fs##name with
| Stdlib.Option.Some v -> string_of_json v
| Stdlib.Option.None ->
Ppx_deriving_json_runtime.of_json_error
"missing field \"name\"");
})
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
B (int_of_json (Js.Array.unsafe_get array 1)))
else
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "C" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
let fs = Js.Array.unsafe_get array 1 in
if
Stdlib.not
(Stdlib.( && )
(Stdlib.( = ) (Js.typeof fs) "object")
(Stdlib.( && )
(Stdlib.not (Js.Array.isArray fs))
(Stdlib.not
(Stdlib.( == )
(Obj.magic fs : 'a Js.null)
Js.null))))
then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON object";
let fs =
(Obj.magic fs
: < name : Js.Json.t Js.undefined > Js.t)
in
C
{
name =
(match Js.Undefined.toOption fs##name with
| Stdlib.Option.Some v -> string_of_json v
| Stdlib.Option.None ->
Ppx_deriving_json_runtime.of_json_error
"missing field \"name\"");
})
else
Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -561,15 +567,17 @@
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `A)
else if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Some (`B (int_of_json (Js.Array.unsafe_get array 1))))
else
match other_of_json_poly x with
| Some x -> (Some x :> [ `A | `B of int | other ] option)
| None -> None
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Some (`B (int_of_json (Js.Array.unsafe_get array 1))))
else
match other_of_json_poly x with
| Some x -> (Some x :> [ `A | `B of int | other ] option)
| None -> None
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -755,12 +763,14 @@
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
A)
else if Stdlib.( = ) tag "Fix" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Fix (recur_of_json (Js.Array.unsafe_get array 1)))
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "Fix" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Fix (recur_of_json (Js.Array.unsafe_get array 1)))
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -814,13 +824,16 @@
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `A)
else if Stdlib.( = ) tag "Fix" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Some
(`Fix (polyrecur_of_json (Js.Array.unsafe_get array 1))))
else None
else
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "Fix" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Some
(`Fix
(polyrecur_of_json (Js.Array.unsafe_get array 1))))
else None
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -882,12 +895,14 @@
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
A)
else if Stdlib.( = ) tag "b_aliased" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
B)
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "b_aliased" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
B)
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -940,12 +955,14 @@
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `a)
else if Stdlib.( = ) tag "b" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `b)
else None
else
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "b" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `b)
else None
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
Expand Down Expand Up @@ -1006,12 +1023,14 @@
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
A (a_of_json (Js.Array.unsafe_get array 1)))
else if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
B (b_of_json (Js.Array.unsafe_get array 1)))
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
B (b_of_json (Js.Array.unsafe_get array 1)))
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a string"
Expand Down Expand Up @@ -1368,7 +1387,6 @@
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Stdlib.( = ) (Js.typeof tag) "string" then
let tag = (Obj.magic tag : string) in
match one_of_json_poly x with
| Some x -> (Some x :> [ one | other ] option)
| None -> (
Expand Down

0 comments on commit e889fdc

Please sign in to comment.