Skip to content

Commit d6ca192

Browse files
authored
upgrade ppxlib to 0.36 / handle Ptyp_open (#60)
1 parent a1fc9ed commit d6ca192

File tree

12 files changed

+1368
-1893
lines changed

12 files changed

+1368
-1893
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
opam-repositories: |
3838
default: https://github.com/ocaml/opam-repository.git
3939
40-
- run: opam install . --deps-only --with-test
40+
- run: opam install . --deps-only --with-test --with-dev-setup
4141

4242
- run: opam exec -- dune build
4343

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Unreleased
2+
3+
- Support `Ptype_open`, e.g. `type u = X.(x) [@@deriving json]`
4+
([#60](https://github.com/melange-community/melange-json/pull/60))
5+
16
## 2.0.0 (2025-03-11)
27

38
- **[breaking]** Library, PPX: Unify runtimes (`*.ppx-runtime` libraries are

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ help: ## Print this help message
1313

1414
.PHONY: create-switch
1515
create-switch: ## Create opam switch
16-
opam switch create . 5.2.0 -y --deps-only
16+
opam switch create . 5.4.0 -y --deps-only
1717

1818
.PHONY: init
1919
init: create-switch install ## Configure everything to develop this repository in local

dune-project

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
(>= "3.10.0")
3636
:with-test))
3737
(ppxlib
38-
(>= "0.32.0"))
39-
(opam-check-npm-deps :with-test) ; todo: use with-dev-setup once opam 2.2 is out
40-
(ocaml-lsp-server :with-test)
38+
(>= "0.36.0"))
39+
(opam-check-npm-deps :with-dev-setup)
40+
(ocaml-lsp-server :with-dev-setup)
4141
(ocamlformat
4242
(and
4343
(>= "0.27.0")
44-
:with-test))))
44+
:with-dev-setup))))
4545

4646
(package
4747
(name melange-json-native)
@@ -53,6 +53,6 @@
5353
(ocaml
5454
(>= "4.12"))
5555
(ppxlib
56-
(>= "0.32.0"))
56+
(>= "0.36.0"))
5757
(yojson
5858
(>= "1.6.0"))))

melange-json-native.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bug-reports: "https://github.com/melange-community/melange-json/issues"
1414
depends: [
1515
"dune" {>= "3.16"}
1616
"ocaml" {>= "4.12"}
17-
"ppxlib" {>= "0.32.0"}
17+
"ppxlib" {>= "0.36.0"}
1818
"yojson" {>= "1.6.0"}
1919
"odoc" {with-doc}
2020
]

melange-json.opam

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ depends: [
1717
"melange" {>= "4.0.0"}
1818
"melange-jest" {with-test}
1919
"reason" {>= "3.10.0" & with-test}
20-
"ppxlib" {>= "0.32.0"}
21-
"opam-check-npm-deps" {with-test}
22-
"ocaml-lsp-server" {with-test}
23-
"ocamlformat" {>= "0.27.0" & with-test}
20+
"ppxlib" {>= "0.36.0"}
21+
"opam-check-npm-deps" {with-dev-setup}
22+
"ocaml-lsp-server" {with-dev-setup}
23+
"ocamlformat" {>= "0.27.0" & with-dev-setup}
2424
"odoc" {with-doc}
2525
]
2626
build: [
@@ -39,6 +39,6 @@ build: [
3939
]
4040
dev-repo: "git+https://github.com/melange-community/melange-json.git"
4141
pin-depends: [
42-
[ "opam-check-npm-deps.dev" "git+https://github.com/ahrefs/opam-check-npm-deps.git#12e63761842c71dc3a688257f7827580ebe2b7f1"]
42+
[ "opam-check-npm-deps.dev" "git+https://github.com/ahrefs/opam-check-npm-deps.git#8adc5a6fa6b744c4007f5085ef0cf1347a4b5f36" ]
4343
]
4444

melange-json.opam.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pin-depends: [
2-
[ "opam-check-npm-deps.dev" "git+https://github.com/ahrefs/opam-check-npm-deps.git#12e63761842c71dc3a688257f7827580ebe2b7f1"]
2+
[ "opam-check-npm-deps.dev" "git+https://github.com/ahrefs/opam-check-npm-deps.git#8adc5a6fa6b744c4007f5085ef0cf1347a4b5f36" ]
33
]
44

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"devDependencies": {
3-
"jest": "^26.5.2",
3+
"jest": "^29.0",
4+
"jest-environment-jsdom": "^29.0",
45
"@testing-library/jest-dom": "^5.10.0"
56
}
67
}

