Skip to content

Commit 4a0fddf

Browse files
Revert "Fix error message for @@compact_variants" (#90)
1 parent 7cee1ac commit 4a0fddf

7 files changed

Lines changed: 93 additions & 190 deletions

ppx/browser/ppx_deriving_json_js.ml

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ module Of_json = struct
107107
[%e ensure_json_object ~loc x];
108108
[%e build_record ~loc derive t.rcd_fields x Fun.id]]
109109

110-
let derive_of_variant ?(is_compact_variants = false) _derive t
111-
~allow_any_constr body x =
110+
let derive_of_variant _derive t ~allow_any_constr ?td body x =
112111
let loc = t.vrt_loc in
113112
let not_array_error =
114113
match allow_any_constr with
@@ -119,7 +118,7 @@ module Of_json = struct
119118
"expected a non empty JSON array"]
120119
in
121120
let string_branch =
122-
if is_compact_variants then
121+
if Option.fold ~none:false ~some:is_compact_variants td then
123122
[%expr
124123
if Stdlib.( = ) (Js.typeof [%e x]) "string" then (
125124
let array = (Obj.magic [||] : Js.Json.t array) in
@@ -171,34 +170,32 @@ module Of_json = struct
171170
in
172171
({ tag = tag_s; payload } : Melange_json.unknown_variant_case)]
173172

174-
let derive_of_variant_case ?(is_compact_variants = false) derive make c
175-
~allow_any_constr next =
173+
let derive_of_variant_case ?td derive make c ~allow_any_constr next =
174+
let compact = Option.fold ~none:false ~some:is_compact_variants td in
176175
let _ = derive in
177176
let _ = allow_any_constr in
178177
match c with
179-
| Vcs_tuple (n, t) when vcs_attr_json_catch_all t.tpl_ctx -> (
178+
| Vcs_tuple (n, t) when vcs_attr_json_catch_all t.tpl_ctx ->
180179
let loc = n.loc in
181-
match t.tpl_types with
182-
| [ _ ] -> make (Some (build_unknown_variant_case_record ~loc))
183-
| _ ->
184-
Location.raise_errorf ~loc
185-
"[@json.catch_all] requires exactly one argument: a record \
186-
type with fields `tag : string` and `payload : \
187-
Melange_json.t list option` (typically \
188-
[Melange_json.unknown_variant_case])")
189-
| Vcs_record (_n, t) when vcs_attr_json_catch_all t.rcd_ctx -> (
180+
(match t.tpl_types with
181+
| [ _ ] -> make (Some (build_unknown_variant_case_record ~loc))
182+
| _ ->
183+
Location.raise_errorf ~loc
184+
"[@json.catch_all] requires exactly one argument: a record \
185+
type with fields `tag : string` and \
186+
`payload : Melange_json.t list option` (typically \
187+
[Melange_json.unknown_variant_case])")
188+
| Vcs_record (_n, t) when vcs_attr_json_catch_all t.rcd_ctx ->
190189
let loc = t.rcd_loc in
191-
match t.rcd_fields with
192-
| [
193-
{ pld_name = { txt = "tag"; _ }; _ };
194-
{ pld_name = { txt = "payload"; _ }; _ };
195-
] ->
196-
make (Some (build_unknown_variant_case_record ~loc))
197-
| _ ->
198-
Location.raise_errorf ~loc
199-
"[@json.catch_all] inline record must have exactly two \
200-
fields named `tag` and `payload` (in that order), with \
201-
types `string` and `Melange_json.t list option`")
190+
(match t.rcd_fields with
191+
| [ { pld_name = { txt = "tag"; _ }; _ };
192+
{ pld_name = { txt = "payload"; _ }; _ } ] ->
193+
make (Some (build_unknown_variant_case_record ~loc))
194+
| _ ->
195+
Location.raise_errorf ~loc
196+
"[@json.catch_all] inline record must have exactly two \
197+
fields named `tag` and `payload` (in that order), with \
198+
types `string` and `Melange_json.t list option`")
202199
| Vcs_record (n, r) ->
203200
let loc = n.loc in
204201
let n = Option.value ~default:n (vcs_attr_json_name r.rcd_ctx) in
@@ -219,7 +216,7 @@ module Of_json = struct
219216
let loc = n.loc in
220217
let n = Option.value ~default:n (vcs_attr_json_name t.tpl_ctx) in
221218
let arity = List.length t.tpl_types in
222-
if is_compact_variants && arity = 0 then
219+
if compact && arity = 0 then
223220
[%expr
224221
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then
225222
[%e make None]
@@ -289,7 +286,8 @@ module To_json = struct
289286
let record = pexp_record ~loc fs None in
290287
as_json ~loc [%expr [%mel.obj [%e record]]]
291288

292-
let derive_of_variant_case ?(is_compact_variants = false) derive c es =
289+
let derive_of_variant_case ?td derive c es =
290+
let compact = Option.fold ~none:false ~some:is_compact_variants td in
293291
match c with
294292
| Vcs_tuple (n, t) when vcs_attr_json_catch_all t.tpl_ctx -> (
295293
let loc = n.loc in
@@ -305,45 +303,37 @@ module To_json = struct
305303
in
306304
let rest =
307305
Stdlib.List.map
308-
(fun (j : Melange_json.t) ->
309-
(Obj.magic j : Js.Json.t))
306+
(fun (j : Melange_json.t) -> (Obj.magic j : Js.Json.t))
310307
xs
311308
in
312309
(Obj.magic
313-
(Stdlib.Array.of_list (head :: rest)
314-
: Js.Json.t array)
310+
(Stdlib.Array.of_list (head :: rest) : Js.Json.t array)
315311
: Js.Json.t)]
316312
| _ ->
317313
Location.raise_errorf ~loc
318314
"[@json.catch_all] requires exactly one argument: a record \
319-
type with fields `tag : string` and `payload : \
320-
Melange_json.t list option` (typically \
315+
type with fields `tag : string` and \
316+
`payload : Melange_json.t list option` (typically \
321317
[Melange_json.unknown_variant_case])")
322318
| Vcs_record (_n, t) when vcs_attr_json_catch_all t.rcd_ctx -> (
323319
let loc = t.rcd_loc in
324320
match t.rcd_fields, es with
325-
| ( [
326-
{ pld_name = { txt = "tag"; _ }; _ };
327-
{ pld_name = { txt = "payload"; _ }; _ };
328-
],
321+
| ( [ { pld_name = { txt = "tag"; _ }; _ };
322+
{ pld_name = { txt = "payload"; _ }; _ } ],
329323
[ tag_e; payload_e ] ) ->
330324
[%expr
331325
match [%e payload_e] with
332326
| Stdlib.Option.None ->
333327
(Obj.magic ([%e tag_e] : string) : Js.Json.t)
334328
| Stdlib.Option.Some xs ->
335-
let head =
336-
(Obj.magic ([%e tag_e] : string) : Js.Json.t)
337-
in
329+
let head = (Obj.magic ([%e tag_e] : string) : Js.Json.t) in
338330
let rest =
339331
Stdlib.List.map
340-
(fun (j : Melange_json.t) ->
341-
(Obj.magic j : Js.Json.t))
332+
(fun (j : Melange_json.t) -> (Obj.magic j : Js.Json.t))
342333
xs
343334
in
344335
(Obj.magic
345-
(Stdlib.Array.of_list (head :: rest)
346-
: Js.Json.t array)
336+
(Stdlib.Array.of_list (head :: rest) : Js.Json.t array)
347337
: Js.Json.t)]
348338
| _ ->
349339
Location.raise_errorf ~loc
@@ -369,7 +359,7 @@ module To_json = struct
369359
let loc = n.loc in
370360
let n = Option.value ~default:n (vcs_attr_json_name t.tpl_ctx) in
371361
let arity = List.length t.tpl_types in
372-
if is_compact_variants && arity = 0 then
362+
if compact && arity = 0 then
373363
as_json ~loc (estring ~loc:n.loc n.txt)
374364
else
375365
let tag =

ppx/native/common/ppx_deriving_json_common.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ let ld_attr_json_drop_default_if_json_equal =
101101
(Attribute.declare_flag "json.drop_default_if_json_equal"
102102
Attribute.Context.label_declaration)
103103

104+
let td_attr_json_compact_variants =
105+
Attribute.get
106+
(Attribute.declare "json.compact_variants"
107+
Attribute.Context.type_declaration
108+
Ast_pattern.(pstr nil)
109+
())
110+
111+
let is_compact_variants td =
112+
Option.is_some (td_attr_json_compact_variants td)
113+
104114
let ld_attr_default ld =
105115
match ld_attr_json_default ld with
106116
| Some e -> Some e

0 commit comments

Comments
 (0)