osury 0.17.3 → 0.19.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.
@@ -0,0 +1,352 @@
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
+ return {
52
+ TAG: "InlineRecord",
53
+ _0: schema._0.map(convertField)
54
+ };
55
+ case "Array" :
56
+ return {
57
+ TAG: "Array",
58
+ _0: convertType(schema._0)
59
+ };
60
+ case "Ref" :
61
+ return {
62
+ TAG: "Named",
63
+ _0: CodegenHelpers.lcFirst(schema._0)
64
+ };
65
+ case "Enum" :
66
+ return {
67
+ TAG: "Enum",
68
+ _0: schema._0
69
+ };
70
+ case "PolyVariant" :
71
+ return {
72
+ TAG: "InlineVariant",
73
+ _0: schema._0.map(c => {
74
+ let payload = convertType(c.payload);
75
+ return {
76
+ tag: c._tag,
77
+ payload: payload
78
+ };
79
+ })
80
+ };
81
+ case "Dict" :
82
+ return {
83
+ TAG: "Dict",
84
+ _0: convertType(schema._0)
85
+ };
86
+ case "Union" :
87
+ return {
88
+ TAG: "InlineVariant",
89
+ _0: schema._0.map(t => {
90
+ let tag = CodegenHelpers.getTagForType(t);
91
+ let payload = convertType(t);
92
+ return {
93
+ tag: tag,
94
+ payload: payload
95
+ };
96
+ })
97
+ };
98
+ }
99
+ }
100
+ }
101
+
102
+ function convertField(field) {
103
+ let baseType = convertType(field.type);
104
+ let wrappedType = field.required || CodegenHelpers.isOptionalType(field.type) ? baseType : ({
105
+ TAG: "Option",
106
+ _0: baseType
107
+ });
108
+ let annotations = [];
109
+ if (CodegenHelpers.isNullableType(field.type)) {
110
+ annotations.push("SNull");
111
+ }
112
+ if (CodegenHelpers.isReservedKeyword(field.name)) {
113
+ annotations.push({
114
+ TAG: "As",
115
+ _0: field.name
116
+ });
117
+ }
118
+ let fieldName = CodegenHelpers.isReservedKeyword(field.name) ? field.name + "_" : field.name;
119
+ return {
120
+ name: fieldName,
121
+ annotations: annotations,
122
+ type_: wrappedType
123
+ };
124
+ }
125
+
126
+ function convertToIrTypeDef(namedSchema, schemasDict, tagsDict, skipSchemaSet) {
127
+ let typeName = CodegenHelpers.lcFirst(namedSchema.name);
128
+ let tagName = Core__Option.getOr(namedSchema.discriminatorPropertyName, "_tag");
129
+ let shouldSkipSchema = Core__Option.isSome(skipSchemaSet[namedSchema.name]);
130
+ let cases = namedSchema.schema;
131
+ if (typeof cases === "object") {
132
+ switch (cases._tag) {
133
+ case "PolyVariant" :
134
+ let irCases = cases._0.map(c => {
135
+ let refName = c.payload;
136
+ let payload;
137
+ if (typeof refName !== "object" || refName._tag !== "Ref") {
138
+ payload = convertType(refName);
139
+ } else {
140
+ let refName$1 = refName._0;
141
+ let other = schemasDict[refName$1];
142
+ payload = other !== undefined ? (
143
+ typeof other !== "object" || other._tag !== "Object" ? convertType(other) : ({
144
+ TAG: "InlineRecord",
145
+ _0: other._0.map(convertField)
146
+ })
147
+ ) : ({
148
+ TAG: "Named",
149
+ _0: CodegenHelpers.lcFirst(refName$1)
150
+ });
151
+ }
152
+ return {
153
+ tag: CodegenHelpers.ucFirst(c._tag),
154
+ payload: payload
155
+ };
156
+ });
157
+ let annotations = shouldSkipSchema ? [
158
+ "GenType",
159
+ {
160
+ TAG: "Tag",
161
+ _0: tagName
162
+ }
163
+ ] : [
164
+ "GenType",
165
+ {
166
+ TAG: "Tag",
167
+ _0: tagName
168
+ },
169
+ "Schema"
170
+ ];
171
+ return {
172
+ name: typeName,
173
+ annotations: annotations,
174
+ kind: {
175
+ TAG: "VariantDef",
176
+ _0: irCases
177
+ }
178
+ };
179
+ case "Union" :
180
+ let types = cases._0;
181
+ if (CodegenHelpers.isPrimitiveOnlyUnion(types)) {
182
+ let irCases$1 = types.map(t => {
183
+ let tag = CodegenHelpers.getTagForType(t);
184
+ let payload = convertType(t);
185
+ return {
186
+ tag: tag,
187
+ payload: payload
188
+ };
189
+ });
190
+ let annotations$1 = shouldSkipSchema ? [
191
+ "GenType",
192
+ {
193
+ TAG: "Tag",
194
+ _0: tagName
195
+ },
196
+ "Unboxed"
197
+ ] : [
198
+ "GenType",
199
+ {
200
+ TAG: "Tag",
201
+ _0: tagName
202
+ },
203
+ "Unboxed",
204
+ "Schema"
205
+ ];
206
+ return {
207
+ name: typeName,
208
+ annotations: annotations$1,
209
+ kind: {
210
+ TAG: "VariantDef",
211
+ _0: irCases$1
212
+ }
213
+ };
214
+ }
215
+ let irCases$2 = types.map(t => {
216
+ if (typeof t === "object" && t._tag === "Ref") {
217
+ let name = t._0;
218
+ let tagValue = tagsDict[name];
219
+ let tag = tagValue !== undefined ? CodegenHelpers.ucFirst(tagValue) : CodegenHelpers.ucFirst(name);
220
+ let other = schemasDict[name];
221
+ let payload = other !== undefined ? (
222
+ typeof other !== "object" || other._tag !== "Object" ? convertType(other) : ({
223
+ TAG: "InlineRecord",
224
+ _0: other._0.map(convertField)
225
+ })
226
+ ) : ({
227
+ TAG: "Named",
228
+ _0: CodegenHelpers.lcFirst(name)
229
+ });
230
+ return {
231
+ tag: tag,
232
+ payload: payload
233
+ };
234
+ }
235
+ let tag$1 = CodegenHelpers.getTagForType(t);
236
+ let payload$1 = convertType(t);
237
+ return {
238
+ tag: tag$1,
239
+ payload: payload$1
240
+ };
241
+ });
242
+ let annotations$2 = shouldSkipSchema ? [
243
+ "GenType",
244
+ {
245
+ TAG: "Tag",
246
+ _0: tagName
247
+ }
248
+ ] : [
249
+ "GenType",
250
+ {
251
+ TAG: "Tag",
252
+ _0: tagName
253
+ },
254
+ "Schema"
255
+ ];
256
+ return {
257
+ name: typeName,
258
+ annotations: annotations$2,
259
+ kind: {
260
+ TAG: "VariantDef",
261
+ _0: irCases$2
262
+ }
263
+ };
264
+ }
265
+ }
266
+ let fields = namedSchema.schema;
267
+ let kind;
268
+ kind = typeof fields !== "object" || fields._tag !== "Object" ? ({
269
+ TAG: "AliasDef",
270
+ _0: convertType(namedSchema.schema)
271
+ }) : ({
272
+ TAG: "RecordDef",
273
+ _0: fields._0.map(convertField)
274
+ });
275
+ let annotations$3 = shouldSkipSchema ? ["GenType"] : [
276
+ "GenType",
277
+ "Schema"
278
+ ];
279
+ return {
280
+ name: typeName,
281
+ annotations: annotations$3,
282
+ kind: kind
283
+ };
284
+ }
285
+
286
+ function generate(schemas) {
287
+ let validationErrors = CodegenTransforms.validateUnionDiscriminators(schemas);
288
+ if (validationErrors.length > 0) {
289
+ return {
290
+ TAG: "Error",
291
+ _0: validationErrors
292
+ };
293
+ }
294
+ let warnings = CodegenTransforms.collectUnionWarnings(schemas);
295
+ let extractedUnions = schemas.flatMap(s => CodegenTransforms.extractUnions(s.name, s.schema).map(extracted => {
296
+ let dict = s.fieldDiscriminators;
297
+ let discriminatorPropertyName = dict !== undefined ? dict[extracted.name] : undefined;
298
+ return {
299
+ name: extracted.name,
300
+ schema: extracted.schema,
301
+ discriminatorTag: undefined,
302
+ discriminatorPropertyName: discriminatorPropertyName,
303
+ fieldDiscriminators: undefined
304
+ };
305
+ }));
306
+ let seen = {};
307
+ let uniqueUnions = extractedUnions.filter(u => {
308
+ if (Core__Option.isSome(seen[u.name])) {
309
+ return false;
310
+ } else {
311
+ seen[u.name] = true;
312
+ return true;
313
+ }
314
+ });
315
+ let modifiedSchemas = schemas.map(s => ({
316
+ name: s.name,
317
+ schema: CodegenTransforms.replaceUnions(s.name, s.schema),
318
+ discriminatorTag: s.discriminatorTag,
319
+ discriminatorPropertyName: s.discriminatorPropertyName,
320
+ fieldDiscriminators: s.fieldDiscriminators
321
+ }));
322
+ let allSchemas = uniqueUnions.concat(modifiedSchemas);
323
+ let schemasDict = {};
324
+ let tagsDict = {};
325
+ allSchemas.forEach(s => {
326
+ schemasDict[s.name] = s.schema;
327
+ let tag = s.discriminatorTag;
328
+ if (tag !== undefined) {
329
+ tagsDict[s.name] = tag;
330
+ return;
331
+ }
332
+ });
333
+ let skipSchemaSet = CodegenTransforms.buildSkipSchemaSet(allSchemas);
334
+ let sorted = CodegenTransforms.topologicalSort(allSchemas);
335
+ let irTypes = sorted.map(s => convertToIrTypeDef(s, schemasDict, tagsDict, skipSchemaSet));
336
+ return {
337
+ TAG: "Ok",
338
+ _0: {
339
+ preamble: "module S = Sury",
340
+ types: irTypes,
341
+ warnings: warnings
342
+ }
343
+ };
344
+ }
345
+
346
+ export {
347
+ convertType,
348
+ convertField,
349
+ convertToIrTypeDef,
350
+ generate,
351
+ }
352
+ /* No side effect */
@@ -3,6 +3,7 @@
3
3
  import * as Errors from "./Errors.res.mjs";
