osury 1.1.0 → 1.2.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/src/IRGen.res.mjs DELETED
@@ -1,367 +0,0 @@
1
- // Generated by ReScript, PLEASE EDIT WITH CARE
2
-
3
- import * as Core__Option from "@rescript/core/src/Core__Option.res.mjs";
4
- import * as CodegenHelpers from "./CodegenHelpers.res.mjs";
5
- import * as CodegenTransforms from "./CodegenTransforms.res.mjs";
6
-
7
- function convertType(schema) {
8
- if (typeof schema !== "object") {
9
- switch (schema) {
10
- case "String" :
11
- return {
12
- TAG: "Primitive",
13
- _0: "PString"
14
- };
15
- case "Number" :
16
- return {
17
- TAG: "Primitive",
18
- _0: "PFloat"
19
- };
20
- case "Integer" :
21
- return {
22
- TAG: "Primitive",
23
- _0: "PInt"
24
- };
25
- case "Boolean" :
26
- return {
27
- TAG: "Primitive",
28
- _0: "PBool"
29
- };
30
- case "Null" :
31
- return {
32
- TAG: "Primitive",
33
- _0: "PUnit"
34
- };
35
- case "Unknown" :
36
- return "JSON";
37
- }
38
- } else {
39
- switch (schema._tag) {
40
- case "Optional" :
41
- return {
42
- TAG: "Option",
43
- _0: convertType(schema._0)
44
- };
45
- case "Nullable" :
46
- return {
47
- TAG: "Nullable",
48
- _0: convertType(schema._0)
49
- };
50
- case "Object" :
51
- let fields = schema._0;
52
- if (fields.length === 0) {
53
- return "JSON";
54
- } else {
55
- return {
56
- TAG: "InlineRecord",
57
- _0: fields.map(convertField)
58
- };
59
- }
60
- case "Array" :
61
- return {
62
- TAG: "Array",
63
- _0: convertType(schema._0)
64
- };
65
- case "Ref" :
66
- return {
67
- TAG: "Named",
68
- _0: CodegenHelpers.lcFirst(schema._0)
69
- };
70
- case "Enum" :
71
- return {
72
- TAG: "Enum",
73
- _0: schema._0
74
- };
75
- case "PolyVariant" :
76
- return {
77
- TAG: "InlineVariant",
78
- _0: schema._0.map(c => {
79
- let payload = convertType(c.payload);
80
- return {
81
- tag: c._tag,
82
- payload: payload
83
- };
84
- })
85
- };
86
- case "Dict" :
87
- return {
88
- TAG: "Dict",
89
- _0: convertType(schema._0)
90
- };
91
- case "Union" :
92
- return {
93
- TAG: "InlineVariant",
94
- _0: schema._0.map(t => {
95
- let tag = CodegenHelpers.getTagForType(t);
96
- let payload = convertType(t);
97
- return {
98
- tag: tag,
99
- payload: payload
100
- };
101
- })
102
- };
103
- }
104
- }
105
- }
106
-
107
- function convertField(field) {
108
- let baseType = convertType(field.type);
109
- let wrappedType = field.required || CodegenHelpers.isOptionalType(field.type) ? baseType : ({
110
- TAG: "Option",
111
- _0: baseType
112
- });
113
- let annotations = [];
114
- if (CodegenHelpers.isNullableType(field.type)) {
115
- annotations.push("SNull");
116
- }
117
- if (CodegenHelpers.isReservedKeyword(field.name)) {
118
- annotations.push({
119
- TAG: "As",
120
- _0: field.name
121
- });
122
- }
123
- let fieldName = CodegenHelpers.isReservedKeyword(field.name) ? field.name + "_" : field.name;
124
- return {
125
- name: fieldName,
126
- annotations: annotations,
127
- type_: wrappedType
128
- };
129
- }
130
-
131
- function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
132
- let typeName = CodegenHelpers.lcFirst(namedSchema.name);
133
- let tagName = Core__Option.getOr(namedSchema.discriminatorPropertyName, "_tag");
134
- let shouldSkipSchema = Core__Option.isSome(skipSchemaSet[namedSchema.name]);
135
- let cases = namedSchema.schema;
136
- if (typeof cases === "object") {
137
- switch (cases._tag) {
138
- case "PolyVariant" :
139
- let irCases = cases._0.map(c => {
140
- let refName = c.payload;
141
- let payload;
142
- if (typeof refName !== "object" || refName._tag !== "Ref") {
143
- payload = convertType(refName);
144
- } else {
145
- let refName$1 = refName._0;
146
- let other = schemasDict[refName$1];
147
- if (other !== undefined) {
148
- if (typeof other !== "object" || other._tag !== "Object") {
149
- payload = convertType(other);
150
- } else {
151
- let filtered = other._0.filter(f => f.name !== tagName);
152
- payload = {
153
- TAG: "InlineRecord",
154
- _0: filtered.map(convertField)
155
- };
156
- }
157
- } else {
158
- payload = {
159
- TAG: "Named",
160
- _0: CodegenHelpers.lcFirst(refName$1)
161
- };
162
- }
163
- }
164
- return {
165
- tag: CodegenHelpers.ucFirst(c._tag),
166
- payload: payload
167
- };
168
- });
169
- let baseAnnotations = [
170
- "GenType",
171
- {
172
- TAG: "Tag",
173
- _0: tagName
174
- }
175
- ];
176
- let annotations = shouldSkipSchema ? baseAnnotations : baseAnnotations.concat(["Schema"]);
177
- return {
178
- name: typeName,
179
- annotations: annotations,
180
- kind: {
181
- TAG: "VariantDef",
182
- _0: irCases
183
- }
184
- };
185
- case "Union" :
186
- let types = cases._0;
187
- if (CodegenHelpers.isPrimitiveOnlyUnion(types)) {
188
- let irCases$1 = types.map(t => {
189
- let tag = CodegenHelpers.getTagForType(t);
190
- let payload = convertType(t);
191
- return {
192
- tag: tag,
193
- payload: payload
194
- };
195
- });
196
- let baseAnnotations$1 = [
197
- "GenType",
198
- {
199
- TAG: "Tag",
200
- _0: tagName
201
- },
202
- "Unboxed"
203
- ];
204
- let annotations$1 = shouldSkipSchema ? baseAnnotations$1 : baseAnnotations$1.concat(["Schema"]);
205
- return {
206
- name: typeName,
207
- annotations: annotations$1,
208
- kind: {
209
- TAG: "VariantDef",
210
- _0: irCases$1
211
- }
212
- };
213
- }
214
- let irCases$2 = types.map(t => {
215
- if (typeof t === "object" && t._tag === "Ref") {
216
- let name = t._0;
217
- let tagValue = tagsDict[name];
218
- let tag = tagValue !== undefined ? CodegenHelpers.ucFirst(tagValue) : CodegenHelpers.ucFirst(name);
219
- let other = schemasDict[name];
220
- let payload;
221
- if (other !== undefined) {
222
- if (typeof other !== "object" || other._tag !== "Object") {
223
- payload = convertType(other);
224
- } else {
225
- let filtered = other._0.filter(f => f.name !== tagName);
226
- payload = {
227
- TAG: "InlineRecord",
228
- _0: filtered.map(convertField)
229
- };
230
- }
231
- } else {
232
- payload = {
233
- TAG: "Named",
234
- _0: CodegenHelpers.lcFirst(name)
235
- };
236
- }
237
- return {
238
- tag: tag,
239
- payload: payload
240
- };
241
- }
242
- let tag$1 = CodegenHelpers.getTagForType(t);
243
- let payload$1 = convertType(t);
244
- return {
245
- tag: tag$1,
246
- payload: payload$1
247
- };
248
- });
249
- let baseAnnotations$2 = [
250
- "GenType",
251
- {
252
- TAG: "Tag",
253
- _0: tagName
254
- }
255
- ];
256
- let annotations$2 = shouldSkipSchema ? baseAnnotations$2 : baseAnnotations$2.concat(["Schema"]);
257
- return {
258
- name: typeName,
259
- annotations: annotations$2,
260
- kind: {
261
- TAG: "VariantDef",
262
- _0: irCases$2
263
- }
264
- };
265
- }
266
- }
267
- let fields = namedSchema.schema;
268
- let kind;
269
- if (typeof fields !== "object" || fields._tag !== "Object") {
270
- kind = {
271
- TAG: "AliasDef",
272
- _0: convertType(namedSchema.schema)
273
- };
274
- } else {
275
- let fields$1 = fields._0;
276
- kind = fields$1.length > 0 ? ({
277
- TAG: "RecordDef",
278
- _0: fields$1.map(convertField)
279
- }) : ({
280
- TAG: "AliasDef",
281
- _0: convertType(namedSchema.schema)
282
- });
283
- }
284
- let annotations$3 = shouldSkipSchema ? ["GenType"] : [
285
- "GenType",
286
- "Schema"
287
- ];
288
- return {
289
- name: typeName,
290
- annotations: annotations$3,
291
- kind: kind
292
- };
293
- }
294
-
295
- function generate(schemas) {
296
- let validationErrors = CodegenTransforms.validateUnionDiscriminators(schemas);
297
- if (validationErrors.length > 0) {
298
- return {
299
- TAG: "Error",
300
- _0: validationErrors
301
- };
302
- }
303
- let warnings = CodegenTransforms.collectUnionWarnings(schemas);
304
- let enumOccurrences = CodegenTransforms.collectInlineEnums(schemas);
305
- let topLevelNames = schemas.map(s => s.name);
306
- let enumNames = CodegenTransforms.resolveEnumNames(enumOccurrences, topLevelNames);
307
- let enumSchemas = CodegenTransforms.buildExtractedEnumSchemas(enumOccurrences, enumNames);
308
- let schemasAfterEnumPromotion = CodegenTransforms.replaceInlineEnums(schemas, enumNames);
309
- let schemas$1 = enumSchemas.concat(schemasAfterEnumPromotion);
310
- let extractedUnions = schemas$1.flatMap(s => CodegenTransforms.extractUnions(s.name, s.schema).map(extracted => {
311
- let dict = s.fieldDiscriminators;
312
- let discriminatorPropertyName = dict !== undefined ? dict[extracted.name] : undefined;
313
- return {
314
- name: extracted.name,
315
- schema: extracted.schema,
316
- discriminatorTag: undefined,
317
- discriminatorPropertyName: discriminatorPropertyName,
318
- fieldDiscriminators: undefined
319
- };
320
- }));
321
- let seen = {};
322
- let uniqueUnions = extractedUnions.filter(u => {
323
- if (Core__Option.isSome(seen[u.name])) {
324
- return false;
325
- } else {
326
- seen[u.name] = true;
327
- return true;
328
- }
329
- });
330
- let modifiedSchemas = schemas$1.map(s => ({
331
- name: s.name,
332
- schema: CodegenTransforms.replaceUnions(s.name, s.schema),
333
- discriminatorTag: s.discriminatorTag,
334
- discriminatorPropertyName: s.discriminatorPropertyName,
335
- fieldDiscriminators: s.fieldDiscriminators
336
- }));
337
- let allSchemas = uniqueUnions.concat(modifiedSchemas);
338
- let schemasDict = {};
339
- let tagsDict = {};
340
- allSchemas.forEach(s => {
341
- schemasDict[s.name] = s.schema;
342
- let tag = s.discriminatorTag;
343
- if (tag !== undefined) {
344
- tagsDict[s.name] = tag;
345
- return;
346
- }
347
- });
348
- let skipSchemaSet = CodegenTransforms.buildSkipSchemaSet(allSchemas);
349
- let sorted = CodegenTransforms.topologicalSort(allSchemas);
350
- let irTypes = sorted.map(s => convertToIrTypeDef(s, schemasDict, tagsDict, skipSchemaSet));
351
- return {
352
- TAG: "Ok",
353
- _0: {
354
- preamble: "module S = Sury",
355
- types: irTypes,
356
- warnings: warnings
357
- }
358
- };
359
- }
360
-
361
- export {
362
- convertType,
363
- convertField,
364
- convertToIrTypeDef,
365
- generate,
366
- }
367
- /* No side effect */