Skip to content

Commit

Permalink
ppx: drop special encoding of enum like variants
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
Khady committed Oct 4, 2024
1 parent 8e617a6 commit 3349ce1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 154 deletions.
59 changes: 17 additions & 42 deletions ppx/browser/ppx_deriving_json_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,48 +101,28 @@ module Of_json = struct

let derive_of_variant _derive t body x =
let loc = t.vrt_loc in
let is_enum =
List.for_all t.vrt_cases ~f:(function
| Vcs_enum _ -> true
| _ -> false)
in
match is_enum with
| true ->
[%expr
let tag =
Ppx_deriving_json_runtime.Primitives.string_of_json [%e x]
in
[%e body]]
| false ->
[%expr
if Js.Array.isArray [%e x] then
let array = (Obj.magic [%e x] : Js.Json.t array) in
let len = Js.Array.length array in
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
"expected a non empty JSON array with element being a \
string"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
[%expr
if Js.Array.isArray [%e x] then
let array = (Obj.magic [%e x] : Js.Json.t array) in
let len = Js.Array.length array in
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
"expected a non empty JSON array"]
"expected a non empty JSON array with element being a \
string"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"]

let derive_of_variant_case derive make c next =
match c with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as ctx) in
[%expr
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then
[%e make None]
else [%e next]]
| Vcs_record (n, r) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as r.rcd_ctx) in
Expand Down Expand Up @@ -211,11 +191,6 @@ module To_json = struct

let derive_of_variant_case derive c es =
match c with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as ctx) in
let tag = [%expr string_to_json [%e estring ~loc:n.loc n.txt]] in
as_json ~loc tag
| Vcs_record (n, r) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as r.rcd_ctx) in
Expand Down
8 changes: 0 additions & 8 deletions ppx/native/ppx_deriving_json_native.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ module Of_json = struct

let derive_of_variant_case derive make vcs =
match vcs with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as ctx) in
[%pat? `String [%p pstring ~loc:n.loc n.txt]] --> make None
| Vcs_tuple (n, t) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as t.tpl_ctx) in
Expand Down Expand Up @@ -212,10 +208,6 @@ module To_json = struct

let derive_of_variant_case derive vcs es =
match vcs with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as ctx) in
[%expr `String [%e estring ~loc:n.loc n.txt]]
| Vcs_tuple (n, t) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as t.tpl_ctx) in
Expand Down
Loading

0 comments on commit 3349ce1

Please sign in to comment.