quicktype-core 23.0.20 → 23.0.22
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/language/Dart.d.ts +8 -1
- package/dist/language/Dart.js +90 -68
- package/package.json +1 -1
package/dist/language/Dart.d.ts
CHANGED
|
@@ -53,15 +53,22 @@ export declare class DartRenderer extends ConvenienceRenderer {
|
|
|
53
53
|
protected emitFileHeader(): void;
|
|
54
54
|
protected emitDescriptionBlock(lines: Sourcelike[]): void;
|
|
55
55
|
protected emitBlock(line: Sourcelike, f: () => void): void;
|
|
56
|
-
protected dartType(t: Type, withIssues?: boolean): Sourcelike;
|
|
56
|
+
protected dartType(t: Type, withIssues?: boolean, forceNullable?: boolean): Sourcelike;
|
|
57
57
|
protected mapList(isNullable: boolean, itemType: Sourcelike, list: Sourcelike, mapper: Sourcelike): Sourcelike;
|
|
58
58
|
protected mapMap(isNullable: boolean, valueType: Sourcelike, map: Sourcelike, valueMapper: Sourcelike): Sourcelike;
|
|
59
59
|
protected mapClass(isNullable: boolean, classType: ClassType, dynamic: Sourcelike): Sourcelike[];
|
|
60
60
|
protected fromDynamicExpression(isNullable: boolean | undefined, t: Type, ...dynamic: Sourcelike[]): Sourcelike;
|
|
61
61
|
protected toDynamicExpression(isNullable: boolean | undefined, t: Type, ...dynamic: Sourcelike[]): Sourcelike;
|
|
62
|
+
private _emitEmptyConstructor;
|
|
63
|
+
private _emitConstructor;
|
|
64
|
+
private _emitVariables;
|
|
65
|
+
private _emitCopyConstructor;
|
|
66
|
+
private _emitStringJsonEncoderDecoder;
|
|
67
|
+
private _emitMapEncoderDecoder;
|
|
62
68
|
protected emitClassDefinition(c: ClassType, className: Name): void;
|
|
63
69
|
protected emitFreezedClassDefinition(c: ClassType, className: Name): void;
|
|
64
70
|
protected emitEnumDefinition(e: EnumType, enumName: Name): void;
|
|
65
71
|
protected emitEnumValues(): void;
|
|
72
|
+
private _emitTopLvlEncoderDecoder;
|
|
66
73
|
protected emitSourceStructure(): void;
|
|
67
74
|
}
|
package/dist/language/Dart.js
CHANGED
|
@@ -267,8 +267,8 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
267
267
|
this.indent(f);
|
|
268
268
|
this.emitLine("}");
|
|
269
269
|
}
|
|
270
|
-
dartType(t, withIssues = false) {
|
|
271
|
-
const nullable = this._options.nullSafety && t.isNullable && !this._options.requiredProperties;
|
|
270
|
+
dartType(t, withIssues = false, forceNullable = false) {
|
|
271
|
+
const nullable = forceNullable || (this._options.nullSafety && t.isNullable && !this._options.requiredProperties);
|
|
272
272
|
const withNullable = (s) => (nullable ? [s, "?"] : s);
|
|
273
273
|
return (0, TypeUtils_1.matchType)(t, _anyType => (0, Source_1.maybeAnnotated)(withIssues, Annotation_1.anyTypeIssueAnnotation, "dynamic"), _nullType => (0, Source_1.maybeAnnotated)(withIssues, Annotation_1.nullTypeIssueAnnotation, "dynamic"), _boolType => withNullable("bool"), _integerType => withNullable("int"), _doubleType => withNullable("double"), _stringType => withNullable("String"), arrayType => withNullable(["List<", this.dartType(arrayType.items, withIssues), ">"]), classType => withNullable(this.nameForNamedType(classType)), mapType => withNullable(["Map<String, ", this.dartType(mapType.values, withIssues), ">"]), enumType => withNullable(this.nameForNamedType(enumType)), unionType => {
|
|
274
274
|
const maybeNullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
|
|
@@ -405,6 +405,76 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
405
405
|
}
|
|
406
406
|
});
|
|
407
407
|
}
|
|
408
|
+
_emitEmptyConstructor(className) {
|
|
409
|
+
this.emitLine(className, "();");
|
|
410
|
+
}
|
|
411
|
+
_emitConstructor(c, className) {
|
|
412
|
+
this.emitLine(className, "({");
|
|
413
|
+
this.indent(() => {
|
|
414
|
+
this.forEachClassProperty(c, "none", (name, _, prop) => {
|
|
415
|
+
const required = this._options.requiredProperties || (this._options.nullSafety && !prop.type.isNullable);
|
|
416
|
+
this.emitLine(required ? "required " : "", "this.", name, ",");
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
this.emitLine("});");
|
|
420
|
+
this.ensureBlankLine();
|
|
421
|
+
}
|
|
422
|
+
_emitVariables(c) {
|
|
423
|
+
this.forEachClassProperty(c, "none", (name, jsonName, p) => {
|
|
424
|
+
const description = this.descriptionForClassProperty(c, jsonName);
|
|
425
|
+
if (description !== undefined) {
|
|
426
|
+
this.emitDescription(description);
|
|
427
|
+
}
|
|
428
|
+
if (this._options.useHive) {
|
|
429
|
+
this.classPropertyCounter++;
|
|
430
|
+
this.emitLine(`@HiveField(${this.classPropertyCounter})`);
|
|
431
|
+
}
|
|
432
|
+
this.emitLine(this._options.finalProperties ? "final " : "", this.dartType(p.type, true), " ", name, ";");
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
_emitCopyConstructor(c, className) {
|
|
436
|
+
this.ensureBlankLine();
|
|
437
|
+
this.emitLine(className, " copyWith({");
|
|
438
|
+
this.indent(() => {
|
|
439
|
+
this.forEachClassProperty(c, "none", (name, _, _p) => {
|
|
440
|
+
this.emitLine(this.dartType(_p.type, true, true), " ", name, ",");
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
|
+
this.emitLine("}) => ");
|
|
444
|
+
this.indent(() => {
|
|
445
|
+
this.emitLine(className, "(");
|
|
446
|
+
this.indent(() => {
|
|
447
|
+
this.forEachClassProperty(c, "none", (name, _, _p) => {
|
|
448
|
+
this.emitLine(name, ": ", name, " ?? ", "this.", name, ",");
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
this.emitLine(");");
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
_emitStringJsonEncoderDecoder(className) {
|
|
455
|
+
this.ensureBlankLine();
|
|
456
|
+
this.emitLine("factory ", className, ".from", this._options.methodNamesWithMap ? "Json" : "RawJson", "(String str) => ", className, ".", this.fromJson, "(json.decode(str));");
|
|
457
|
+
this.ensureBlankLine();
|
|
458
|
+
this.emitLine("String ", this._options.methodNamesWithMap ? "toJson() => " : "toRawJson() => ", "json.encode(", this.toJson, "());");
|
|
459
|
+
}
|
|
460
|
+
_emitMapEncoderDecoder(c, className) {
|
|
461
|
+
this.ensureBlankLine();
|
|
462
|
+
this.emitLine("factory ", className, ".", this.fromJson, "(Map<String, dynamic> json) => ", className, "(");
|
|
463
|
+
this.indent(() => {
|
|
464
|
+
this.forEachClassProperty(c, "none", (name, jsonName, property) => {
|
|
465
|
+
this.emitLine(name, ": ", this.fromDynamicExpression(property.type.isNullable, property.type, 'json["', stringEscape(jsonName), '"]'), ",");
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
this.emitLine(");");
|
|
469
|
+
this.ensureBlankLine();
|
|
470
|
+
this.emitLine("Map<String, dynamic> ", this.toJson, "() => {");
|
|
471
|
+
this.indent(() => {
|
|
472
|
+
this.forEachClassProperty(c, "none", (name, jsonName, property) => {
|
|
473
|
+
this.emitLine('"', stringEscape(jsonName), '": ', this.toDynamicExpression(property.type.isNullable, property.type, name), ",");
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
this.emitLine("};");
|
|
477
|
+
}
|
|
408
478
|
emitClassDefinition(c, className) {
|
|
409
479
|
this.emitDescription(this.descriptionForType(c));
|
|
410
480
|
if (this._options.useHive) {
|
|
@@ -414,73 +484,22 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
414
484
|
}
|
|
415
485
|
this.emitBlock(["class ", className], () => {
|
|
416
486
|
if (c.getProperties().size === 0) {
|
|
417
|
-
this.
|
|
487
|
+
this._emitEmptyConstructor(className);
|
|
418
488
|
}
|
|
419
489
|
else {
|
|
420
|
-
this.
|
|
421
|
-
this.indent(() => {
|
|
422
|
-
this.forEachClassProperty(c, "none", (name, _, prop) => {
|
|
423
|
-
const required = this._options.requiredProperties || (this._options.nullSafety && !prop.type.isNullable);
|
|
424
|
-
this.emitLine(required ? "required " : "", "this.", name, ",");
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
this.emitLine("});");
|
|
490
|
+
this._emitVariables(c);
|
|
428
491
|
this.ensureBlankLine();
|
|
429
|
-
this.
|
|
430
|
-
const description = this.descriptionForClassProperty(c, jsonName);
|
|
431
|
-
if (description !== undefined) {
|
|
432
|
-
this.emitDescription(description);
|
|
433
|
-
}
|
|
434
|
-
if (this._options.useHive) {
|
|
435
|
-
this.classPropertyCounter++;
|
|
436
|
-
this.emitLine(`@HiveField(${this.classPropertyCounter})`);
|
|
437
|
-
}
|
|
438
|
-
this.emitLine(this._options.finalProperties ? "final " : "", this.dartType(p.type, true), " ", name, ";");
|
|
439
|
-
});
|
|
492
|
+
this._emitConstructor(c, className);
|
|
440
493
|
}
|
|
441
494
|
if (this._options.generateCopyWith) {
|
|
442
|
-
this.
|
|
443
|
-
this.emitLine(className, " copyWith({");
|
|
444
|
-
this.indent(() => {
|
|
445
|
-
this.forEachClassProperty(c, "none", (name, _, _p) => {
|
|
446
|
-
this.emitLine(this.dartType(_p.type, true), " ", name, ",");
|
|
447
|
-
});
|
|
448
|
-
});
|
|
449
|
-
this.emitLine("}) => ");
|
|
450
|
-
this.indent(() => {
|
|
451
|
-
this.emitLine(className, "(");
|
|
452
|
-
this.indent(() => {
|
|
453
|
-
this.forEachClassProperty(c, "none", (name, _, _p) => {
|
|
454
|
-
this.emitLine(name, ": ", name, " ?? ", "this.", name, ",");
|
|
455
|
-
});
|
|
456
|
-
});
|
|
457
|
-
this.emitLine(");");
|
|
458
|
-
});
|
|
495
|
+
this._emitCopyConstructor(c, className);
|
|
459
496
|
}
|
|
460
497
|
if (this._options.justTypes)
|
|
461
498
|
return;
|
|
462
499
|
if (this._options.codersInClass) {
|
|
463
|
-
this.
|
|
464
|
-
this.emitLine("factory ", className, ".from", this._options.methodNamesWithMap ? "Json" : "RawJson", "(String str) => ", className, ".", this.fromJson, "(json.decode(str));");
|
|
465
|
-
this.ensureBlankLine();
|
|
466
|
-
this.emitLine("String ", this._options.methodNamesWithMap ? "toJson() => " : "toRawJson() => ", "json.encode(", this.toJson, "());");
|
|
500
|
+
this._emitStringJsonEncoderDecoder(className);
|
|
467
501
|
}
|
|
468
|
-
this.
|
|
469
|
-
this.emitLine("factory ", className, ".", this.fromJson, "(Map<String, dynamic> json) => ", className, "(");
|
|
470
|
-
this.indent(() => {
|
|
471
|
-
this.forEachClassProperty(c, "none", (name, jsonName, property) => {
|
|
472
|
-
this.emitLine(name, ": ", this.fromDynamicExpression(property.type.isNullable, property.type, 'json["', stringEscape(jsonName), '"]'), ",");
|
|
473
|
-
});
|
|
474
|
-
});
|
|
475
|
-
this.emitLine(");");
|
|
476
|
-
this.ensureBlankLine();
|
|
477
|
-
this.emitLine("Map<String, dynamic> ", this.toJson, "() => {");
|
|
478
|
-
this.indent(() => {
|
|
479
|
-
this.forEachClassProperty(c, "none", (name, jsonName, property) => {
|
|
480
|
-
this.emitLine('"', stringEscape(jsonName), '": ', this.toDynamicExpression(property.type.isNullable, property.type, name), ",");
|
|
481
|
-
});
|
|
482
|
-
});
|
|
483
|
-
this.emitLine("};");
|
|
502
|
+
this._emitMapEncoderDecoder(c, className);
|
|
484
503
|
});
|
|
485
504
|
}
|
|
486
505
|
emitFreezedClassDefinition(c, className) {
|
|
@@ -538,18 +557,21 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
|
538
557
|
}
|
|
539
558
|
}`);
|
|
540
559
|
}
|
|
560
|
+
_emitTopLvlEncoderDecoder() {
|
|
561
|
+
this.forEachTopLevel("leading-and-interposing", (t, name) => {
|
|
562
|
+
const { encoder, decoder } = (0, Support_1.defined)(this._topLevelDependents.get(name));
|
|
563
|
+
this.emitLine(this.dartType(t), " ", decoder, "(String str) => ", this.fromDynamicExpression(t.isNullable, t, "json.decode(str)"), ";");
|
|
564
|
+
this.ensureBlankLine();
|
|
565
|
+
this.emitLine("String ", encoder, "(", this.dartType(t), " data) => json.encode(", this.toDynamicExpression(t.isNullable, t, "data"), ");");
|
|
566
|
+
// this.emitBlock(["String ", encoder, "(", this.dartType(t), " data)"], () => {
|
|
567
|
+
// this.emitJsonEncoderBlock(t);
|
|
568
|
+
// });
|
|
569
|
+
});
|
|
570
|
+
}
|
|
541
571
|
emitSourceStructure() {
|
|
542
572
|
this.emitFileHeader();
|
|
543
573
|
if (!this._options.justTypes && !this._options.codersInClass) {
|
|
544
|
-
this.
|
|
545
|
-
const { encoder, decoder } = (0, Support_1.defined)(this._topLevelDependents.get(name));
|
|
546
|
-
this.emitLine(this.dartType(t), " ", decoder, "(String str) => ", this.fromDynamicExpression(t.isNullable, t, "json.decode(str)"), ";");
|
|
547
|
-
this.ensureBlankLine();
|
|
548
|
-
this.emitLine("String ", encoder, "(", this.dartType(t), " data) => json.encode(", this.toDynamicExpression(t.isNullable, t, "data"), ");");
|
|
549
|
-
// this.emitBlock(["String ", encoder, "(", this.dartType(t), " data)"], () => {
|
|
550
|
-
// this.emitJsonEncoderBlock(t);
|
|
551
|
-
// });
|
|
552
|
-
});
|
|
574
|
+
this._emitTopLvlEncoderDecoder();
|
|
553
575
|
}
|
|
554
576
|
this.forEachNamedType("leading-and-interposing", (c, n) => this._options.useFreezed ? this.emitFreezedClassDefinition(c, n) : this.emitClassDefinition(c, n), (e, n) => this.emitEnumDefinition(e, n), (_e, _n) => {
|
|
555
577
|
// We don't support this yet.
|