ppx/native/common/ppx_deriving_tools.ml

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,35 @@ open Ast_builder.Default
44
open StdLabels
55
open Expansion_helpers
66

7+
module Lid = struct
8+
let flatten =
9+
let rec flat accu = function
10+
| Lident s -> s :: accu
11+
| Ldot (lid, s) -> flat (s :: accu) lid
12+
| Lapply (_, _) -> failwith "Longident.flat"
13+
in
14+
fun lid -> flat [] lid
15+
16+
let unflatten l =
17+
match l with
18+
| [] -> None
19+
| hd :: tl ->
20+
Some
21+
(List.fold_left
22+
~f:(fun p s -> Ldot (p, s))
23+
~init:(Lident hd) tl)
24+
end
25+
726
let not_supported ~loc what =
827
Location.raise_errorf ~loc "%s are not supported" what
928

1029
let map_loc f a_loc = { a_loc with txt = f a_loc.txt }
1130

31+
let lident_with_optional_open ?opn label =
32+
match opn with
33+
| Some { txt = lid; _ } -> Longident.Ldot (lid, label)
34+
| None -> lident label
35+
1236
let gen_bindings ~loc prefix n =
1337
List.split
1438
(List.init ~len:n ~f:(fun i ->
@@ -129,13 +153,14 @@ module Schema = struct
129153
| Rinherit _ ->
130154
not_supported ~loc:field.prf_loc "this polyvariant inherit"
131155

132-
let repr_core_type ty =
156+
let rec repr_core_type ty =
133157
let loc = ty.ptyp_loc in
134158
match ty.ptyp_desc with
135159
| Ptyp_tuple ts -> `Ptyp_tuple ts
136160
| Ptyp_constr (id, ts) -> `Ptyp_constr (id, ts)
137161
| Ptyp_var txt -> `Ptyp_var { txt; loc = ty.ptyp_loc }
138162
| Ptyp_variant (fs, Closed, None) -> `Ptyp_variant fs
163+
| Ptyp_open (id, ct) -> `Ptyp_open (id, repr_core_type ct)
139164
| Ptyp_variant _ -> not_supported ~loc "non closed polyvariants"
140165
| Ptyp_arrow _ -> not_supported ~loc "function types"
141166
| Ptyp_any -> not_supported ~loc "type placeholders"
@@ -246,11 +271,31 @@ module Schema = struct
246271

247272
method private derive_of_core_type' t =
248273
let loc = t.ptyp_loc in
249-
match repr_core_type t with
274+
self#derive_of_core_type_repr ~loc t (repr_core_type t)
275+
276+
method private derive_of_core_type_repr ?opn ~loc t repr =
277+
match repr with
250278
| `Ptyp_tuple ts -> As_fun (self#derive_of_tuple t ts)
251279
| `Ptyp_var label ->
252-
As_val (ederiver self#name (map_loc lident label))
280+
As_val
281+
(ederiver self#name
282+
(map_loc (lident_with_optional_open ?opn) label))
283+
| `Ptyp_open (_, `Ptyp_open _) -> assert false
284+
| `Ptyp_open (lid, ct) ->
285+
self#derive_of_core_type_repr ~opn:lid ~loc t ct
253286
| `Ptyp_constr (id, ts) ->
287+
let id =
288+
match opn with
289+
| Some { txt = lid; loc } ->
290+
{
291+
txt =
292+
Lid.flatten lid @ Lid.flatten id.txt
293+
|> Lid.unflatten
294+
|> Option.get;
295+
loc;
296+
}
297+
| None -> id
298+
in
254299
self#derive_type_ref' self#name ~loc id ts
255300
| `Ptyp_variant fs -> As_fun (self#derive_of_polyvariant t fs)
256301

ppx/test/dune

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
../../.ocamlformat
99
../native/ppx_deriving_json_native_test.exe
1010
../browser/ppx_deriving_json_js_test.exe))
11+
12+
(cram
13+
(applies_to ptype_open)
14+
(enabled_if
15+
(>= %{ocaml_version} 5.2.0)))
16+

0 commit comments

Comments
 (0)