quicktype 17.0.4 → 17.0.5
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/dist/cli/CompressedJSONFromStream.js +38 -26
- package/dist/cli/GraphQLIntrospection.js +41 -33
- package/dist/cli/TypeSource.d.ts +1 -1
- package/dist/cli/URLGrammar.js +3 -4
- package/dist/cli/index.js +364 -363
- package/dist/quicktype-core/Annotation.js +0 -2
- package/dist/quicktype-core/ConvenienceRenderer.d.ts +17 -17
- package/dist/quicktype-core/ConvenienceRenderer.js +81 -104
- package/dist/quicktype-core/DateTime.js +0 -1
- package/dist/quicktype-core/DeclarationIR.d.ts +1 -1
- package/dist/quicktype-core/DeclarationIR.js +10 -13
- package/dist/quicktype-core/EncodedMarkovChain.js +0 -1
- package/dist/quicktype-core/GatherNames.js +25 -47
- package/dist/quicktype-core/Graph.d.ts +2 -2
- package/dist/quicktype-core/Graph.js +19 -23
- package/dist/quicktype-core/GraphRewriting.d.ts +2 -2
- package/dist/quicktype-core/GraphRewriting.js +54 -65
- package/dist/quicktype-core/MakeTransformations.js +22 -23
- package/dist/quicktype-core/MarkovChain.d.ts +3 -3
- package/dist/quicktype-core/MarkovChain.js +8 -9
- package/dist/quicktype-core/Messages.d.ts +3 -3
- package/dist/quicktype-core/Messages.js +3 -8
- package/dist/quicktype-core/Naming.d.ts +11 -11
- package/dist/quicktype-core/Naming.js +61 -76
- package/dist/quicktype-core/Renderer.d.ts +6 -6
- package/dist/quicktype-core/Renderer.js +17 -34
- package/dist/quicktype-core/RendererOptions.d.ts +5 -5
- package/dist/quicktype-core/RendererOptions.js +3 -6
- package/dist/quicktype-core/Run.d.ts +9 -9
- package/dist/quicktype-core/Run.js +67 -72
- package/dist/quicktype-core/Source.d.ts +3 -3
- package/dist/quicktype-core/Source.js +18 -19
- package/dist/quicktype-core/TargetLanguage.d.ts +10 -10
- package/dist/quicktype-core/TargetLanguage.js +2 -6
- package/dist/quicktype-core/Transformers.d.ts +25 -25
- package/dist/quicktype-core/Transformers.js +66 -94
- package/dist/quicktype-core/Type.d.ts +43 -41
- package/dist/quicktype-core/Type.js +71 -96
- package/dist/quicktype-core/TypeBuilder.d.ts +2 -2
- package/dist/quicktype-core/TypeBuilder.js +47 -53
- package/dist/quicktype-core/TypeGraph.d.ts +4 -4
- package/dist/quicktype-core/TypeGraph.js +40 -49
- package/dist/quicktype-core/TypeUtils.d.ts +2 -2
- package/dist/quicktype-core/TypeUtils.js +22 -23
- package/dist/quicktype-core/UnifyClasses.js +12 -16
- package/dist/quicktype-core/UnionBuilder.d.ts +3 -3
- package/dist/quicktype-core/UnionBuilder.js +30 -33
- package/dist/quicktype-core/attributes/AccessorNames.d.ts +2 -2
- package/dist/quicktype-core/attributes/AccessorNames.js +17 -18
- package/dist/quicktype-core/attributes/Constraints.d.ts +3 -3
- package/dist/quicktype-core/attributes/Constraints.js +4 -8
- package/dist/quicktype-core/attributes/Description.js +5 -6
- package/dist/quicktype-core/attributes/EnumValues.js +3 -4
- package/dist/quicktype-core/attributes/StringTypes.d.ts +1 -1
- package/dist/quicktype-core/attributes/StringTypes.js +21 -24
- package/dist/quicktype-core/attributes/TypeAttributes.d.ts +3 -3
- package/dist/quicktype-core/attributes/TypeAttributes.js +10 -12
- package/dist/quicktype-core/attributes/TypeNames.d.ts +9 -9
- package/dist/quicktype-core/attributes/TypeNames.js +21 -49
- package/dist/quicktype-core/attributes/URIAttributes.d.ts +1 -1
- package/dist/quicktype-core/attributes/URIAttributes.js +6 -10
- package/dist/quicktype-core/index.js +125 -128
- package/dist/quicktype-core/input/CompressedJSON.d.ts +7 -7
- package/dist/quicktype-core/input/CompressedJSON.js +54 -49
- package/dist/quicktype-core/input/FetchingJSONSchemaStore.js +13 -5
- package/dist/quicktype-core/input/Inference.d.ts +2 -2
- package/dist/quicktype-core/input/Inference.js +17 -25
- package/dist/quicktype-core/input/Inputs.d.ts +2 -2
- package/dist/quicktype-core/input/Inputs.js +54 -38
- package/dist/quicktype-core/input/JSONSchemaInput.d.ts +11 -11
- package/dist/quicktype-core/input/JSONSchemaInput.js +523 -503
- package/dist/quicktype-core/input/JSONSchemaStore.d.ts +1 -1
- package/dist/quicktype-core/input/JSONSchemaStore.js +35 -24
- package/dist/quicktype-core/input/PostmanCollection.js +1 -2
- package/dist/quicktype-core/input/io/NodeIO.js +44 -55
- package/dist/quicktype-core/input/io/get-stream/index.js +2 -6
- package/dist/quicktype-core/language/All.js +2 -3
- package/dist/quicktype-core/language/CPlusPlus.d.ts +8 -8
- package/dist/quicktype-core/language/CPlusPlus.js +151 -185
- package/dist/quicktype-core/language/CSharp.d.ts +7 -7
- package/dist/quicktype-core/language/CSharp.js +72 -82
- package/dist/quicktype-core/language/Crystal.d.ts +2 -2
- package/dist/quicktype-core/language/Crystal.js +17 -18
- package/dist/quicktype-core/language/Dart.d.ts +4 -4
- package/dist/quicktype-core/language/Dart.js +34 -36
- package/dist/quicktype-core/language/Elm.d.ts +6 -6
- package/dist/quicktype-core/language/Elm.js +45 -47
- package/dist/quicktype-core/language/Golang.d.ts +4 -4
- package/dist/quicktype-core/language/Golang.js +20 -23
- package/dist/quicktype-core/language/Haskell.d.ts +5 -5
- package/dist/quicktype-core/language/Haskell.js +23 -25
- package/dist/quicktype-core/language/JSONSchema.d.ts +3 -3
- package/dist/quicktype-core/language/JSONSchema.js +16 -17
- package/dist/quicktype-core/language/Java.d.ts +2 -2
- package/dist/quicktype-core/language/Java.js +97 -97
- package/dist/quicktype-core/language/JavaScript.d.ts +7 -7
- package/dist/quicktype-core/language/JavaScript.js +17 -19
- package/dist/quicktype-core/language/JavaScriptPropTypes.js +13 -15
- package/dist/quicktype-core/language/JavaScriptUnicodeMaps.js +11 -12
- package/dist/quicktype-core/language/Kotlin.d.ts +2 -2
- package/dist/quicktype-core/language/Kotlin.js +37 -39
- package/dist/quicktype-core/language/Objective-C.d.ts +3 -3
- package/dist/quicktype-core/language/Objective-C.js +41 -45
- package/dist/quicktype-core/language/Php.d.ts +2 -2
- package/dist/quicktype-core/language/Php.js +41 -66
- package/dist/quicktype-core/language/Pike.d.ts +1 -1
- package/dist/quicktype-core/language/Pike.js +17 -18
- package/dist/quicktype-core/language/Python.d.ts +8 -8
- package/dist/quicktype-core/language/Python.js +64 -63
- package/dist/quicktype-core/language/Rust.d.ts +2 -2
- package/dist/quicktype-core/language/Rust.js +19 -21
- package/dist/quicktype-core/language/Swift.d.ts +7 -7
- package/dist/quicktype-core/language/Swift.js +804 -807
- package/dist/quicktype-core/language/TypeScriptFlow.d.ts +7 -7
- package/dist/quicktype-core/language/TypeScriptFlow.js +26 -28
- package/dist/quicktype-core/language/ruby/index.d.ts +4 -4
- package/dist/quicktype-core/language/ruby/index.js +24 -49
- package/dist/quicktype-core/language/ruby/keywords.js +0 -1
- package/dist/quicktype-core/rewrites/CombineClasses.js +8 -9
- package/dist/quicktype-core/rewrites/ExpandStrings.d.ts +1 -1
- package/dist/quicktype-core/rewrites/ExpandStrings.js +17 -18
- package/dist/quicktype-core/rewrites/FlattenStrings.js +6 -7
- package/dist/quicktype-core/rewrites/FlattenUnions.js +8 -9
- package/dist/quicktype-core/rewrites/InferMaps.js +10 -11
- package/dist/quicktype-core/rewrites/ReplaceObjectType.js +7 -8
- package/dist/quicktype-core/rewrites/ResolveIntersections.js +67 -68
- package/dist/quicktype-core/support/Acronyms.js +1 -3
- package/dist/quicktype-core/support/Chance.js +0 -10
- package/dist/quicktype-core/support/Converters.js +0 -1
- package/dist/quicktype-core/support/Strings.d.ts +4 -4
- package/dist/quicktype-core/support/Strings.js +16 -17
- package/dist/quicktype-core/support/Support.d.ts +1 -1
- package/dist/quicktype-core/support/Support.js +6 -30
- package/dist/quicktype-graphql-input/GraphQLSchema.d.ts +4 -4
- package/dist/quicktype-graphql-input/GraphQLSchema.js +0 -1
- package/dist/quicktype-graphql-input/index.d.ts +1 -1
- package/dist/quicktype-graphql-input/index.js +212 -229
- package/dist/quicktype-typescript-input/index.js +4 -28
- package/package.json +19 -25
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JSONPythonRenderer = exports.PythonRenderer = exports.PythonTargetLanguage = exports.pythonOptions = void 0;
|
|
4
3
|
const TargetLanguage_1 = require("../TargetLanguage");
|
|
5
4
|
const Type_1 = require("../Type");
|
|
6
5
|
const RendererOptions_1 = require("../RendererOptions");
|
|
@@ -103,12 +102,12 @@ class PythonTargetLanguage extends TargetLanguage_1.TargetLanguage {
|
|
|
103
102
|
}
|
|
104
103
|
needsTransformerForType(t) {
|
|
105
104
|
if (t instanceof Type_1.UnionType) {
|
|
106
|
-
return
|
|
105
|
+
return collection_utils_1.iterableSome(t.members, m => this.needsTransformerForType(m));
|
|
107
106
|
}
|
|
108
107
|
return t.kind === "integer-string" || t.kind === "bool-string";
|
|
109
108
|
}
|
|
110
109
|
makeRenderer(renderContext, untypedOptionValues) {
|
|
111
|
-
const options =
|
|
110
|
+
const options = RendererOptions_1.getOptionValues(exports.pythonOptions, untypedOptionValues);
|
|
112
111
|
if (options.justTypes) {
|
|
113
112
|
return new PythonRenderer(this, renderContext, options);
|
|
114
113
|
}
|
|
@@ -150,10 +149,10 @@ function isPartCharacter3(utf16Unit) {
|
|
|
150
149
|
}
|
|
151
150
|
return true;
|
|
152
151
|
}
|
|
153
|
-
const legalizeName3 =
|
|
152
|
+
const legalizeName3 = Strings_1.utf16LegalizeCharacters(isPartCharacter3);
|
|
154
153
|
function classNameStyle(original) {
|
|
155
|
-
const words =
|
|
156
|
-
return
|
|
154
|
+
const words = Strings_1.splitIntoWords(original);
|
|
155
|
+
return Strings_1.combineWords(words, legalizeName3, Strings_1.firstUpperWordStyle, Strings_1.firstUpperWordStyle, Strings_1.allUpperWordStyle, Strings_1.allUpperWordStyle, "", isStartCharacter3);
|
|
157
156
|
}
|
|
158
157
|
function getWordStyle(uppercase, forceSnakeNameStyle) {
|
|
159
158
|
if (!forceSnakeNameStyle) {
|
|
@@ -164,16 +163,15 @@ function getWordStyle(uppercase, forceSnakeNameStyle) {
|
|
|
164
163
|
function snakeNameStyle(original, uppercase, forceSnakeNameStyle) {
|
|
165
164
|
const wordStyle = getWordStyle(uppercase, forceSnakeNameStyle);
|
|
166
165
|
const separator = forceSnakeNameStyle ? "_" : "";
|
|
167
|
-
const words =
|
|
168
|
-
return
|
|
166
|
+
const words = Strings_1.splitIntoWords(original);
|
|
167
|
+
return Strings_1.combineWords(words, legalizeName3, wordStyle, wordStyle, wordStyle, wordStyle, separator, isStartCharacter3);
|
|
169
168
|
}
|
|
170
169
|
class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
171
|
-
pyOptions;
|
|
172
|
-
imports = new Map();
|
|
173
|
-
declaredTypes = new Set();
|
|
174
170
|
constructor(targetLanguage, renderContext, pyOptions) {
|
|
175
171
|
super(targetLanguage, renderContext);
|
|
176
172
|
this.pyOptions = pyOptions;
|
|
173
|
+
this.imports = new Map();
|
|
174
|
+
this.declaredTypes = new Set();
|
|
177
175
|
}
|
|
178
176
|
forbiddenNamesForGlobalNamespace() {
|
|
179
177
|
return forbiddenTypeNames;
|
|
@@ -182,16 +180,16 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
182
180
|
return { names: forbiddenPropertyNames, includeGlobalForbidden: false };
|
|
183
181
|
}
|
|
184
182
|
makeNamedTypeNamer() {
|
|
185
|
-
return
|
|
183
|
+
return Naming_1.funPrefixNamer("type", classNameStyle);
|
|
186
184
|
}
|
|
187
185
|
namerForObjectProperty() {
|
|
188
|
-
return
|
|
186
|
+
return Naming_1.funPrefixNamer("property", s => snakeNameStyle(s, false, this.pyOptions.nicePropertyNames));
|
|
189
187
|
}
|
|
190
188
|
makeUnionMemberNamer() {
|
|
191
189
|
return null;
|
|
192
190
|
}
|
|
193
191
|
makeEnumCaseNamer() {
|
|
194
|
-
return
|
|
192
|
+
return Naming_1.funPrefixNamer("enum-case", s => snakeNameStyle(s, true, this.pyOptions.nicePropertyNames));
|
|
195
193
|
}
|
|
196
194
|
get commentLineStart() {
|
|
197
195
|
return "# ";
|
|
@@ -217,7 +215,7 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
217
215
|
}
|
|
218
216
|
string(s) {
|
|
219
217
|
const openQuote = '"';
|
|
220
|
-
return [openQuote,
|
|
218
|
+
return [openQuote, Strings_1.stringEscape(s), '"'];
|
|
221
219
|
}
|
|
222
220
|
withImport(module, name) {
|
|
223
221
|
if (this.pyOptions.features.typeHints || module !== "typing") {
|
|
@@ -225,7 +223,7 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
225
223
|
// place, but right now we just make the type source and then throw it away. It's
|
|
226
224
|
// not a performance issue, so it's fine, I just bemoan this special case, and
|
|
227
225
|
// potential others down the road.
|
|
228
|
-
|
|
226
|
+
collection_utils_1.mapUpdateInto(this.imports, module, s => (s ? collection_utils_1.setUnionInto(s, [name]) : new Set([name])));
|
|
229
227
|
}
|
|
230
228
|
return name;
|
|
231
229
|
}
|
|
@@ -239,9 +237,9 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
239
237
|
return ["'", name, "'"];
|
|
240
238
|
}
|
|
241
239
|
pythonType(t) {
|
|
242
|
-
const actualType =
|
|
243
|
-
return
|
|
244
|
-
const [hasNull, nonNulls] =
|
|
240
|
+
const actualType = Transformers_1.followTargetType(t);
|
|
241
|
+
return TypeUtils_1.matchType(actualType, _anyType => this.withTyping("Any"), _nullType => "None", _boolType => "bool", _integerType => "int", _doubletype => "float", _stringType => "str", arrayType => [this.withTyping("List"), "[", this.pythonType(arrayType.items), "]"], classType => this.namedType(classType), mapType => [this.withTyping("Dict"), "[str, ", this.pythonType(mapType.values), "]"], enumType => this.namedType(enumType), unionType => {
|
|
242
|
+
const [hasNull, nonNulls] = TypeUtils_1.removeNullFromUnion(unionType);
|
|
245
243
|
const memberTypes = Array.from(nonNulls).map(m => this.pythonType(m));
|
|
246
244
|
if (hasNull !== null) {
|
|
247
245
|
let rest = [];
|
|
@@ -252,17 +250,17 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
252
250
|
return [
|
|
253
251
|
this.withTyping("Optional"),
|
|
254
252
|
"[Union[",
|
|
255
|
-
|
|
253
|
+
collection_utils_1.arrayIntercalate(", ", memberTypes),
|
|
256
254
|
"]]",
|
|
257
255
|
...rest
|
|
258
256
|
];
|
|
259
257
|
}
|
|
260
258
|
else {
|
|
261
|
-
return [this.withTyping("Optional"), "[",
|
|
259
|
+
return [this.withTyping("Optional"), "[", Support_1.defined(collection_utils_1.iterableFirst(memberTypes)), "]", ...rest];
|
|
262
260
|
}
|
|
263
261
|
}
|
|
264
262
|
else {
|
|
265
|
-
return [this.withTyping("Union"), "[",
|
|
263
|
+
return [this.withTyping("Union"), "[", collection_utils_1.arrayIntercalate(", ", memberTypes), "]"];
|
|
266
264
|
}
|
|
267
265
|
}, transformedStringType => {
|
|
268
266
|
if (transformedStringType.kind === "date-time") {
|
|
@@ -271,7 +269,7 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
271
269
|
if (transformedStringType.kind === "uuid") {
|
|
272
270
|
return this.withImport("uuid", "UUID");
|
|
273
271
|
}
|
|
274
|
-
return
|
|
272
|
+
return Support_1.panic(`Transformed type ${transformedStringType.kind} not supported`);
|
|
275
273
|
});
|
|
276
274
|
}
|
|
277
275
|
declarationLine(t) {
|
|
@@ -281,7 +279,7 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
281
279
|
if (t instanceof Type_1.EnumType) {
|
|
282
280
|
return ["class ", this.nameForNamedType(t), "(", this.withImport("enum", "Enum"), "):"];
|
|
283
281
|
}
|
|
284
|
-
return
|
|
282
|
+
return Support_1.panic(`Can't declare type ${t.kind}`);
|
|
285
283
|
}
|
|
286
284
|
declareType(t, emitter) {
|
|
287
285
|
this.emitBlock(this.declarationLine(t), () => {
|
|
@@ -297,7 +295,7 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
297
295
|
this.forEachClassProperty(t, "none", (name, _, cp) => {
|
|
298
296
|
args.push([name, this.typeHint(": ", this.pythonType(cp.type))]);
|
|
299
297
|
});
|
|
300
|
-
this.emitBlock(["def __init__(self, ",
|
|
298
|
+
this.emitBlock(["def __init__(self, ", collection_utils_1.arrayIntercalate(", ", args), ")", this.typeHint(" -> None"), ":"], () => {
|
|
301
299
|
if (args.length === 0) {
|
|
302
300
|
this.emitLine("pass");
|
|
303
301
|
}
|
|
@@ -322,8 +320,8 @@ class PythonRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
322
320
|
}
|
|
323
321
|
sortClassProperties(properties, propertyNames) {
|
|
324
322
|
if (this.pyOptions.features.dataClasses) {
|
|
325
|
-
return
|
|
326
|
-
return p.type instanceof Type_1.UnionType &&
|
|
323
|
+
return collection_utils_1.mapSortBy(properties, (p) => {
|
|
324
|
+
return p.type instanceof Type_1.UnionType && TypeUtils_1.nullableFromUnion(p.type) != null ? 1 : 0;
|
|
327
325
|
});
|
|
328
326
|
}
|
|
329
327
|
else {
|
|
@@ -398,13 +396,13 @@ function compose(input, f) {
|
|
|
398
396
|
}
|
|
399
397
|
if (input.lambda !== undefined) {
|
|
400
398
|
// `input` is a lambda, so build `lambda x: f(input(x))`.
|
|
401
|
-
return { lambda:
|
|
399
|
+
return { lambda: Source_1.multiWord(" ", "lambda x:", f([Source_1.parenIfNeeded(input.lambda), "(x)"])), value: undefined };
|
|
402
400
|
}
|
|
403
401
|
// `input` is the identify function, so the composition is `lambda x: f(x)`.
|
|
404
|
-
return { lambda:
|
|
402
|
+
return { lambda: Source_1.multiWord(" ", "lambda x:", f("x")), value: undefined };
|
|
405
403
|
}
|
|
406
404
|
if (f.value !== undefined) {
|
|
407
|
-
return
|
|
405
|
+
return Support_1.panic("Cannot compose into a value");
|
|
408
406
|
}
|
|
409
407
|
if (f.lambda === undefined) {
|
|
410
408
|
// `f` is the identity function, so the result is just `input`.
|
|
@@ -418,7 +416,7 @@ function compose(input, f) {
|
|
|
418
416
|
}
|
|
419
417
|
// `input` is a lambda, so the result is `lambda x: f(input(x))`.
|
|
420
418
|
return {
|
|
421
|
-
lambda:
|
|
419
|
+
lambda: Source_1.multiWord("", "lambda x: ", Source_1.parenIfNeeded(f.lambda), "(", Source_1.parenIfNeeded(input.lambda), "(x))"),
|
|
422
420
|
value: undefined
|
|
423
421
|
};
|
|
424
422
|
}
|
|
@@ -433,31 +431,34 @@ function makeLambda(vol) {
|
|
|
433
431
|
if (vol.value === undefined) {
|
|
434
432
|
return vol.lambda;
|
|
435
433
|
}
|
|
436
|
-
return
|
|
434
|
+
return Source_1.multiWord("", "lambda x: ", Source_1.parenIfNeeded(vol.lambda), "(", vol.value, ")");
|
|
437
435
|
}
|
|
438
436
|
else if (vol.value !== undefined) {
|
|
439
|
-
return
|
|
437
|
+
return Source_1.multiWord(" ", "lambda x:", vol.value);
|
|
440
438
|
}
|
|
441
|
-
return
|
|
439
|
+
return Source_1.multiWord(" ", "lambda x:", "x");
|
|
442
440
|
}
|
|
443
441
|
// If `vol` is a value, return the value in its source form.
|
|
444
442
|
// Calling this with `vol` being a lambda is not allowed.
|
|
445
443
|
function makeValue(vol) {
|
|
446
444
|
if (vol.value === undefined) {
|
|
447
|
-
return
|
|
445
|
+
return Support_1.panic("Cannot make value from lambda without value");
|
|
448
446
|
}
|
|
449
447
|
if (vol.lambda !== undefined) {
|
|
450
|
-
return [
|
|
448
|
+
return [Source_1.parenIfNeeded(vol.lambda), "(", vol.value, ")"];
|
|
451
449
|
}
|
|
452
450
|
return vol.value;
|
|
453
451
|
}
|
|
454
452
|
class JSONPythonRenderer extends PythonRenderer {
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
453
|
+
constructor() {
|
|
454
|
+
super(...arguments);
|
|
455
|
+
this._deserializerFunctions = new Set();
|
|
456
|
+
this._converterNamer = Naming_1.funPrefixNamer("converter", s => snakeNameStyle(s, false, this.pyOptions.nicePropertyNames));
|
|
457
|
+
this._topLevelConverterNames = new Map();
|
|
458
|
+
this._haveTypeVar = false;
|
|
459
|
+
this._haveEnumTypeVar = false;
|
|
460
|
+
this._haveDateutil = false;
|
|
461
|
+
}
|
|
461
462
|
emitTypeVar(tvar, constraints) {
|
|
462
463
|
if (!this.pyOptions.features.typeHints) {
|
|
463
464
|
return;
|
|
@@ -663,7 +664,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
663
664
|
case "is-type":
|
|
664
665
|
return this.emitIsTypeConverter();
|
|
665
666
|
default:
|
|
666
|
-
return
|
|
667
|
+
return Support_1.assertNever(cf);
|
|
667
668
|
}
|
|
668
669
|
}
|
|
669
670
|
// Return the name of the Python converter function `cf`.
|
|
@@ -676,10 +677,10 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
676
677
|
}
|
|
677
678
|
// Applies the converter function to `arg`
|
|
678
679
|
convFn(cf, arg) {
|
|
679
|
-
return compose(arg, { lambda:
|
|
680
|
+
return compose(arg, { lambda: Source_1.singleWord(this.conv(cf)), value: undefined });
|
|
680
681
|
}
|
|
681
682
|
typeObject(t) {
|
|
682
|
-
const s =
|
|
683
|
+
const s = TypeUtils_1.matchType(t, _anyType => undefined, _nullType => "type(None)", _boolType => "bool", _integerType => "int", _doubleType => "float", _stringType => "str", _arrayType => "List", classType => this.nameForNamedType(classType), _mapType => "dict", enumType => this.nameForNamedType(enumType), _unionType => undefined, transformedStringType => {
|
|
683
684
|
if (transformedStringType.kind === "date-time") {
|
|
684
685
|
return this.withImport("datetime", "datetime");
|
|
685
686
|
}
|
|
@@ -689,7 +690,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
689
690
|
return undefined;
|
|
690
691
|
});
|
|
691
692
|
if (s === undefined) {
|
|
692
|
-
return
|
|
693
|
+
return Support_1.panic(`No type object for ${t.kind}`);
|
|
693
694
|
}
|
|
694
695
|
return s;
|
|
695
696
|
}
|
|
@@ -708,7 +709,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
708
709
|
return compose(inputTransformer, v => [
|
|
709
710
|
this.conv("union"),
|
|
710
711
|
"([",
|
|
711
|
-
|
|
712
|
+
collection_utils_1.arrayIntercalate(", ", lambdas),
|
|
712
713
|
"], ",
|
|
713
714
|
v,
|
|
714
715
|
")"
|
|
@@ -751,7 +752,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
751
752
|
vol = compose(inputTransformer, v => [this.withImport("uuid", "UUID"), "(", v, ")"]);
|
|
752
753
|
break;
|
|
753
754
|
default:
|
|
754
|
-
return
|
|
755
|
+
return Support_1.panic(`Parsing of ${immediateTargetType.kind} in a transformer is not supported`);
|
|
755
756
|
}
|
|
756
757
|
return consume(consumer, vol);
|
|
757
758
|
}
|
|
@@ -775,23 +776,23 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
775
776
|
vol = compose(inputTransformer, v => ["str(", v, ")"]);
|
|
776
777
|
break;
|
|
777
778
|
default:
|
|
778
|
-
return
|
|
779
|
+
return Support_1.panic(`Parsing of ${xfer.sourceType.kind} in a transformer is not supported`);
|
|
779
780
|
}
|
|
780
781
|
return consume(consumer, vol);
|
|
781
782
|
}
|
|
782
783
|
else {
|
|
783
|
-
return
|
|
784
|
+
return Support_1.panic(`Transformer ${xfer.kind} is not supported`);
|
|
784
785
|
}
|
|
785
786
|
}
|
|
786
787
|
// Returns the code to deserialize `value` as type `t`. If `t` has
|
|
787
788
|
// an associated transformer, the code for that transformer is
|
|
788
789
|
// returned.
|
|
789
790
|
deserializer(value, t) {
|
|
790
|
-
const xf =
|
|
791
|
+
const xf = Transformers_1.transformationForType(t);
|
|
791
792
|
if (xf !== undefined) {
|
|
792
793
|
return this.transformer(value, xf.transformer, xf.targetType);
|
|
793
794
|
}
|
|
794
|
-
return
|
|
795
|
+
return TypeUtils_1.matchType(t, _anyType => value, _nullType => this.convFn("none", value), _boolType => this.convFn("bool", value), _integerType => this.convFn("int", value), _doubleType => this.convFn("from-float", value), _stringType => this.convFn("str", value), arrayType => compose(value, v => [
|
|
795
796
|
this.conv("list"),
|
|
796
797
|
"(",
|
|
797
798
|
makeLambda(this.deserializer(identity, arrayType.items)).source,
|
|
@@ -799,7 +800,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
799
800
|
v,
|
|
800
801
|
")"
|
|
801
802
|
]), classType => compose(value, {
|
|
802
|
-
lambda:
|
|
803
|
+
lambda: Source_1.singleWord(this.nameForNamedType(classType), ".from_dict"),
|
|
803
804
|
value: undefined
|
|
804
805
|
}), mapType => compose(value, v => [
|
|
805
806
|
this.conv("dict"),
|
|
@@ -808,13 +809,13 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
808
809
|
", ",
|
|
809
810
|
v,
|
|
810
811
|
")"
|
|
811
|
-
]), enumType => compose(value, { lambda:
|
|
812
|
+
]), enumType => compose(value, { lambda: Source_1.singleWord(this.nameForNamedType(enumType)), value: undefined }), unionType => {
|
|
812
813
|
// FIXME: handle via transformers
|
|
813
814
|
const deserializers = Array.from(unionType.members).map(m => makeLambda(this.deserializer(identity, m)).source);
|
|
814
815
|
return compose(value, v => [
|
|
815
816
|
this.conv("union"),
|
|
816
817
|
"([",
|
|
817
|
-
|
|
818
|
+
collection_utils_1.arrayIntercalate(", ", deserializers),
|
|
818
819
|
"], ",
|
|
819
820
|
v,
|
|
820
821
|
")"
|
|
@@ -827,16 +828,16 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
827
828
|
if (transformedStringType.kind === "uuid") {
|
|
828
829
|
return compose(value, v => [this.withImport("uuid", "UUID"), "(", v, ")"]);
|
|
829
830
|
}
|
|
830
|
-
return
|
|
831
|
+
return Support_1.panic(`Transformed type ${transformedStringType.kind} not supported`);
|
|
831
832
|
});
|
|
832
833
|
}
|
|
833
834
|
serializer(value, t) {
|
|
834
|
-
const xf =
|
|
835
|
+
const xf = Transformers_1.transformationForType(t);
|
|
835
836
|
if (xf !== undefined) {
|
|
836
837
|
const reverse = xf.reverse;
|
|
837
838
|
return this.transformer(value, reverse.transformer, reverse.targetType);
|
|
838
839
|
}
|
|
839
|
-
return
|
|
840
|
+
return TypeUtils_1.matchType(t, _anyType => value, _nullType => this.convFn("none", value), _boolType => this.convFn("bool", value), _integerType => this.convFn("int", value), _doubleType => this.convFn("to-float", value), _stringType => this.convFn("str", value), arrayType => compose(value, v => [
|
|
840
841
|
this.conv("list"),
|
|
841
842
|
"(",
|
|
842
843
|
makeLambda(this.serializer(identity, arrayType.items)).source,
|
|
@@ -855,7 +856,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
855
856
|
return compose(value, v => [
|
|
856
857
|
this.conv("union"),
|
|
857
858
|
"([",
|
|
858
|
-
|
|
859
|
+
collection_utils_1.arrayIntercalate(", ", serializers),
|
|
859
860
|
"], ",
|
|
860
861
|
v,
|
|
861
862
|
")"
|
|
@@ -867,7 +868,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
867
868
|
if (transformedStringType.kind === "uuid") {
|
|
868
869
|
return compose(value, v => ["str(", v, ")"]);
|
|
869
870
|
}
|
|
870
|
-
return
|
|
871
|
+
return Support_1.panic(`Transformed type ${transformedStringType.kind} not supported`);
|
|
871
872
|
});
|
|
872
873
|
}
|
|
873
874
|
emitClassMembers(t) {
|
|
@@ -883,7 +884,7 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
883
884
|
this.emitLine(name, " = ", makeValue(this.deserializer(property, cp.type)));
|
|
884
885
|
args.push(name);
|
|
885
886
|
});
|
|
886
|
-
this.emitLine("return ", className, "(",
|
|
887
|
+
this.emitLine("return ", className, "(", collection_utils_1.arrayIntercalate(", ", args), ")");
|
|
887
888
|
});
|
|
888
889
|
this.ensureBlankLine();
|
|
889
890
|
this.emitBlock(["def to_dict(self)", this.typeHint(" -> dict"), ":"], () => {
|
|
@@ -948,13 +949,13 @@ class JSONPythonRenderer extends PythonRenderer {
|
|
|
948
949
|
""
|
|
949
950
|
]);
|
|
950
951
|
this.forEachTopLevel("none", (_, name) => {
|
|
951
|
-
const { fromDict } =
|
|
952
|
+
const { fromDict } = Support_1.defined(this._topLevelConverterNames.get(name));
|
|
952
953
|
this.emitLine(this.commentLineStart, " result = ", fromDict, "(json.loads(json_string))");
|
|
953
954
|
});
|
|
954
955
|
}
|
|
955
956
|
emitClosingCode() {
|
|
956
957
|
this.forEachTopLevel(["interposing", 2], (t, name) => {
|
|
957
|
-
const { fromDict, toDict } =
|
|
958
|
+
const { fromDict, toDict } = Support_1.defined(this._topLevelConverterNames.get(name));
|
|
958
959
|
const pythonType = this.pythonType(t);
|
|
959
960
|
this.emitBlock(["def ", fromDict, "(", this.typingDecl("s", "Any"), ")", this.typeHint(" -> ", pythonType), ":"], () => {
|
|
960
961
|
this.emitLine("return ", makeValue(this.deserializer({ value: "s" }, t)));
|
|
@@ -39,13 +39,13 @@ export declare class RustRenderer extends ConvenienceRenderer {
|
|
|
39
39
|
protected forbiddenForObjectProperties(_c: ClassType, _className: Name): ForbiddenWordsInfo;
|
|
40
40
|
protected forbiddenForUnionMembers(_u: UnionType, _unionName: Name): ForbiddenWordsInfo;
|
|
41
41
|
protected forbiddenForEnumCases(_e: EnumType, _enumName: Name): ForbiddenWordsInfo;
|
|
42
|
-
protected
|
|
42
|
+
protected readonly commentLineStart: string;
|
|
43
43
|
private nullableRustType;
|
|
44
44
|
protected isImplicitCycleBreaker(t: Type): boolean;
|
|
45
45
|
private rustType;
|
|
46
46
|
private breakCycle;
|
|
47
47
|
private emitRenameAttribute;
|
|
48
|
-
private
|
|
48
|
+
private readonly visibility;
|
|
49
49
|
protected emitStructDefinition(c: ClassType, className: Name): void;
|
|
50
50
|
protected emitBlock(line: Sourcelike, f: () => void): void;
|
|
51
51
|
protected emitUnion(u: UnionType, unionName: Name): void;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RustRenderer = exports.RustTargetLanguage = exports.rustOptions = exports.Visibility = exports.Density = void 0;
|
|
4
3
|
const collection_utils_1 = require("collection-utils");
|
|
5
4
|
const TargetLanguage_1 = require("../TargetLanguage");
|
|
6
5
|
const ConvenienceRenderer_1 = require("../ConvenienceRenderer");
|
|
@@ -38,7 +37,7 @@ exports.rustOptions = {
|
|
|
38
37
|
};
|
|
39
38
|
class RustTargetLanguage extends TargetLanguage_1.TargetLanguage {
|
|
40
39
|
makeRenderer(renderContext, untypedOptionValues) {
|
|
41
|
-
return new RustRenderer(this, renderContext,
|
|
40
|
+
return new RustRenderer(this, renderContext, RendererOptions_1.getOptionValues(exports.rustOptions, untypedOptionValues));
|
|
42
41
|
}
|
|
43
42
|
constructor() {
|
|
44
43
|
super("Rust", ["rust", "rs", "rustlang"], "rs");
|
|
@@ -121,37 +120,36 @@ const keywords = [
|
|
|
121
120
|
"union"
|
|
122
121
|
];
|
|
123
122
|
const isAsciiLetterOrUnderscoreOrDigit = (codePoint) => {
|
|
124
|
-
if (!
|
|
123
|
+
if (!Strings_1.isAscii(codePoint)) {
|
|
125
124
|
return false;
|
|
126
125
|
}
|
|
127
|
-
return
|
|
126
|
+
return Strings_1.isLetterOrUnderscoreOrDigit(codePoint);
|
|
128
127
|
};
|
|
129
128
|
const isAsciiLetterOrUnderscore = (codePoint) => {
|
|
130
|
-
if (!
|
|
129
|
+
if (!Strings_1.isAscii(codePoint)) {
|
|
131
130
|
return false;
|
|
132
131
|
}
|
|
133
|
-
return
|
|
132
|
+
return Strings_1.isLetterOrUnderscore(codePoint);
|
|
134
133
|
};
|
|
135
|
-
const legalizeName =
|
|
134
|
+
const legalizeName = Strings_1.legalizeCharacters(isAsciiLetterOrUnderscoreOrDigit);
|
|
136
135
|
function rustStyle(original, isSnakeCase) {
|
|
137
|
-
const words =
|
|
136
|
+
const words = Strings_1.splitIntoWords(original);
|
|
138
137
|
const wordStyle = isSnakeCase ? Strings_1.allLowerWordStyle : Strings_1.firstUpperWordStyle;
|
|
139
|
-
const combined =
|
|
138
|
+
const combined = Strings_1.combineWords(words, legalizeName, wordStyle, wordStyle, wordStyle, wordStyle, isSnakeCase ? "_" : "", isAsciiLetterOrUnderscore);
|
|
140
139
|
return combined === "_" ? "_underscore" : combined;
|
|
141
140
|
}
|
|
142
|
-
const snakeNamingFunction =
|
|
143
|
-
const camelNamingFunction =
|
|
141
|
+
const snakeNamingFunction = Naming_1.funPrefixNamer("default", (original) => rustStyle(original, true));
|
|
142
|
+
const camelNamingFunction = Naming_1.funPrefixNamer("camel", (original) => rustStyle(original, false));
|
|
144
143
|
const standardUnicodeRustEscape = (codePoint) => {
|
|
145
144
|
if (codePoint <= 0xffff) {
|
|
146
|
-
return "\\u{" +
|
|
145
|
+
return "\\u{" + Strings_1.intToHex(codePoint, 4) + "}";
|
|
147
146
|
}
|
|
148
147
|
else {
|
|
149
|
-
return "\\u{" +
|
|
148
|
+
return "\\u{" + Strings_1.intToHex(codePoint, 6) + "}";
|
|
150
149
|
}
|
|
151
150
|
};
|
|
152
|
-
const rustStringEscape =
|
|
151
|
+
const rustStringEscape = Strings_1.utf32ConcatMap(Strings_1.escapeNonPrintableMapper(Strings_1.isPrintable, standardUnicodeRustEscape));
|
|
153
152
|
class RustRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
154
|
-
_options;
|
|
155
153
|
constructor(targetLanguage, renderContext, _options) {
|
|
156
154
|
super(targetLanguage, renderContext);
|
|
157
155
|
this._options = _options;
|
|
@@ -191,11 +189,11 @@ class RustRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
191
189
|
return kind === "array" || kind === "map";
|
|
192
190
|
}
|
|
193
191
|
rustType(t, withIssues = false) {
|
|
194
|
-
return
|
|
195
|
-
const nullable =
|
|
192
|
+
return TypeUtils_1.matchType(t, _anyType => Source_1.maybeAnnotated(withIssues, Annotation_1.anyTypeIssueAnnotation, "Option<serde_json::Value>"), _nullType => Source_1.maybeAnnotated(withIssues, Annotation_1.nullTypeIssueAnnotation, "Option<serde_json::Value>"), _boolType => "bool", _integerType => "i64", _doubleType => "f64", _stringType => "String", arrayType => ["Vec<", this.rustType(arrayType.items, withIssues), ">"], classType => this.nameForNamedType(classType), mapType => ["HashMap<String, ", this.rustType(mapType.values, withIssues), ">"], enumType => this.nameForNamedType(enumType), unionType => {
|
|
193
|
+
const nullable = TypeUtils_1.nullableFromUnion(unionType);
|
|
196
194
|
if (nullable !== null)
|
|
197
195
|
return this.nullableRustType(nullable, withIssues);
|
|
198
|
-
const [hasNull] =
|
|
196
|
+
const [hasNull] = TypeUtils_1.removeNullFromUnion(unionType);
|
|
199
197
|
const isCycleBreaker = this.isCycleBreakerType(unionType);
|
|
200
198
|
const name = isCycleBreaker
|
|
201
199
|
? ["Box<", this.nameForNamedType(unionType), ">"]
|
|
@@ -241,14 +239,14 @@ class RustRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
241
239
|
this.emitLine("}");
|
|
242
240
|
}
|
|
243
241
|
emitUnion(u, unionName) {
|
|
244
|
-
const isMaybeWithSingleType =
|
|
242
|
+
const isMaybeWithSingleType = TypeUtils_1.nullableFromUnion(u);
|
|
245
243
|
if (isMaybeWithSingleType !== null) {
|
|
246
244
|
return;
|
|
247
245
|
}
|
|
248
246
|
this.emitDescription(this.descriptionForType(u));
|
|
249
247
|
this.emitLine("#[derive(", this._options.deriveDebug ? "Debug, " : "", "Serialize, Deserialize)]");
|
|
250
248
|
this.emitLine("#[serde(untagged)]");
|
|
251
|
-
const [, nonNulls] =
|
|
249
|
+
const [, nonNulls] = TypeUtils_1.removeNullFromUnion(u);
|
|
252
250
|
const blankLines = this._options.density === Density.Dense ? "none" : "interposing";
|
|
253
251
|
this.emitBlock(["pub enum ", unionName], () => this.forEachUnionMember(u, nonNulls, blankLines, null, (fieldName, t) => {
|
|
254
252
|
const rustType = this.breakCycle(t, true);
|
|
@@ -272,7 +270,7 @@ class RustRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
272
270
|
this.emitCommentLines(this.leadingComments);
|
|
273
271
|
return;
|
|
274
272
|
}
|
|
275
|
-
const topLevelName =
|
|
273
|
+
const topLevelName = Support_1.defined(collection_utils_1.mapFirst(this.topLevels)).getCombinedName();
|
|
276
274
|
this.emitMultiline(`// Example code that deserializes and serializes the model.
|
|
277
275
|
// extern crate serde;
|
|
278
276
|
// #[macro_use]
|
|
@@ -38,13 +38,13 @@ export interface SwiftProperty {
|
|
|
38
38
|
export declare class SwiftTargetLanguage extends TargetLanguage {
|
|
39
39
|
constructor();
|
|
40
40
|
protected getOptions(): Option<any>[];
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
readonly stringTypeMapping: StringTypeMapping;
|
|
42
|
+
readonly supportsOptionalClassProperties: boolean;
|
|
43
|
+
readonly supportsUnionsWithBothNumberTypes: boolean;
|
|
44
44
|
protected makeRenderer(renderContext: RenderContext, untypedOptionValues: {
|
|
45
45
|
[name: string]: any;
|
|
46
46
|
}): SwiftRenderer;
|
|
47
|
-
|
|
47
|
+
readonly dateTimeRecognizer: DateTimeRecognizer;
|
|
48
48
|
}
|
|
49
49
|
export declare class SwiftRenderer extends ConvenienceRenderer {
|
|
50
50
|
private readonly _options;
|
|
@@ -65,7 +65,7 @@ export declare class SwiftRenderer extends ConvenienceRenderer {
|
|
|
65
65
|
private emitBlock;
|
|
66
66
|
private emitBlockWithAccess;
|
|
67
67
|
private justTypesCase;
|
|
68
|
-
private
|
|
68
|
+
private readonly lowerNamingFunction;
|
|
69
69
|
protected swiftPropertyType(p: ClassProperty): Sourcelike;
|
|
70
70
|
protected swiftType(t: Type, withIssues?: boolean, noOptional?: boolean): Sourcelike;
|
|
71
71
|
protected proposedUnionMemberNameForTypeKind(kind: TypeKind): string | null;
|
|
@@ -75,8 +75,8 @@ export declare class SwiftRenderer extends ConvenienceRenderer {
|
|
|
75
75
|
protected getProtocolsArray(_t: Type, isClass: boolean): string[];
|
|
76
76
|
private getProtocolString;
|
|
77
77
|
private getEnumPropertyGroups;
|
|
78
|
-
private
|
|
79
|
-
private
|
|
78
|
+
private readonly accessLevel;
|
|
79
|
+
private readonly objcMembersDeclaration;
|
|
80
80
|
protected startFile(basename: Sourcelike): void;
|
|
81
81
|
protected endFile(): void;
|
|
82
82
|
protected propertyLinesDefinition(name: Name, parameter: ClassProperty): Sourcelike;
|