Skip to content

Commit e889fdc

Browse files
committed
fix unused tag warning
1 parent 23a2466 commit e889fdc

File tree

2 files changed

+96
-77
lines changed

2 files changed

+96
-77
lines changed

ppx/browser/ppx_deriving_json_js.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ module Of_json = struct
108108
if Stdlib.( > ) len 0 then
109109
let tag = Js.Array.unsafe_get array 0 in
110110
if Stdlib.( = ) (Js.typeof tag) "string" then
111-
let tag = (Obj.magic tag : string) in
112111
[%e body]
113112
else
114113
Ppx_deriving_json_runtime.of_json_error
@@ -127,6 +126,7 @@ module Of_json = struct
127126
let loc = n.loc in
128127
let n = Option.value ~default:n (vcs_attr_json_name r.rcd_ctx) in
129128
[%expr
129+
let tag = (Obj.magic tag : string) in
130130
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then (
131131
[%e ensure_json_array_len ~loc 2 [%expr len]];
132132
let fs = Js.Array.unsafe_get array 1 in
@@ -140,6 +140,7 @@ module Of_json = struct
140140
let n = Option.value ~default:n (vcs_attr_json_name t.tpl_ctx) in
141141
let arity = List.length t.tpl_types in
142142
[%expr
143+
let tag = (Obj.magic tag : string) in
143144
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then (
144145
[%e ensure_json_array_len ~loc (arity + 1) [%expr len]];
145146
[%e

ppx/test/ppx_deriving_json_js.t

Lines changed: 94 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -351,42 +351,48 @@
351351
Ppx_deriving_json_runtime.of_json_error
352352
"expected a JSON array of length 1";
353353
A)
354-
else if Stdlib.( = ) tag "B" then (
355-
if Stdlib.( <> ) len 2 then
356-
Ppx_deriving_json_runtime.of_json_error
357-
"expected a JSON array of length 2";
358-
B (int_of_json (Js.Array.unsafe_get array 1)))
359-
else if Stdlib.( = ) tag "C" then (
360-
if Stdlib.( <> ) len 2 then
361-
Ppx_deriving_json_runtime.of_json_error
362-
"expected a JSON array of length 2";
363-
let fs = Js.Array.unsafe_get array 1 in
364-
if
365-
Stdlib.not
366-
(Stdlib.( && )
367-
(Stdlib.( = ) (Js.typeof fs) "object")
368-
(Stdlib.( && )
369-
(Stdlib.not (Js.Array.isArray fs))
370-
(Stdlib.not
371-
(Stdlib.( == )
372-
(Obj.magic fs : 'a Js.null)
373-
Js.null))))
374-
then
375-
Ppx_deriving_json_runtime.of_json_error
376-
"expected a JSON object";
377-
let fs =
378-
(Obj.magic fs : < name : Js.Json.t Js.undefined > Js.t)
379-
in
380-
C
381-
{
382-
name =
383-
(match Js.Undefined.toOption fs##name with
384-
| Stdlib.Option.Some v -> string_of_json v
385-
| Stdlib.Option.None ->
386-
Ppx_deriving_json_runtime.of_json_error
387-
"missing field \"name\"");
388-
})
389-
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
354+
else
355+
let tag = (Obj.magic tag : string) in
356+
if Stdlib.( = ) tag "B" then (
357+
if Stdlib.( <> ) len 2 then
358+
Ppx_deriving_json_runtime.of_json_error
359+
"expected a JSON array of length 2";
360+
B (int_of_json (Js.Array.unsafe_get array 1)))
361+
else
362+
let tag = (Obj.magic tag : string) in
363+
if Stdlib.( = ) tag "C" then (
364+
if Stdlib.( <> ) len 2 then
365+
Ppx_deriving_json_runtime.of_json_error
366+
"expected a JSON array of length 2";
367+
let fs = Js.Array.unsafe_get array 1 in
368+
if
369+
Stdlib.not
370+
(Stdlib.( && )
371+
(Stdlib.( = ) (Js.typeof fs) "object")
372+
(Stdlib.( && )
373+
(Stdlib.not (Js.Array.isArray fs))
374+
(Stdlib.not
375+
(Stdlib.( == )
376+
(Obj.magic fs : 'a Js.null)
377+
Js.null))))
378+
then
379+
Ppx_deriving_json_runtime.of_json_error
380+
"expected a JSON object";
381+
let fs =
382+
(Obj.magic fs
383+
: < name : Js.Json.t Js.undefined > Js.t)
384+
in
385+
C
386+
{
387+
name =
388+
(match Js.Undefined.toOption fs##name with
389+
| Stdlib.Option.Some v -> string_of_json v
390+
| Stdlib.Option.None ->
391+
Ppx_deriving_json_runtime.of_json_error
392+
"missing field \"name\"");
393+
})
394+
else
395+
Ppx_deriving_json_runtime.of_json_error "invalid JSON"
390396
else
391397
Ppx_deriving_json_runtime.of_json_error
392398
"expected a non empty JSON array with element being a \
@@ -561,15 +567,17 @@
561567
Ppx_deriving_json_runtime.of_json_error
562568
"expected a JSON array of length 1";
563569
Some `A)
564-
else if Stdlib.( = ) tag "B" then (
565-
if Stdlib.( <> ) len 2 then
566-
Ppx_deriving_json_runtime.of_json_error
567-
"expected a JSON array of length 2";
568-
Some (`B (int_of_json (Js.Array.unsafe_get array 1))))
569570
else
570-
match other_of_json_poly x with
571-
| Some x -> (Some x :> [ `A | `B of int | other ] option)
572-
| None -> None
571+
let tag = (Obj.magic tag : string) in
572+
if Stdlib.( = ) tag "B" then (
573+
if Stdlib.( <> ) len 2 then
574+
Ppx_deriving_json_runtime.of_json_error
575+
"expected a JSON array of length 2";
576+
Some (`B (int_of_json (Js.Array.unsafe_get array 1))))
577+
else
578+
match other_of_json_poly x with
579+
| Some x -> (Some x :> [ `A | `B of int | other ] option)
580+
| None -> None
573581
else
574582
Ppx_deriving_json_runtime.of_json_error
575583
"expected a non empty JSON array with element being a \
@@ -755,12 +763,14 @@
755763
Ppx_deriving_json_runtime.of_json_error
756764
"expected a JSON array of length 1";
757765
A)
758-
else if Stdlib.( = ) tag "Fix" then (
759-
if Stdlib.( <> ) len 2 then
760-
Ppx_deriving_json_runtime.of_json_error
761-
"expected a JSON array of length 2";
762-
Fix (recur_of_json (Js.Array.unsafe_get array 1)))
763-
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
766+
else
767+
let tag = (Obj.magic tag : string) in
768+
if Stdlib.( = ) tag "Fix" then (
769+
if Stdlib.( <> ) len 2 then
770+
Ppx_deriving_json_runtime.of_json_error
771+
"expected a JSON array of length 2";
772+
Fix (recur_of_json (Js.Array.unsafe_get array 1)))
773+
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
764774
else
765775
Ppx_deriving_json_runtime.of_json_error
766776
"expected a non empty JSON array with element being a \
@@ -814,13 +824,16 @@
814824
Ppx_deriving_json_runtime.of_json_error
815825
"expected a JSON array of length 1";
816826
Some `A)
817-
else if Stdlib.( = ) tag "Fix" then (
818-
if Stdlib.( <> ) len 2 then
819-
Ppx_deriving_json_runtime.of_json_error
820-
"expected a JSON array of length 2";
821-
Some
822-
(`Fix (polyrecur_of_json (Js.Array.unsafe_get array 1))))
823-
else None
827+
else
828+
let tag = (Obj.magic tag : string) in
829+
if Stdlib.( = ) tag "Fix" then (
830+
if Stdlib.( <> ) len 2 then
831+
Ppx_deriving_json_runtime.of_json_error
832+
"expected a JSON array of length 2";
833+
Some
834+
(`Fix
835+
(polyrecur_of_json (Js.Array.unsafe_get array 1))))
836+
else None
824837
else
825838
Ppx_deriving_json_runtime.of_json_error
826839
"expected a non empty JSON array with element being a \
@@ -882,12 +895,14 @@
882895
Ppx_deriving_json_runtime.of_json_error
883896
"expected a JSON array of length 1";
884897
A)
885-
else if Stdlib.( = ) tag "b_aliased" then (
886-
if Stdlib.( <> ) len 1 then
887-
Ppx_deriving_json_runtime.of_json_error
888-
"expected a JSON array of length 1";
889-
B)
890-
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
898+
else
899+
let tag = (Obj.magic tag : string) in
900+
if Stdlib.( = ) tag "b_aliased" then (
901+
if Stdlib.( <> ) len 1 then
902+
Ppx_deriving_json_runtime.of_json_error
903+
"expected a JSON array of length 1";
904+
B)
905+
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
891906
else
892907
Ppx_deriving_json_runtime.of_json_error
893908
"expected a non empty JSON array with element being a \
@@ -940,12 +955,14 @@
940955
Ppx_deriving_json_runtime.of_json_error
941956
"expected a JSON array of length 1";
942957
Some `a)
943-
else if Stdlib.( = ) tag "b" then (
944-
if Stdlib.( <> ) len 1 then
945-
Ppx_deriving_json_runtime.of_json_error
946-
"expected a JSON array of length 1";
947-
Some `b)
948-
else None
958+
else
959+
let tag = (Obj.magic tag : string) in
960+
if Stdlib.( = ) tag "b" then (
961+
if Stdlib.( <> ) len 1 then
962+
Ppx_deriving_json_runtime.of_json_error
963+
"expected a JSON array of length 1";
964+
Some `b)
965+
else None
949966
else
950967
Ppx_deriving_json_runtime.of_json_error
951968
"expected a non empty JSON array with element being a \
@@ -1006,12 +1023,14 @@
10061023
Ppx_deriving_json_runtime.of_json_error
10071024
"expected a JSON array of length 2";
10081025
A (a_of_json (Js.Array.unsafe_get array 1)))
1009-
else if Stdlib.( = ) tag "B" then (
1010-
if Stdlib.( <> ) len 2 then
1011-
Ppx_deriving_json_runtime.of_json_error
1012-
"expected a JSON array of length 2";
1013-
B (b_of_json (Js.Array.unsafe_get array 1)))
1014-
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
1026+
else
1027+
let tag = (Obj.magic tag : string) in
1028+
if Stdlib.( = ) tag "B" then (
1029+
if Stdlib.( <> ) len 2 then
1030+
Ppx_deriving_json_runtime.of_json_error
1031+
"expected a JSON array of length 2";
1032+
B (b_of_json (Js.Array.unsafe_get array 1)))
1033+
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
10151034
else
10161035
Ppx_deriving_json_runtime.of_json_error
10171036
"expected a non empty JSON array with element being a string"
@@ -1368,7 +1387,6 @@
13681387
if Stdlib.( > ) len 0 then
13691388
let tag = Js.Array.unsafe_get array 0 in
13701389
if Stdlib.( = ) (Js.typeof tag) "string" then
1371-
let tag = (Obj.magic tag : string) in
13721390
match one_of_json_poly x with
13731391
| Some x -> (Some x :> [ one | other ] option)
13741392
| None -> (

0 commit comments

Comments
 (0)