quicktype-core 20.0.25 → 20.0.27

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.
@@ -54,10 +54,11 @@ export declare class DartRenderer extends ConvenienceRenderer {
54
54
  protected emitDescriptionBlock(lines: Sourcelike[]): void;
55
55
  protected emitBlock(line: Sourcelike, f: () => void): void;
56
56
  protected dartType(t: Type, withIssues?: boolean): Sourcelike;
57
- protected mapList(itemType: Sourcelike, list: Sourcelike, mapper: Sourcelike): Sourcelike;
58
- protected mapMap(valueType: Sourcelike, map: Sourcelike, valueMapper: Sourcelike): Sourcelike;
59
- protected fromDynamicExpression(t: Type, ...dynamic: Sourcelike[]): Sourcelike;
60
- protected toDynamicExpression(t: Type, ...dynamic: Sourcelike[]): Sourcelike;
57
+ protected mapList(isNullable: boolean, itemType: Sourcelike, list: Sourcelike, mapper: Sourcelike): Sourcelike;
58
+ protected mapMap(isNullable: boolean, valueType: Sourcelike, map: Sourcelike, valueMapper: Sourcelike): Sourcelike;
59
+ protected mapClass(isNullable: boolean, classType: ClassType, dynamic: Sourcelike): Sourcelike[];
60
+ protected fromDynamicExpression(isNullable: boolean | undefined, t: Type, ...dynamic: Sourcelike[]): Sourcelike;
61
+ protected toDynamicExpression(isNullable: boolean | undefined, t: Type, ...dynamic: Sourcelike[]): Sourcelike;
61
62
  protected emitClassDefinition(c: ClassType, className: Name): void;
62
63
  protected emitFreezedClassDefinition(c: ClassType, className: Name): void;
63
64
  protected emitEnumDefinition(e: EnumType, enumName: Name): void;
@@ -49,7 +49,6 @@ class DartTargetLanguage extends TargetLanguage_1.TargetLanguage {
49
49
  const mapping = new Map();
50
50
  mapping.set("date", "date");
51
51
  mapping.set("date-time", "date-time");
52
- // mapping.set("uuid", "uuid");
53
52
  return mapping;
54
53
  }
55
54
  makeRenderer(renderContext, untypedOptionValues) {
@@ -269,132 +268,97 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
269
268
  this.emitLine("}");
270
269
  }
271
270
  dartType(t, withIssues = false) {
272
- 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 => {
273
- if (this._options.nullSafety) {
274
- return ["bool", "?"];
275
- }
276
- return "bool";
277
- }, _integerType => {
278
- if (this._options.nullSafety) {
279
- return ["int", "?"];
280
- }
281
- return "int";
282
- }, _doubleType => {
283
- if (this._options.nullSafety) {
284
- return ["double", "?"];
285
- }
286
- return "double";
287
- }, _stringType => {
288
- if (this._options.nullSafety) {
289
- return ["String", "?"];
290
- }
291
- return "String";
292
- }, arrayType => {
293
- if (this._options.nullSafety) {
294
- return ["List<", this.dartType(arrayType.items, withIssues), ">", "?"];
295
- }
296
- return ["List<", this.dartType(arrayType.items, withIssues), ">"];
297
- }, classType => {
298
- if (this._options.nullSafety) {
299
- return [this.nameForNamedType(classType), "?"];
300
- }
301
- return this.nameForNamedType(classType);
302
- }, mapType => {
303
- if (this._options.nullSafety) {
304
- return [["Map<String, ", this.dartType(mapType.values, withIssues), ">"], "?"];
305
- }
306
- return ["Map<String, ", this.dartType(mapType.values, withIssues), ">"];
307
- }, enumType => {
308
- if (this._options.nullSafety) {
309
- return [this.nameForNamedType(enumType), "?"];
310
- }
311
- return this.nameForNamedType(enumType);
312
- }, unionType => {
271
+ const nullable = this._options.nullSafety && t.isNullable && !this._options.requiredProperties;
272
+ const withNullable = (s) => (nullable ? [s, "?"] : s);
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 => {
313
274
  const maybeNullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
314
275
  if (maybeNullable === null) {
315
276
  return "dynamic";
316
277
  }
317
- return this.dartType(maybeNullable, withIssues);
278
+ return withNullable(this.dartType(maybeNullable, withIssues));
318
279
  }, transformedStringType => {
319
280
  switch (transformedStringType.kind) {
320
281
  case "date-time":
321
282
  case "date":
322
- return this._options.nullSafety ? ["DateTime", "?"] : "DateTime";
283
+ return withNullable("DateTime");
323
284
  default:
324
- return this._options.nullSafety ? ["String", "?"] : "String";
285
+ return withNullable("String");
325
286
  }
326
287
  });
327
288
  }
328
- mapList(itemType, list, mapper) {
329
- if (this._options.nullSafety) {
289
+ mapList(isNullable, itemType, list, mapper) {
290
+ if (this._options.nullSafety && isNullable && !this._options.requiredProperties) {
330
291
  return [list, " == null ? [] : ", "List<", itemType, ">.from(", list, "!.map((x) => ", mapper, "))"];
331
292
  }
332
293
  return ["List<", itemType, ">.from(", list, ".map((x) => ", mapper, "))"];
333
294
  }
334
- mapMap(valueType, map, valueMapper) {
335
- if (this._options.nullSafety) {
295
+ mapMap(isNullable, valueType, map, valueMapper) {
296
+ if (this._options.nullSafety && isNullable && !this._options.requiredProperties) {
336
297
  return ["Map.from(", map, "!).map((k, v) => MapEntry<String, ", valueType, ">(k, ", valueMapper, "))"];
337
298
  }
338
299
  return ["Map.from(", map, ").map((k, v) => MapEntry<String, ", valueType, ">(k, ", valueMapper, "))"];
339
300
  }
340
- fromDynamicExpression(t, ...dynamic) {
301
+ mapClass(isNullable, classType, dynamic) {
302
+ if (this._options.nullSafety && isNullable && !this._options.requiredProperties) {
303
+ return [dynamic, " == null ? null : ", this.nameForNamedType(classType), ".", this.fromJson, "(", dynamic, ")"];
304
+ }
305
+ return [this.nameForNamedType(classType), ".", this.fromJson, "(", dynamic, ")"];
306
+ }
307
+ //If the first time is the unionType type, after nullableFromUnion conversion,
308
+ //the isNullable property will become false, which is obviously wrong,
309
+ //so add isNullable property
310
+ fromDynamicExpression(isNullable = false, t, ...dynamic) {
341
311
  return (0, TypeUtils_1.matchType)(t, _anyType => dynamic, _nullType => dynamic, // FIXME: check null
342
312
  // FIXME: check null
343
- _boolType => dynamic, _integerType => dynamic, _doubleType => [dynamic, ".toDouble()"], _stringType => dynamic, arrayType => this.mapList(this.dartType(arrayType.items), dynamic, this.fromDynamicExpression(arrayType.items, "x")), classType => [this.nameForNamedType(classType), ".", this.fromJson, "(", dynamic, ")"], mapType => this.mapMap(this.dartType(mapType.values), dynamic, this.fromDynamicExpression(mapType.values, "v")), enumType => {
344
- if (this._options.nullSafety) {
345
- return [(0, Support_1.defined)(this._enumValues.get(enumType)), "!.map[", dynamic, "]"];
346
- }
347
- return [(0, Support_1.defined)(this._enumValues.get(enumType)), ".map[", dynamic, "]"];
313
+ _boolType => dynamic, _integerType => dynamic, _doubleType => [dynamic, this._options.nullSafety ? "?.toDouble()" : ".toDouble()"], _stringType => dynamic, arrayType => this.mapList(isNullable || arrayType.isNullable, this.dartType(arrayType.items), dynamic, this.fromDynamicExpression(arrayType.items.isNullable, arrayType.items, "x")), classType => this.mapClass(isNullable || classType.isNullable, classType, dynamic), mapType => this.mapMap(mapType.isNullable || isNullable, this.dartType(mapType.values), dynamic, this.fromDynamicExpression(mapType.values.isNullable, mapType.values, "v")), enumType => {
314
+ return [(0, Support_1.defined)(this._enumValues.get(enumType)), ".map[", dynamic, this._options.nullSafety ? "]!" : "]"];
348
315
  }, unionType => {
349
316
  const maybeNullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
350
317
  if (maybeNullable === null) {
351
318
  return dynamic;
352
319
  }
353
- if (maybeNullable.kind === "array") {
354
- return [dynamic, " == null ? [] : ", this.fromDynamicExpression(maybeNullable, dynamic)];
355
- }
356
- return dynamic;
320
+ return this.fromDynamicExpression(unionType.isNullable, maybeNullable, dynamic);
357
321
  }, transformedStringType => {
358
322
  switch (transformedStringType.kind) {
359
323
  case "date-time":
360
324
  case "date":
325
+ if ((transformedStringType.isNullable || isNullable) && !this._options.requiredProperties && this._options.nullSafety) {
326
+ return [dynamic, " == null ? null : ", "DateTime.parse(", dynamic, ")"];
327
+ }
361
328
  return ["DateTime.parse(", dynamic, ")"];
362
329
  default:
363
330
  return dynamic;
364
331
  }
365
332
  });
366
333
  }
367
- toDynamicExpression(t, ...dynamic) {
368
- return (0, TypeUtils_1.matchType)(t, _anyType => dynamic, _nullType => dynamic, _boolType => dynamic, _integerType => dynamic, _doubleType => dynamic, _stringType => dynamic, arrayType => this.mapList("dynamic", dynamic, this.toDynamicExpression(arrayType.items, "x")), _classType => {
369
- if (this._options.nullSafety) {
370
- return [dynamic, "!.", this.toJson, "()"];
334
+ //If the first time is the unionType type, after nullableFromUnion conversion,
335
+ //the isNullable property will become false, which is obviously wrong,
336
+ //so add isNullable property
337
+ toDynamicExpression(isNullable = false, t, ...dynamic) {
338
+ return (0, TypeUtils_1.matchType)(t, _anyType => dynamic, _nullType => dynamic, _boolType => dynamic, _integerType => dynamic, _doubleType => dynamic, _stringType => dynamic, arrayType => this.mapList(arrayType.isNullable || isNullable, "dynamic", dynamic, this.toDynamicExpression(arrayType.items.isNullable, arrayType.items, "x")), _classType => {
339
+ if (this._options.nullSafety && (_classType.isNullable || isNullable) && !this._options.requiredProperties) {
340
+ return [dynamic, "?.", this.toJson, "()"];
371
341
  }
372
342
  return [dynamic, ".", this.toJson, "()"];
373
- }, mapType => this.mapMap("dynamic", dynamic, this.toDynamicExpression(mapType.values, "v")), enumType => {
374
- if (this._options.nullSafety) {
375
- return [(0, Support_1.defined)(this._enumValues.get(enumType)), ".reverse![", dynamic, "]"];
376
- }
343
+ }, mapType => this.mapMap(mapType.isNullable || isNullable, "dynamic", dynamic, this.toDynamicExpression(mapType.values.isNullable, mapType.values, "v")), enumType => {
377
344
  return [(0, Support_1.defined)(this._enumValues.get(enumType)), ".reverse[", dynamic, "]"];
378
345
  }, unionType => {
379
346
  const maybeNullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
380
347
  if (maybeNullable === null) {
381
348
  return dynamic;
382
349
  }
383
- if (maybeNullable.kind === "array") {
384
- return [dynamic, " == null ? [] : ", this.toDynamicExpression(maybeNullable, dynamic)];
385
- }
386
- return dynamic;
350
+ return this.toDynamicExpression(unionType.isNullable, maybeNullable, dynamic);
387
351
  }, transformedStringType => {
388
352
  switch (transformedStringType.kind) {
389
353
  case "date-time":
390
- if (this._options.nullSafety) {
354
+ if (this._options.nullSafety && !this._options.requiredProperties && (transformedStringType.isNullable || isNullable)) {
391
355
  return [dynamic, "?.toIso8601String()"];
392
356
  }
393
357
  return [dynamic, ".toIso8601String()"];
394
358
  case "date":
395
- if (this._options.nullSafety) {
359
+ if (this._options.nullSafety && !this._options.requiredProperties && (transformedStringType.isNullable || isNullable)) {
396
360
  return [
397
- '"${',
361
+ "\"${",
398
362
  dynamic,
399
363
  "!.year.toString().padLeft(4, '0')",
400
364
  "}-${",
@@ -405,7 +369,7 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
405
369
  ];
406
370
  }
407
371
  return [
408
- '"${',
372
+ "\"${",
409
373
  dynamic,
410
374
  ".year.toString().padLeft(4, '0')",
411
375
  "}-${",
@@ -433,8 +397,9 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
433
397
  else {
434
398
  this.emitLine(className, "({");
435
399
  this.indent(() => {
436
- this.forEachClassProperty(c, "none", (name, _, _p) => {
437
- this.emitLine(this._options.requiredProperties ? "required " : "", "this.", name, ",");
400
+ this.forEachClassProperty(c, "none", (name, _, prop) => {
401
+ const required = this._options.requiredProperties || (this._options.nullSafety && !prop.type.isNullable);
402
+ this.emitLine(required ? "required " : "", "this.", name, ",");
438
403
  });
439
404
  });
440
405
  this.emitLine("});");
@@ -482,7 +447,7 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
482
447
  this.emitLine("factory ", className, ".", this.fromJson, "(Map<String, dynamic> json) => ", className, "(");
483
448
  this.indent(() => {
484
449
  this.forEachClassProperty(c, "none", (name, jsonName, property) => {
485
- this.emitLine(name, ": ", this.fromDynamicExpression(property.type, 'json["', stringEscape(jsonName), '"]'), ",");
450
+ this.emitLine(name, ": ", this.fromDynamicExpression(property.type.isNullable, property.type, "json[\"", stringEscape(jsonName), "\"]"), ",");
486
451
  });
487
452
  });
488
453
  this.emitLine(");");
@@ -490,7 +455,7 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
490
455
  this.emitLine("Map<String, dynamic> ", this.toJson, "() => {");
491
456
  this.indent(() => {
492
457
  this.forEachClassProperty(c, "none", (name, jsonName, property) => {
493
- this.emitLine('"', stringEscape(jsonName), '": ', this.toDynamicExpression(property.type, name), ",");
458
+ this.emitLine("\"", stringEscape(jsonName), "\": ", this.toDynamicExpression(property.type.isNullable, property.type, name), ",");
494
459
  });
495
460
  });
496
461
  this.emitLine("};");
@@ -531,7 +496,7 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
531
496
  this.indent(() => {
532
497
  this.forEachEnumCase(e, "none", (name, jsonName, pos) => {
533
498
  const comma = pos === "first" || pos === "middle" ? "," : [];
534
- this.emitLine('"', stringEscape(jsonName), '": ', enumName, ".", name, comma);
499
+ this.emitLine("\"", stringEscape(jsonName), "\": ", enumName, ".", name, comma);
535
500
  });
536
501
  });
537
502
  this.emitLine("});");
@@ -541,12 +506,12 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
541
506
  this.ensureBlankLine();
542
507
  this.emitMultiline(`class EnumValues<T> {
543
508
  Map<String, T> map;
544
- Map<T, String>? reverseMap;
509
+ late Map<T, String> reverseMap;
545
510
 
546
511
  EnumValues(this.map);
547
512
 
548
- Map<T, String>? get reverse {
549
- reverseMap ??= map.map((k, v) => MapEntry(v, k));
513
+ Map<T, String> get reverse {
514
+ reverseMap = map.map((k, v) => MapEntry(v, k));
550
515
  return reverseMap;
551
516
  }
552
517
  }`);
@@ -556,9 +521,9 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
556
521
  if (!this._options.justTypes && !this._options.codersInClass) {
557
522
  this.forEachTopLevel("leading-and-interposing", (t, name) => {
558
523
  const { encoder, decoder } = (0, Support_1.defined)(this._topLevelDependents.get(name));
559
- this.emitLine(this.dartType(t), " ", decoder, "(String str) => ", this.fromDynamicExpression(t, "json.decode(str)"), ";");
524
+ this.emitLine(this.dartType(t), " ", decoder, "(String str) => ", this.fromDynamicExpression(t.isNullable, t, "json.decode(str)"), ";");
560
525
  this.ensureBlankLine();
561
- this.emitLine("String ", encoder, "(", this.dartType(t), " data) => json.encode(", this.toDynamicExpression(t, "data"), ");");
526
+ this.emitLine("String ", encoder, "(", this.dartType(t), " data) => json.encode(", this.toDynamicExpression(t.isNullable, t, "data"), ");");
562
527
  // this.emitBlock(["String ", encoder, "(", this.dartType(t), " data)"], () => {
563
528
  // this.emitJsonEncoderBlock(t);
564
529
  // });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quicktype-core",
3
- "version": "20.0.25",
3
+ "version": "20.0.27",
4
4
  "description": "The quicktype engine as a library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",