Skip to content

Commit

Permalink
remove containers dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Aug 15, 2024
1 parent 83f8d8a commit b5aa8a5
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 39 deletions.
6 changes: 3 additions & 3 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

(maintainers
"Antonio Nuno Monteiro <[email protected]>"
"Javier Ch\195\161varri <[email protected]>")
"Javier Chávarri <[email protected]>")

(license "LGPL-3.0-only" "MPL-2.0")

Expand All @@ -23,7 +23,8 @@

(package
(name melange-json)
(synopsis "Compositional JSON encode/decode library and PPX for Melange, with native compatibility")
(synopsis
"Compositional JSON encode/decode library and PPX for Melange, with native compatibility")
(description
"Provides encoders and decoders to convert JSON values into typed values. With the possibility to create custom encoders and decoders and automate them with a PPX.")
(depends
Expand All @@ -36,7 +37,6 @@
(>= "3.10.0")
:with-test))
ppxlib
containers
yojson ; only used for the native version
(opam-check-npm-deps :with-test) ; todo: use with-dev-setup once opam 2.2 is out
(ocaml-lsp-server :with-test)
Expand Down
1 change: 0 additions & 1 deletion melange-json.opam
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ depends: [
"melange-jest" {with-test}
"reason" {>= "3.10.0" & with-test}
"ppxlib"
"containers"
"yojson"
"opam-check-npm-deps" {with-test}
"ocaml-lsp-server" {with-test}
Expand Down
2 changes: 1 addition & 1 deletion ppx/browser/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
\
ppx_deriving_json_runtime
ppx_deriving_json_browser_test)
(libraries ppxlib containers)
(libraries ppxlib)
(ppx_runtime_libraries melange-json.browser_runtime)
(preprocess
(pps ppxlib.metaquot))
Expand Down
20 changes: 10 additions & 10 deletions ppx/browser/ppx_deriving_json_browser.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
open Printf
open ContainersLabels
open StdLabels
open Ppxlib
open Ast_builder.Default
open Ppx_deriving_tools
Expand All @@ -16,7 +16,7 @@ module Of_json = struct
let build_js_type ~loc (fs : label_declaration list) =
let f ld =
let n = ld.pld_name in
let n = Option.get_or ~default:n (ld_attr_json_key ld) in
let n = Option.value ~default:n (ld_attr_json_key ld) in
let pof_desc = Otag (n, [%type: Js.Json.t Js.undefined]) in
{ pof_loc = loc; pof_attributes = []; pof_desc }
in
Expand All @@ -27,7 +27,7 @@ module Of_json = struct
let handle_field fs ld =
( map_loc lident ld.pld_name,
let n = ld.pld_name in
let n = Option.get_or ~default:n (ld_attr_json_key ld) in
let n = Option.value ~default:n (ld_attr_json_key ld) in
[%expr
match
Js.Undefined.toOption
Expand Down Expand Up @@ -133,13 +133,13 @@ module Of_json = struct
match c with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as ctx) 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]
else [%e next]]
| Vcs_record (n, r) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as r.rcd_ctx) 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 (
[%e ensure_json_array_len ~loc 2 [%expr len]];
Expand All @@ -151,7 +151,7 @@ module Of_json = struct
else [%e next]]
| Vcs_tuple (n, t) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as t.tpl_ctx) in
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 (
Expand Down Expand Up @@ -185,7 +185,7 @@ module To_json = struct
let fs =
List.map2 t.rcd_fields es ~f:(fun ld x ->
let n = ld.pld_name in
let n = Option.get_or ~default:n (ld_attr_json_key ld) in
let n = Option.value ~default:n (ld_attr_json_key ld) in
let this = derive ld.pld_type x in
map_loc lident n, this)
in
Expand All @@ -196,18 +196,18 @@ module To_json = struct
match c with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as ctx) 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.get_or ~default:n (vcs_attr_json_as r.rcd_ctx) in
let n = Option.value ~default:n (vcs_attr_json_as r.rcd_ctx) in
let tag = [%expr 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.get_or ~default:n (vcs_attr_json_as t.tpl_ctx) in
let n = Option.value ~default:n (vcs_attr_json_as t.tpl_ctx) in
let tag = [%expr 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
2 changes: 1 addition & 1 deletion ppx/native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
\
ppx_deriving_json_runtime
ppx_deriving_json_native_test)
(libraries ppxlib containers)
(libraries ppxlib)
(ppx_runtime_libraries melange-json.native_runtime)
(preprocess
(pps ppxlib.metaquot))
Expand Down
20 changes: 10 additions & 10 deletions ppx/native/ppx_deriving_json_native.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
open Printf
open ContainersLabels
open StdLabels
open Ppxlib
open Ast_builder.Default
open Ppx_deriving_tools
Expand Down Expand Up @@ -54,7 +54,7 @@ module Of_json = struct
List.fold_left (List.rev fs) ~init:[ fail_case ]
~f:(fun next ld ->
let key =
Option.get_or ~default:ld.pld_name (ld_attr_json_key ld)
Option.value ~default:ld.pld_name (ld_attr_json_key ld)
in
pstring ~loc:key.loc key.txt
--> [%expr
Expand All @@ -68,7 +68,7 @@ module Of_json = struct
let fields =
List.map fs ~f:(fun ld ->
let key =
Option.get_or ~default:ld.pld_name (ld_attr_json_key ld)
Option.value ~default:ld.pld_name (ld_attr_json_key ld)
in
let default = ld_attr_default ld in
( map_loc lident ld.pld_name,
Expand Down Expand Up @@ -135,11 +135,11 @@ module Of_json = struct
match vcs with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as ctx) 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.get_or ~default:n (vcs_attr_json_as t.tpl_ctx) in
let n = Option.value ~default:n (vcs_attr_json_as t.tpl_ctx) in
let arity = List.length t.tpl_types in
if arity = 0 then
[%pat? `List [ `String [%p pstring ~loc:n.loc n.txt] ]]
Expand All @@ -151,7 +151,7 @@ module Of_json = struct
--> make (Some (build_tuple ~loc derive xexprs t.tpl_types))
| Vcs_record (n, t) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as t.rcd_ctx) in
let n = Option.value ~default:n (vcs_attr_json_as t.rcd_ctx) in
let allow_extra_fields =
match t.rcd_ctx with
| Vcs_ctx_variant cd ->
Expand Down Expand Up @@ -181,7 +181,7 @@ module To_json = struct
let es =
List.map2 t.rcd_fields es ~f:(fun ld x ->
let key =
Option.get_or ~default:ld.pld_name (ld_attr_json_key ld)
Option.value ~default:ld.pld_name (ld_attr_json_key ld)
in
[%expr
[%e estring ~loc:key.loc key.txt], [%e derive ld.pld_type x]])
Expand All @@ -192,18 +192,18 @@ module To_json = struct
match vcs with
| Vcs_enum (n, ctx) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as ctx) 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.get_or ~default:n (vcs_attr_json_as t.tpl_ctx) in
let n = Option.value ~default:n (vcs_attr_json_as t.tpl_ctx) in
[%expr
`List
(`String [%e estring ~loc:n.loc n.txt]
:: [%e elist ~loc (List.map2 t.tpl_types es ~f:derive)])]
| Vcs_record (n, t) ->
let loc = n.loc in
let n = Option.get_or ~default:n (vcs_attr_json_as t.rcd_ctx) in
let n = Option.value ~default:n (vcs_attr_json_as t.rcd_ctx) in
[%expr
`List
(`String [%e estring ~loc:n.loc n.txt]
Expand Down
2 changes: 1 addition & 1 deletion ppx/test/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(cram
(deps
(package ppx_deriving_json)
(package melange-json)
./example.ml
../../.ocamlformat
../native/ppx_deriving_json_native_test.exe
Expand Down
24 changes: 12 additions & 12 deletions tools/ppx_deriving_tools.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Printf
open Ppxlib
open Ast_builder.Default
open ContainersLabels
open StdLabels
open Expansion_helpers

exception Error of location * string
Expand All @@ -19,7 +19,7 @@ let map_loc f a_loc = { a_loc with txt = f a_loc.txt }

let gen_bindings ~loc prefix n =
List.split
(List.init n ~f:(fun i ->
(List.init ~len:n ~f:(fun i ->
let id = sprintf "%s_%i" prefix i in
let patt = ppat_var ~loc { loc; txt = id } in
let expr = pexp_ident ~loc { loc; txt = lident id } in
Expand Down Expand Up @@ -250,7 +250,7 @@ module Schema = struct
fun ~ctxt (_rec_flag, type_decls) ->
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
let bindings =
List.flat_map type_decls ~f:(fun decl ->
List.concat_map type_decls ~f:(fun decl ->
self#derive_of_type_declaration decl)
in
[%str
Expand Down Expand Up @@ -376,7 +376,7 @@ module Schema = struct
fun ~ctxt (_rec_flag, tds) ->
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
let bindings =
List.flat_map tds ~f:self#derive_of_type_declaration
List.concat_map tds ~f:self#derive_of_type_declaration
in
[%str
[@@@ocaml.warning "-39-11-27"]
Expand Down Expand Up @@ -689,17 +689,17 @@ module Conv = struct
let loc = t.ptyp_loc in
let is_enum, cases = repr_polyvariant_cases cs in
let ctors, inherits =
List.partition_filter_map cases ~f:(fun (c, r) ->
List.partition_map cases ~f:(fun (c, r) ->
let ctx = Vcs_ctx_polyvariant c in
match r with
| `Rtag (n, ts) ->
if is_enum then `Left (n, Vcs_enum (n, ctx))
if is_enum then Left (n, Vcs_enum (n, ctx))
else
let t =
{ tpl_loc = loc; tpl_types = ts; tpl_ctx = ctx }
in
`Left (n, Vcs_tuple (n, t))
| `Rinherit (n, ts) -> `Right (n, ts))
Left (n, Vcs_tuple (n, t))
| `Rinherit (n, ts) -> Right (n, ts))
in
let catch_all =
[%pat? x]
Expand Down Expand Up @@ -785,17 +785,17 @@ module Conv = struct
let loc = t.ptyp_loc in
let is_enum, cases = repr_polyvariant_cases cs in
let ctors, inherits =
List.partition_filter_map cases ~f:(fun (c, r) ->
List.partition_map cases ~f:(fun (c, r) ->
let ctx = Vcs_ctx_polyvariant c in
match r with
| `Rtag (n, ts) ->
if is_enum then `Left (n, Vcs_enum (n, ctx))
if is_enum then Left (n, Vcs_enum (n, ctx))
else
let t =
{ tpl_loc = loc; tpl_types = ts; tpl_ctx = ctx }
in
`Left (n, Vcs_tuple (n, t))
| `Rinherit (n, ts) -> `Right (n, ts))
Left (n, Vcs_tuple (n, t))
| `Rinherit (n, ts) -> Right (n, ts))
in
let catch_all =
[%pat? x]
Expand Down

0 comments on commit b5aa8a5

Please sign in to comment.