quicktype-core 7.0.1 → 7.0.3

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.
@@ -1,12 +1,13 @@
1
- import { Type, EnumType, UnionType, ClassType, ClassProperty } from "../Type";
1
+ import { ClassProperty, ClassType, EnumType, Type, UnionType } from "../Type";
2
2
  import { Sourcelike } from "../Source";
3
3
  import { StringTypeMapping } from "../TypeBuilder";
4
- import { Name, Namer, DependencyName } from "../Naming";
4
+ import { DependencyName, Name, Namer } from "../Naming";
5
5
  import { ConvenienceRenderer, ForbiddenWordsInfo } from "../ConvenienceRenderer";
6
6
  import { TargetLanguage } from "../TargetLanguage";
7
- import { Option, BooleanOption, OptionValues, StringOption } from "../RendererOptions";
7
+ import { BooleanOption, Option, OptionValues, StringOption } from "../RendererOptions";
8
8
  import { RenderContext } from "../Renderer";
9
9
  export declare const dartOptions: {
10
+ nullSafety: BooleanOption;
10
11
  justTypes: BooleanOption;
11
12
  codersInClass: BooleanOption;
12
13
  methodNamesWithMap: BooleanOption;
@@ -12,6 +12,7 @@ const Annotation_1 = require("../Annotation");
12
12
  const Support_1 = require("../support/Support");
13
13
  const collection_utils_1 = require("collection-utils");
14
14
  exports.dartOptions = {
15
+ nullSafety: new RendererOptions_1.BooleanOption("null-safety", "Null Safety", true),
15
16
  justTypes: new RendererOptions_1.BooleanOption("just-types", "Types only", false),
16
17
  codersInClass: new RendererOptions_1.BooleanOption("coders-in-class", "Put encoder & decoder in Class", false),
17
18
  methodNamesWithMap: new RendererOptions_1.BooleanOption("from-map", "Use method names fromMap() & toMap()", false),
@@ -258,7 +259,7 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
258
259
  }
259
260
  }
260
261
  emitDescriptionBlock(lines) {
261
- this.emitCommentLines(lines, " * ", "/**", " */");
262
+ this.emitCommentLines(lines, "///", "");
262
263
  }
263
264
  emitBlock(line, f) {
264
265
  this.emitLine(line, " {");
@@ -266,7 +267,47 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
266
267
  this.emitLine("}");
267
268
  }
