Skip to content

Commit 06a3cad

Browse files
committed
feat(ocaml_of_json_schema): accept newtypes and empty structs
1 parent a036f24 commit 06a3cad

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

engine/utils/ocaml_of_json_schema/ocaml_of_json_schema.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ let exporters = {
438438
},
439439
},
440440
empty_struct: {
441-
guard: def => (eq(keys(def), new Set(["type"])) && def.type == 'object'),
441+
guard: def => (eq(keys(def), new Set(["type"])) && (def.type == 'object' || def.type == 'null')),
442442
f: (name, _) => {
443443
return {
444444
type: `EmptyStruct${name}`,
@@ -447,6 +447,21 @@ let exporters = {
447447
};
448448
},
449449
},
450+
newtype: {
451+
guard: def => !exporters['empty_struct'].guard(def) && is_type.expr(def, ["try-parse"]),
452+
f: (name, o) => {
453+
let path = [name + '-newtype-ref'];
454+
let te = is_type.expr(o, path);
455+
let ocaml_type = ocaml_of_type_expr(te, path);
456+
let arms = ocaml_arms_of_type_expr(te, path);
457+
let to_json = ocaml_yojson_of_type_expr(te, `(let Newtype${name} inner = o in inner)`, path);
458+
return {
459+
type: `Newtype${name} of ${ocaml_type}`,
460+
parse: `Newtype${name}(${mk_match('o', arms, path)})`,
461+
to_json,
462+
};
463+
},
464+
},
450465
// object is a *flat* record
451466
object: {
452467
guard: def => (eq(keys(def), new Set(["type", "required", "properties"]))

0 commit comments

Comments
 (0)