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