268
269
  dartType(t, withIssues = false) {
269
- return TypeUtils_1.matchType(t, _anyType => Source_1.maybeAnnotated(withIssues, Annotation_1.anyTypeIssueAnnotation, "dynamic"), _nullType => Source_1.maybeAnnotated(withIssues, Annotation_1.nullTypeIssueAnnotation, "dynamic"), _boolType => "bool", _integerType => "int", _doubleType => "double", _stringType => "String", arrayType => ["List<", this.dartType(arrayType.items, withIssues), ">"], classType => this.nameForNamedType(classType), mapType => ["Map<String, ", this.dartType(mapType.values, withIssues), ">"], enumType => this.nameForNamedType(enumType), unionType => {
270
+ return TypeUtils_1.matchType(t, _anyType => Source_1.maybeAnnotated(withIssues, Annotation_1.anyTypeIssueAnnotation, "dynamic"), _nullType => Source_1.maybeAnnotated(withIssues, Annotation_1.nullTypeIssueAnnotation, "dynamic"), _boolType => {
271
+ if (this._options.nullSafety) {
272
+ return ["bool", "?"];
273
+ }
274
+ return "bool";
275
+ }, _integerType => {
276
+ if (this._options.nullSafety) {
277
+ return ["int", "?"];
278
+ }
279
+ return "int";
280
+ }, _doubleType => {
281
+ if (this._options.nullSafety) {
282
+ return ["double", "?"];
283
+ }
284
+ return "double";
285
+ }, _stringType => {
286
+ if (this._options.nullSafety) {
287
+ return ["String", "?"];
288
+ }
289
+ return "String";
290
+ }, arrayType => {
291
+ if (this._options.nullSafety) {
292
+ return ["List<", this.dartType(arrayType.items, withIssues), ">", "?"];
293
+ }
294
+ return ["List<", this.dartType(arrayType.items, withIssues), ">"];
295
+ }, classType => {
296
+ if (this._options.nullSafety) {
297
+ return [this.nameForNamedType(classType), "?"];
298
+ }
299
+ return this.nameForNamedType(classType);
300
+ }, mapType => {
301
+ if (this._options.nullSafety) {
302
+ return [["Map<String, ", this.dartType(mapType.values, withIssues), ">"], "?"];
303
+ }
304
+ return ["Map<String, ", this.dartType(mapType.values, withIssues), ">"];
305
+ }, enumType => {
306
+ if (this._options.nullSafety) {
307
+ return [this.nameForNamedType(enumType), "?"];
308
+ }
309
+ return this.nameForNamedType(enumType);
310
+ }, unionType => {
270
311
  const maybeNullable = TypeUtils_1.nullableFromUnion(unionType);
271
312
  if (maybeNullable === null) {
272
313
  return "dynamic";
@@ -276,27 +317,41 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
276
317
  switch (transformedStringType.kind) {
277
318
  case "date-time":
278
319
  case "date":
279
- return "DateTime";
320
+ return this._options.nullSafety ? ["DateTime", "?"] : "DateTime";
280
321
  default:
281
- return "String";
322
+ return this._options.nullSafety ? ["String", "?"] : "String";
282
323
  }
283
324
  });
284
325
  }
285
326
  mapList(itemType, list, mapper) {
327
+ if (this._options.nullSafety) {
328
+ return [list, " == null ? [] : ", "List<", itemType, ">.from(", list, "!.map((x) => ", mapper, "))"];
329
+ }
286
330
  return ["List<", itemType, ">.from(", list, ".map((x) => ", mapper, "))"];
287
331
  }
288
332
  mapMap(valueType, map, valueMapper) {
333
+ if (this._options.nullSafety) {
334
+ return ["Map.from(", map, "!).map((k, v) => MapEntry<String, ", valueType, ">(k, ", valueMapper, "))"];
335
+ }
289
336
  return ["Map.from(", map, ").map((k, v) => MapEntry<String, ", valueType, ">(k, ", valueMapper, "))"];
290
337
  }
291
338
  fromDynamicExpression(t, ...dynamic) {
292
339
  return TypeUtils_1.matchType(t, _anyType => dynamic, _nullType => dynamic, // FIXME: check null
293
340
  // FIXME: check null
294
- _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 => [Support_1.defined(this._enumValues.get(enumType)), ".map[", dynamic, "]"], unionType => {
341
+ _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 => {
342
+ if (this._options.nullSafety) {
343
+ return [Support_1.defined(this._enumValues.get(enumType)), "!.map[", dynamic, "]"];
344
+ }
345
+ return [Support_1.defined(this._enumValues.get(enumType)), ".map[", dynamic, "]"];
346
+ }, unionType => {
295
347
  const maybeNullable = TypeUtils_1.nullableFromUnion(unionType);
296
348
  if (maybeNullable === null) {
297
349
  return dynamic;
298
350
  }
299
- return [dynamic, " == null ? null : ", this.fromDynamicExpression(maybeNullable, dynamic)];
351
+ if (maybeNullable.kind === "array") {
352
+ return [dynamic, " == null ? [] : ", this.fromDynamicExpression(maybeNullable, dynamic)];
353
+ }
354
+ return dynamic;
300
355
  }, transformedStringType => {
301
356
  switch (transformedStringType.kind) {
302
357
  case "date-time":
@@ -308,17 +363,45 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
308
363
  });
309
364
  }
310
365
  toDynamicExpression(t, ...dynamic) {
311
- return 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 => [dynamic, ".", this.toJson, "()"], mapType => this.mapMap("dynamic", dynamic, this.toDynamicExpression(mapType.values, "v")), enumType => [Support_1.defined(this._enumValues.get(enumType)), ".reverse[", dynamic, "]"], unionType => {
366
+ return 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 => {
367
+ if (this._options.nullSafety) {
368
+ return [dynamic, "!.", this.toJson, "()"];
369
+ }
370
+ return [dynamic, ".", this.toJson, "()"];
371
+ }, mapType => this.mapMap("dynamic", dynamic, this.toDynamicExpression(mapType.values, "v")), enumType => {
372
+ if (this._options.nullSafety) {
373
+ return [Support_1.defined(this._enumValues.get(enumType)), ".reverse![", dynamic, "]"];
374
+ }
375
+ return [Support_1.defined(this._enumValues.get(enumType)), ".reverse[", dynamic, "]"];
376
+ }, unionType => {
312
377
  const maybeNullable = TypeUtils_1.nullableFromUnion(unionType);
313
378
  if (maybeNullable === null) {
314
379
  return dynamic;
315
380
  }
316
- return [dynamic, " == null ? null : ", this.toDynamicExpression(maybeNullable, dynamic)];
381
+ if (maybeNullable.kind === "array") {
382
+ return [dynamic, " == null ? [] : ", this.toDynamicExpression(maybeNullable, dynamic)];
383
+ }
384
+ return dynamic;
317
385
  }, transformedStringType => {
318
386
  switch (transformedStringType.kind) {
319
387
  case "date-time":
388
+ if (this._options.nullSafety) {
389
+ return [dynamic, "?.toIso8601String()"];
390
+ }
320
391
  return [dynamic, ".toIso8601String()"];
321
392
  case "date":
393
+ if (this._options.nullSafety) {
394
+ return [
395
+ '"${',
396
+ dynamic,
397
+ "!.year.toString().padLeft(4, '0')",
398
+ "}-${",
399
+ dynamic,
400
+ "!.month.toString().padLeft(2, '0')}-${",
401
+ dynamic,
402
+ "!.day.toString().padLeft(2, '0')}\""
403
+ ];
404
+ }
322
405
  return [
323
406
  '"${',
324
407
  dynamic,
@@ -456,14 +539,12 @@ class DartRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
456
539
  this.ensureBlankLine();
457
540
  this.emitMultiline(`class EnumValues<T> {
458
541
  Map<String, T> map;
459
- Map<T, String> reverseMap;
542
+ Map<T, String>? reverseMap;
460
543
 
461
544
  EnumValues(this.map);
462
545
 
463
- Map<T, String> get reverse {
464
- if (reverseMap == null) {
465
- reverseMap = map.map((k, v) => new MapEntry(v, k));
466
- }
546
+ Map<T, String>? get reverse {
547
+ reverseMap ??= map.map((k, v) => MapEntry(v, k));
467
548
  return reverseMap;
468
549
  }
469
550
  }`);
@@ -134,7 +134,6 @@ const keywords = [
134
134
  "continue",
135
135
  "default",
136
136
  "defer",
137
- "description",
138
137
  "do",
139
138
  "else",
140
139
  "fallthrough",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quicktype-core",
3
- "version": "7.0.1",
3
+ "version": "7.0.3",
4
4
  "description": "The quicktype engine as a library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",
@@ -45,6 +45,6 @@
45
45
  "fs": false
46
46
  },
47
47
  "config": {
48
- "commit": "dd3f28ff1786d83fa386cfc2dc776dec4f2f91ba"
48
+ "commit": "90a6bc16e854c337955ea453fe36a6d7fcd17c99"
49
49
  }
50
50
  }