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: qualify usages of stdlib infix operators #11

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

- PPX: Qualify usages of infix operators with `Stdlib`
([#11](https://github.com/melange-community/melange-json/pull/11))

## 1.2.0 (2024-08-16)

- Port PPX from @andreypopp/ppx_deriving_json
Expand Down
34 changes: 20 additions & 14 deletions ppx/browser/ppx_deriving_json_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@ module Of_json = struct

let eis_json_object ~loc x =
[%expr
Js.typeof [%e x] = "object"
&& (not (Js.Array.isArray [%e x]))
&& not ((Obj.magic [%e x] : 'a Js.null) == Js.null)]
Stdlib.( && )
(Stdlib.( = ) (Js.typeof [%e x]) "object")
(Stdlib.( && )
(Stdlib.not (Js.Array.isArray [%e x]))
(Stdlib.not
(Stdlib.( == ) (Obj.magic [%e x] : 'a Js.null) Js.null)))]

let ensure_json_object ~loc x =
[%expr
if not [%e eis_json_object ~loc x] then
if Stdlib.not [%e eis_json_object ~loc x] then
Ppx_deriving_json_runtime.of_json_error
[%e estring ~loc (sprintf "expected a JSON object")]]

let ensure_json_array_len ~loc n len =
[%expr
if [%e len] <> [%e eint ~loc n] then
if Stdlib.( <> ) [%e len] [%e eint ~loc n] then
Ppx_deriving_json_runtime.of_json_error
[%e
estring ~loc (sprintf "expected a JSON array of length %i" n)]]
Expand All @@ -77,9 +80,11 @@ module Of_json = struct
let n = List.length t.tpl_types in
[%expr
if
Js.Array.isArray [%e x]
&& Js.Array.length (Obj.magic [%e x] : Js.Json.t array)
= [%e eint ~loc n]
Stdlib.( && )
(Js.Array.isArray [%e x])
(Stdlib.( = )
(Js.Array.length (Obj.magic [%e x] : Js.Json.t array))
[%e eint ~loc n])
then
let es = (Obj.magic [%e x] : Js.Json.t array) in
[%e build_tuple ~loc derive 0 t.tpl_types [%expr es]]
Expand Down Expand Up @@ -113,9 +118,9 @@ module Of_json = struct
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 len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
if Stdlib.( = ) (Js.typeof tag) "string" then
let tag = (Obj.magic tag : string) in
[%e body]
else
Expand All @@ -135,13 +140,14 @@ module Of_json = struct
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as ctx) in
[%expr
if tag = [%e estring ~loc:n.loc n.txt] then [%e make None]
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
[%expr
if tag = [%e estring ~loc:n.loc n.txt] then (
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
[%e ensure_json_object ~loc [%expr fs]];
Expand All @@ -154,10 +160,10 @@ module Of_json = struct
let n = Option.value ~default:n (vcs_attr_json_as t.tpl_ctx) in
let arity = List.length t.tpl_types in
[%expr
if tag = [%e estring ~loc:n.loc n.txt] then (
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then (
[%e ensure_json_array_len ~loc (arity + 1) [%expr len]];
[%e
if arity = 0 then make None
if Stdlib.( = ) arity 0 then make None
else
make
(Some
Expand Down
Loading
Loading