From c5647f72cc87b33a5fa7aeefba92ef389f48eb2c Mon Sep 17 00:00:00 2001 From: Andrey Popp <8mayday@gmail.com> Date: Sat, 5 Oct 2024 13:56:44 +0400 Subject: [PATCH] ppx: flatten tuples in poly constructors We want to flatten the representation of polyvariants with tuple payload and represent them as [TAG, ARG1, ARG2, ...] instead of [TAG, [ARG1, ARG2, ...]]. Fixes https://github.com/melange-community/melange-json/issues/24 --- CHANGES.md | 3 +++ ppx/tools/ppx_deriving_tools.ml | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 553a340..e392148 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,9 @@ ([#13](https://github.com/melange-community/melange-json/pull/13)) - PPX: Add `yojson` as runtime dep for the native version ([#15](https://github.com/melange-community/melange-json/pull/15)) +- PPX: change JSON representation of polyvariants, make it compatible with + ppx_deriving_yojson and ppx_yojson_conv + ([#27](https://github.com/melange-community/melange-json/pull/27)) ## 1.3.0 (2024-08-28) diff --git a/ppx/tools/ppx_deriving_tools.ml b/ppx/tools/ppx_deriving_tools.ml index 13a28f8..d37a735 100644 --- a/ppx/tools/ppx_deriving_tools.ml +++ b/ppx/tools/ppx_deriving_tools.ml @@ -115,7 +115,12 @@ let register_combined ?deps name derivings = module Schema = struct let repr_row_field field = match field.prf_desc with - | Rtag (id, _, ts) -> `Rtag (id, ts) + | Rtag (id, _, []) -> `Rtag (id, []) + | Rtag (id, _, [ { ptyp_desc = Ptyp_tuple ts; _ } ]) -> `Rtag (id, ts) + | Rtag (id, _, [ t ]) -> `Rtag (id, [ t ]) + | Rtag (_, _, _ :: _) -> + not_supported ~loc:field.prf_loc + "polyvariant constructor with more than one argument" | Rinherit { ptyp_desc = Ptyp_constr (id, ts); _ } -> `Rinherit (id, ts) | Rinherit _ ->