Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ppx: remove string_to_json usage on js side #35

Merged
merged 5 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
([#30](https://github.com/melange-community/melange-json/pull/30))
- PPX: add support for `int64` in the runtime
([#33](https://github.com/melange-community/melange-json/pull/33))
- PPX: qualify `string_to_json` usage on js side
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the end this isn't really what ended up happening 😬

([#35](https://github.com/melange-community/melange-json/pull/35))

## 1.3.0 (2024-08-28)

Expand Down
12 changes: 10 additions & 2 deletions ppx/browser/ppx_deriving_json_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,21 @@ module To_json = struct
| Vcs_record (n, r) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_name r.rcd_ctx) in
let tag = [%expr string_to_json [%e estring ~loc:n.loc n.txt]] in
let tag =
[%expr
Ppx_deriving_json_runtime.Primitives.string_to_json
[%e estring ~loc:n.loc n.txt]]
in
let es = [ derive_of_record derive r es ] in
as_json ~loc (pexp_array ~loc (tag :: es))
| Vcs_tuple (n, t) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_name t.tpl_ctx) in
let tag = [%expr string_to_json [%e estring ~loc:n.loc n.txt]] in
let tag =
[%expr
Ppx_deriving_json_runtime.Primitives.string_to_json
[%e estring ~loc:n.loc n.txt]]
in
let es = List.map2 t.tpl_types es ~f:derive in
as_json ~loc (pexp_array ~loc (tag :: es))

Expand Down
122 changes: 102 additions & 20 deletions ppx/test/ppx_deriving_json_js.t
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,23 @@
let rec sum_to_json =
(fun x ->
match x with
| A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
| A ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
|]
: Js.Json.t)
| B x_0 ->
(Obj.magic [| string_to_json "B"; int_to_json x_0 |]
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "B";
int_to_json x_0;
|]
: Js.Json.t)
| C { name = x_name } ->
(Obj.magic
[|
string_to_json "C";
Ppx_deriving_json_runtime.Primitives.string_to_json "C";
(Obj.magic [%mel.obj { name = string_to_json x_name }]
: Js.Json.t);
|]
Expand Down Expand Up @@ -472,7 +481,9 @@
| S2 (x_0, x_1) ->
(Obj.magic
[|
string_to_json "S2"; int_to_json x_0; string_to_json x_1;
Ppx_deriving_json_runtime.Primitives.string_to_json "S2";
int_to_json x_0;
string_to_json x_1;
|]
: Js.Json.t)
: sum2 -> Js.Json.t)
Expand Down Expand Up @@ -532,7 +543,12 @@
let rec other_to_json =
(fun x ->
match x with
| `C -> (Obj.magic [| string_to_json "C" |] : Js.Json.t)
| `C ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "C";
|]
: Js.Json.t)
: other -> Js.Json.t)

let _ = other_to_json
Expand Down Expand Up @@ -595,9 +611,18 @@
let rec poly_to_json =
(fun x ->
match x with
| `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
| `A ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
|]
: Js.Json.t)
| `B x_0 ->
(Obj.magic [| string_to_json "B"; int_to_json x_0 |]
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "B";
int_to_json x_0;
|]
: Js.Json.t)
| #other as x -> other_to_json x
: poly -> Js.Json.t)
Expand Down Expand Up @@ -663,7 +688,9 @@
| `P2 (x_0, x_1) ->
(Obj.magic
[|
string_to_json "P2"; int_to_json x_0; string_to_json x_1;
Ppx_deriving_json_runtime.Primitives.string_to_json "P2";
int_to_json x_0;
string_to_json x_1;
|]
: Js.Json.t)
: poly2 -> Js.Json.t)
Expand Down Expand Up @@ -721,7 +748,12 @@
fun x ->
match x with
| `C x_0 ->
(Obj.magic [| string_to_json "C"; a_to_json x_0 |] : Js.Json.t)
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "C";
a_to_json x_0;
|]
: Js.Json.t)

let _ = c_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]
Expand Down Expand Up @@ -775,9 +807,18 @@
let rec recur_to_json =
(fun x ->
match x with
| A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
| A ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
|]
: Js.Json.t)
| Fix x_0 ->
(Obj.magic [| string_to_json "Fix"; recur_to_json x_0 |]
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "Fix";
recur_to_json x_0;
|]
: Js.Json.t)
: recur -> Js.Json.t)

Expand Down Expand Up @@ -842,9 +883,18 @@
let rec polyrecur_to_json =
(fun x ->
match x with
| `A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
| `A ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
|]
: Js.Json.t)
| `Fix x_0 ->
(Obj.magic [| string_to_json "Fix"; polyrecur_to_json x_0 |]
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "Fix";
polyrecur_to_json x_0;
|]
: Js.Json.t)
: polyrecur -> Js.Json.t)

Expand Down Expand Up @@ -900,8 +950,19 @@
let rec evar_to_json =
(fun x ->
match x with
| A -> (Obj.magic [| string_to_json "A" |] : Js.Json.t)
| B -> (Obj.magic [| string_to_json "b_aliased" |] : Js.Json.t)
| A ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
|]
: Js.Json.t)
| B ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json
"b_aliased";
|]
: Js.Json.t)
: evar -> Js.Json.t)

let _ = evar_to_json
Expand Down Expand Up @@ -964,8 +1025,19 @@
let rec epoly_to_json =
(fun x ->
match x with
| `a -> (Obj.magic [| string_to_json "A_aliased" |] : Js.Json.t)
| `b -> (Obj.magic [| string_to_json "b" |] : Js.Json.t)
| `a ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json
"A_aliased";
|]
: Js.Json.t)
| `b ->
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "b";
|]
: Js.Json.t)
: epoly -> Js.Json.t)

let _ = epoly_to_json
Expand Down Expand Up @@ -1019,9 +1091,19 @@
fun x ->
match x with
| A x_0 ->
(Obj.magic [| string_to_json "A"; a_to_json x_0 |] : Js.Json.t)
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
a_to_json x_0;
|]
: Js.Json.t)
| B x_0 ->
(Obj.magic [| string_to_json "B"; b_to_json x_0 |] : Js.Json.t)
(Obj.magic
[|
Ppx_deriving_json_runtime.Primitives.string_to_json "B";
b_to_json x_0;
|]
: Js.Json.t)

let _ = p2_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]
Expand Down Expand Up @@ -1147,7 +1229,7 @@
| A { a = x_a } ->
(Obj.magic
[|
string_to_json "A";
Ppx_deriving_json_runtime.Primitives.string_to_json "A";
(Obj.magic [%mel.obj { a = int_to_json x_a }] : Js.Json.t);
|]
: Js.Json.t)
Expand Down
26 changes: 26 additions & 0 deletions ppx/test/ppx_deriving_json_js_variants.e2e.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

$ echo '(lang dune 3.11)
> (using melange 0.1)' > dune-project

$ echo '
> (library
> (name lib)
> (modes melange)
> (modules main)
> (flags :standard -w -37-69)
> (preprocess (pps melange.ppx melange-json.ppx)))
> (melange.emit
> (alias js)
> (target output)
> (modules)
> (libraries lib)
> (module_systems commonjs))' > dune

$ echo '
> type sum = A [@@deriving json]
> let json = sum_to_json A
> ' >> main.ml

Can build without having to open Ppx_deriving_json_runtime.Primitives

$ dune build @js