4
4
  import * as Schema from "./Schema.res.mjs";
5
5
  import * as Core__Array from "@rescript/core/src/Core__Array.res.mjs";
6
+ import * as Core__Option from "@rescript/core/src/Core__Option.res.mjs";
6
7
 
7
8
  function pathToName(path) {
8
9
  return path.split("/").filter(s => {
@@ -92,7 +93,9 @@ function parsePathResponses(pathsJson) {
92
93
  _0: {
93
94
  name: name,
94
95
  schema: schemaType._0,
95
- discriminatorTag: undefined
96
+ discriminatorTag: undefined,
97
+ discriminatorPropertyName: undefined,
98
+ fieldDiscriminators: undefined
96
99
  }
97
100
  };
98
101
  } else {
@@ -130,6 +133,86 @@ function parsePathResponses(pathsJson) {
130
133
  };
131
134
  }
132
135
 
136
+ function extractFieldDiscriminators(schemaJson) {
137
+ let result = {};
138
+ if (typeof schemaJson === "object" && schemaJson !== null && !Array.isArray(schemaJson)) {
139
+ let match = schemaJson["properties"];
140
+ if (typeof match === "object" && match !== null && !Array.isArray(match)) {
141
+ Object.entries(match).forEach(param => {
142
+ let propJson = param[1];
143
+ if (typeof propJson !== "object" || propJson === null || Array.isArray(propJson)) {
144
+ return;
145
+ }
146
+ let match = propJson["anyOf"];
147
+ let match$1 = propJson["discriminator"];
148
+ if (match === undefined) {
149
+ return;
150
+ }
151
+ if (!Array.isArray(match)) {
152
+ return;
153
+ }
154
+ if (match$1 === undefined) {
155
+ return;
156
+ }
157
+ if (typeof match$1 !== "object" || match$1 === null || Array.isArray(match$1)) {
158
+ return;
159
+ }
160
+ let match$2 = match$1["propertyName"];
161
+ if (match$2 === undefined) {
162
+ return;
163
+ }
164
+ if (typeof match$2 !== "string") {
165
+ return;
166
+ }
167
+ let memberNames = Core__Array.filterMap(match, item => {
168
+ if (typeof item !== "object" || item === null || Array.isArray(item)) {
169
+ return;
170
+ }
171
+ let match = item["$ref"];
172
+ if (match === undefined) {
173
+ return;
174
+ }
175
+ if (typeof match !== "string") {
176
+ return;
177
+ }
178
+ let parts = match.split("/");
179
+ return parts[parts.length - 1 | 0];
180
+ });
181
+ if (memberNames.length < 2) {
182
+ return;
183
+ }
184
+ let lcNames = memberNames.map(n => {
185
+ let first = n.charAt(0).toLowerCase();
186
+ let rest = n.slice(1);
187
+ return first + rest;
188
+ });
189
+ let firstName = Core__Option.getOr(lcNames[0], "unknown");
190
+ let restNames = lcNames.slice(1);
191
+ let unionName = firstName + restNames.map(n => "Or" + ucFirst(n)).join("");
192
+ result[unionName] = match$2;
193
+ });
194
+ }
195
+ }
196
+ return result;
197
+ }
198
+
199
+ function extractDiscriminatorPropertyName(schemaJson) {
200
+ if (typeof schemaJson !== "object" || schemaJson === null || Array.isArray(schemaJson)) {
201
+ return;
202
+ }
203
+ let match = schemaJson["discriminator"];
204
+ if (match === undefined) {
205
+ return;
206
+ }
207
+ if (typeof match !== "object" || match === null || Array.isArray(match)) {
208
+ return;
209
+ }
210
+ let match$1 = match["propertyName"];
211
+ if (typeof match$1 === "string") {
212
+ return match$1;
213
+ }
214
+ }
215
+
133
216
  function extractDiscriminatorTag(schemaJson) {
134
217
  if (typeof schemaJson !== "object" || schemaJson === null || Array.isArray(schemaJson)) {
135
218
  return;
@@ -169,6 +252,9 @@ function parseComponentSchemas(componentsJson) {
169
252
  let results = entries.map(param => {
170
253
  let schemaJson = param[1];
171
254
  let discriminatorTag = extractDiscriminatorTag(schemaJson);
255
+ let discriminatorPropertyName = extractDiscriminatorPropertyName(schemaJson);
256
+ let fieldDiscs = extractFieldDiscriminators(schemaJson);
257
+ let fieldDiscriminators = Object.entries(fieldDiscs).length > 0 ? fieldDiscs : undefined;
172
258
  let schemaType = Schema.parse(schemaJson);
173
259
  if (schemaType.TAG === "Ok") {
174
260
  return {
@@ -176,7 +262,9 @@ function parseComponentSchemas(componentsJson) {
176
262
  _0: {
177
263
  name: param[0],
178
264
  schema: schemaType._0,
179
- discriminatorTag: discriminatorTag
265
+ discriminatorTag: discriminatorTag,
266
+ discriminatorPropertyName: discriminatorPropertyName,
267
+ fieldDiscriminators: fieldDiscriminators
180
268
  }
181
269
  };
182
270
  } else {
@@ -280,6 +368,8 @@ export {
280
368
  pathToName,
281
369
  ucFirst,
282
370
  parsePathResponses,
371
+ extractFieldDiscriminators,
372
+ extractDiscriminatorPropertyName,
283
373
  extractDiscriminatorTag,
284
374
  parseComponentSchemas,
285
375
  parseDocument,