osury 0.12.0 → 0.14.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 +42 -49
package/package.json
CHANGED
package/src/Codegen.res.mjs
CHANGED
|
@@ -96,9 +96,8 @@ function generateType(schema) {
|
|
|
96
96
|
} else {
|
|
97
97
|
switch (schema._tag) {
|
|
98
98
|
case "Optional" :
|
|
99
|
-
return `option<` + generateType(schema._0) + `>`;
|
|
100
99
|
case "Nullable" :
|
|
101
|
-
return `
|
|
100
|
+
return `option<` + generateType(schema._0) + `>`;
|
|
102
101
|
case "Object" :
|
|
103
102
|
return generateRecord(schema._0);
|
|
104
103
|
case "Array" :
|
|
@@ -118,12 +117,25 @@ function generateType(schema) {
|
|
|
118
117
|
}
|
|
119
118
|
}
|
|
120
119
|
|
|
121
|
-
function
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
120
|
+
function isOptionalType(schema) {
|
|
121
|
+
if (typeof schema !== "object") {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
switch (schema._tag) {
|
|
125
|
+
case "Optional" :
|
|
126
|
+
case "Nullable" :
|
|
127
|
+
return true;
|
|
128
|
+
default:
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function isNullableType(schema) {
|
|
134
|
+
if (typeof schema !== "object") {
|
|
135
|
+
return false;
|
|
136
|
+
} else {
|
|
137
|
+
return schema._tag === "Nullable";
|
|
138
|
+
}
|
|
127
139
|
}
|
|
128
140
|
|
|
129
141
|
function generateRecord(fields) {
|
|
@@ -133,22 +145,31 @@ function generateRecord(fields) {
|
|
|
133
145
|
let fieldStrs = fields.map(field => {
|
|
134
146
|
let typeStr = generateType(field.type);
|
|
135
147
|
let optionalType = field.required || isOptionalType(field.type) ? typeStr : `option<` + typeStr + `>`;
|
|
148
|
+
let finalType = isNullableType(field.type) ? `@s.null ` + optionalType : optionalType;
|
|
136
149
|
let asAttr = reservedKeywords.includes(field.name) ? `@as("` + field.name + `") ` : "";
|
|
137
150
|
let fieldName = reservedKeywords.includes(field.name) ? field.name + `_` : field.name;
|
|
138
|
-
return asAttr + fieldName + `: ` +
|
|
151
|
+
return asAttr + fieldName + `: ` + finalType;
|
|
139
152
|
});
|
|
140
153
|
return `{\n ` + fieldStrs.join(",\n ") + `\n}`;
|
|
141
154
|
}
|
|
142
155
|
|
|
143
|
-
function
|
|
144
|
-
let caseStrs =
|
|
145
|
-
let
|
|
146
|
-
|
|
147
|
-
return `#` + tag + `(` + payload + `)`;
|
|
156
|
+
function generatePolyVariant(cases) {
|
|
157
|
+
let caseStrs = cases.map(c => {
|
|
158
|
+
let payloadStr = generateType(c.payload);
|
|
159
|
+
return `#` + c._tag + `(` + payloadStr + `)`;
|
|
148
160
|
});
|
|
149
161
|
return `[` + caseStrs.join(" | ") + `]`;
|
|
150
162
|
}
|
|
151
163
|
|
|
164
|
+
function ucFirst(s) {
|
|
165
|
+
if (s.length === 0) {
|
|
166
|
+
return s;
|
|
167
|
+
}
|
|
168
|
+
let first = s.charAt(0).toUpperCase();
|
|
169
|
+
let rest = s.slice(1);
|
|
170
|
+
return first + rest;
|
|
171
|
+
}
|
|
172
|
+
|
|
152
173
|
function getTagForType(t) {
|
|
153
174
|
if (typeof t !== "object") {
|
|
154
175
|
switch (t) {
|
|
@@ -187,34 +208,13 @@ function getTagForType(t) {
|
|
|
187
208
|
}
|
|
188
209
|
}
|
|
189
210
|
|
|
190
|
-
function
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function isOptionalType(schema) {
|
|
200
|
-
if (typeof schema !== "object") {
|
|
201
|
-
return false;
|
|
202
|
-
}
|
|
203
|
-
switch (schema._tag) {
|
|
204
|
-
case "Optional" :
|
|
205
|
-
case "Nullable" :
|
|
206
|
-
return true;
|
|
207
|
-
default:
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function isNullableType(schema) {
|
|
213
|
-
if (typeof schema !== "object") {
|
|
214
|
-
return false;
|
|
215
|
-
} else {
|
|
216
|
-
return schema._tag === "Nullable";
|
|
217
|
-
}
|
|
211
|
+
function generateUnion(types) {
|
|
212
|
+
let caseStrs = types.map(t => {
|
|
213
|
+
let tag = getTagForType(t);
|
|
214
|
+
let payload = generateType(t);
|
|
215
|
+
return `#` + tag + `(` + payload + `)`;
|
|
216
|
+
});
|
|
217
|
+
return `[` + caseStrs.join(" | ") + `]`;
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
function hasUnion(_schema) {
|
|
@@ -784,12 +784,6 @@ export type t<T> = { [key: string]: T };
|
|
|
784
784
|
`;
|
|
785
785
|
}
|
|
786
786
|
|
|
787
|
-
function generateNullableShim() {
|
|
788
|
-
return `// Generated by osury - Nullable type shim for @genType
|
|
789
|
-
export type t<T> = T | null;
|
|
790
|
-
`;
|
|
791
|
-
}
|
|
792
|
-
|
|
793
787
|
export {
|
|
794
788
|
reservedKeywords,
|
|
795
789
|
isReservedKeyword,
|
|
@@ -822,6 +816,5 @@ export {
|
|
|
822
816
|
collectUnionWarnings,
|
|
823
817
|
generateModule,
|
|
824
818
|
generateDictShim,
|
|
825
|
-
generateNullableShim,
|
|
826
819
|
}
|
|
827
820
|
/* No side effect */
|