osury 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Codegen.res.mjs +58 -4
package/package.json
CHANGED
package/src/Codegen.res.mjs
CHANGED
|
@@ -241,6 +241,43 @@ function hasUnion(_schema) {
|
|
|
241
241
|
};
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
+
function isRefOnlyUnion(types) {
|
|
245
|
+
return types.every(t => {
|
|
246
|
+
if (typeof t !== "object") {
|
|
247
|
+
return false;
|
|
248
|
+
} else {
|
|
249
|
+
return t._tag === "Ref";
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function generateInlineRecord(refName, schemasDict) {
|
|
255
|
+
let other = schemasDict[refName];
|
|
256
|
+
if (other !== undefined) {
|
|
257
|
+
if (typeof other !== "object" || other._tag !== "Object") {
|
|
258
|
+
return generateType(other);
|
|
259
|
+
} else {
|
|
260
|
+
return generateRecord(other._0);
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
return lcFirst(refName);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function generateInlineVariantBody(types, schemasDict) {
|
|
268
|
+
return types.map(t => {
|
|
269
|
+
if (typeof t === "object" && t._tag === "Ref") {
|
|
270
|
+
let name = t._0;
|
|
271
|
+
let tag = ucFirst(name);
|
|
272
|
+
let inlineRecord = generateInlineRecord(name, schemasDict);
|
|
273
|
+
return tag + `(` + inlineRecord + `)`;
|
|
274
|
+
}
|
|
275
|
+
let tag$1 = getTagForType(t);
|
|
276
|
+
let payload = generateType(t);
|
|
277
|
+
return tag$1 + `(` + payload + `)`;
|
|
278
|
+
}).join(" | ");
|
|
279
|
+
}
|
|
280
|
+
|
|
244
281
|
function getUnionName(types) {
|
|
245
282
|
let names = types.map(t => {
|
|
246
283
|
if (typeof t !== "object") {
|
|
@@ -517,15 +554,24 @@ function generateVariantBody(types) {
|
|
|
517
554
|
}).join(" | ");
|
|
518
555
|
}
|
|
519
556
|
|
|
520
|
-
function generateTypeDefWithSkipSet(namedSchema, _skipSet) {
|
|
557
|
+
function generateTypeDefWithSkipSet(namedSchema, _skipSet, schemasDict) {
|
|
521
558
|
let typeName = lcFirst(namedSchema.name);
|
|
522
559
|
let types = namedSchema.schema;
|
|
523
560
|
if (typeof types === "object" && types._tag === "Union") {
|
|
524
|
-
let
|
|
525
|
-
|
|
561
|
+
let types$1 = types._0;
|
|
562
|
+
if (isRefOnlyUnion(types$1)) {
|
|
563
|
+
let variantBody = generateInlineVariantBody(types$1, schemasDict);
|
|
564
|
+
return `@genType
|
|
526
565
|
@tag("_tag")
|
|
527
566
|
@schema
|
|
528
567
|
type ` + typeName + ` = ` + variantBody;
|
|
568
|
+
}
|
|
569
|
+
let variantBody$1 = generateVariantBody(types$1);
|
|
570
|
+
return `@genType
|
|
571
|
+
@tag("_tag")
|
|
572
|
+
@unboxed
|
|
573
|
+
@schema
|
|
574
|
+
type ` + typeName + ` = ` + variantBody$1;
|
|
529
575
|
}
|
|
530
576
|
let typeBody = generateType(namedSchema.schema);
|
|
531
577
|
return `@genType
|
|
@@ -540,6 +586,7 @@ function generateTypeDef(namedSchema) {
|
|
|
540
586
|
let variantBody = generateVariantBody(types._0);
|
|
541
587
|
return `@genType
|
|
542
588
|
@tag("_tag")
|
|
589
|
+
@unboxed
|
|
543
590
|
@schema
|
|
544
591
|
type ` + typeName + ` = ` + variantBody;
|
|
545
592
|
}
|
|
@@ -568,9 +615,13 @@ function generateModule(schemas) {
|
|
|
568
615
|
schema: replaceUnions(s.name, s.schema)
|
|
569
616
|
}));
|
|
570
617
|
let allSchemas = uniqueUnions.concat(modifiedSchemas);
|
|
618
|
+
let schemasDict = {};
|
|
619
|
+
allSchemas.forEach(s => {
|
|
620
|
+
schemasDict[s.name] = s.schema;
|
|
621
|
+
});
|
|
571
622
|
let sorted = topologicalSort(allSchemas);
|
|
572
623
|
let skipSet = {};
|
|
573
|
-
let typeDefs = sorted.map(s => generateTypeDefWithSkipSet(s, skipSet)).join("\n\n");
|
|
624
|
+
let typeDefs = sorted.map(s => generateTypeDefWithSkipSet(s, skipSet, schemasDict)).join("\n\n");
|
|
574
625
|
return "module S = Sury\n\n" + typeDefs;
|
|
575
626
|
}
|
|
576
627
|
|
|
@@ -593,6 +644,9 @@ export {
|
|
|
593
644
|
getTagForType,
|
|
594
645
|
generateUnion,
|
|
595
646
|
hasUnion,
|
|
647
|
+
isRefOnlyUnion,
|
|
648
|
+
generateInlineRecord,
|
|
649
|
+
generateInlineVariantBody,
|
|
596
650
|
getUnionName,
|
|
597
651
|
extractUnions,
|
|
598
652
|
extractUnionsFromType,
|