quicktype-core 23.0.98 → 23.0.100

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.
@@ -670,7 +670,7 @@ function addTypesInSchema(resolver, typeBuilder, references, attributeProducers)
670
670
  const itemsLoc = loc.push("items");
671
671
  itemType = yield toType(checkJSONSchema(items, itemsLoc.canonicalRef), itemsLoc, singularAttributes);
672
672
  }
673
- else if (items !== undefined) {
673
+ else if (items !== undefined && items !== true) {
674
674
  return (0, Messages_1.messageError)("SchemaArrayItemsMustBeStringOrArray", withRef(loc, { actual: items }));
675
675
  }
676
676
  else {
@@ -5,6 +5,7 @@ import { Sourcelike } from "../Source";
5
5
  import { TargetLanguage } from "../TargetLanguage";
6
6
  import { ConvenienceRenderer } from "../ConvenienceRenderer";
7
7
  import { RenderContext } from "../Renderer";
8
+ import { StringTypeMapping } from "..";
8
9
  export declare const goOptions: {
9
10
  justTypes: BooleanOption;
10
11
  justTypesAndPackage: BooleanOption;
@@ -17,6 +18,7 @@ export declare class GoTargetLanguage extends TargetLanguage {
17
18
  constructor();
18
19
  protected getOptions(): Option<any>[];
19
20
  get supportsUnionsWithBothNumberTypes(): boolean;
21
+ get stringTypeMapping(): StringTypeMapping;
20
22
  get supportsOptionalClassProperties(): boolean;
21
23
  protected makeRenderer(renderContext: RenderContext, untypedOptionValues: {
22
24
  [name: string]: any;
@@ -48,6 +50,10 @@ export declare class GoRenderer extends ConvenienceRenderer {
48
50
  private emitUnion;
49
51
  private emitSingleFileHeaderComments;
50
52
  private emitPackageDefinitons;
53
+ private emitImports;
51
54
  private emitHelperFunctions;
52
55
  protected emitSourceStructure(): void;
56
+ private collectAllImports;
57
+ private collectClassImports;
58
+ private collectUnionImports;
53
59
  }
@@ -36,6 +36,11 @@ class GoTargetLanguage extends TargetLanguage_1.TargetLanguage {
36
36
  get supportsUnionsWithBothNumberTypes() {
37
37
  return true;
38
38
  }
39
+ get stringTypeMapping() {
40
+ const mapping = new Map();
41
+ mapping.set("date-time", "date-time");
42
+ return mapping;
43
+ }
39
44
  get supportsOptionalClassProperties() {
40
45
  return true;
41
46
  }
@@ -57,7 +62,7 @@ const primitiveValueTypeKinds = ["integer", "double", "bool", "string"];
57
62
  const compoundTypeKinds = ["array", "class", "map", "enum"];
58
63
  function isValueType(t) {
59
64
  const kind = t.kind;
60
- return primitiveValueTypeKinds.indexOf(kind) >= 0 || kind === "class" || kind === "enum";
65
+ return primitiveValueTypeKinds.indexOf(kind) >= 0 || kind === "class" || kind === "enum" || kind === "date-time";
61
66
  }
62
67
  function canOmitEmpty(cp) {
63
68
  if (!cp.isOptional)
@@ -154,6 +159,11 @@ class GoRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
154
159
  if (nullable !== null)
155
160
  return this.nullableGoType(nullable, withIssues);
156
161
  return this.nameForNamedType(unionType);
162
+ }, transformedStringType => {
163
+ if (transformedStringType.kind === "date-time") {
164
+ return "time.Time";
165
+ }
166
+ return "string";
157
167
  });
158
168
  }
159
169
  emitTopLevel(t, name) {
@@ -190,8 +200,8 @@ class GoRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
190
200
  }
191
201
  emitClass(c, className) {
192
202
  this.startFile(className);
193
- this.emitPackageDefinitons(false);
194
203
  let columns = [];
204
+ const usedTypes = new Set();
195
205
  this.forEachClassProperty(c, "none", (name, jsonName, p) => {
196
206
  const description = this.descriptionForClassProperty(c, jsonName);
197
207
  const docStrings = description !== undefined && description.length > 0 ? description.map(d => "// " + d) : [];
@@ -207,7 +217,9 @@ class GoRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
207
217
  [goType, " "],
208
218
  ["`", tags, "`"]
209
219
  ]);
220
+ usedTypes.add(goType.toString());
210
221
  });
222
+ this.emitPackageDefinitons(false, usedTypes.has("time.Time") || usedTypes.has("*,time.Time") ? new Set(["time"]) : undefined);
211
223
  this.emitDescription(this.descriptionForType(c));
212
224
  this.emitStruct(className, columns);
213
225
  this.endFile();
@@ -310,7 +322,7 @@ class GoRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
310
322
  this.emitLine("// bytes, err = ", ref, ".Marshal()");
311
323
  });
312
324
  }
313
- emitPackageDefinitons(includeJSONEncodingImport) {
325
+ emitPackageDefinitons(includeJSONEncodingImport, imports = new Set()) {
314
326
  if (!this._options.justTypes || this._options.justTypesAndPackage) {
315
327
  this.ensureBlankLine();
316
328
  const packageDeclaration = "package " + this._options.packageName;
@@ -318,25 +330,35 @@ class GoRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
318
330
  this.ensureBlankLine();
319
331
  }
320
332
  if (!this._options.justTypes && !this._options.justTypesAndPackage) {
321
- this.ensureBlankLine();
322
333
  if (this.haveNamedUnions && this._options.multiFileOutput === false) {
323
- this.emitLineOnce('import "bytes"');
324
- this.emitLineOnce('import "errors"');
334
+ imports.add("bytes");
335
+ imports.add("errors");
325
336
  }
326
337
  if (includeJSONEncodingImport) {
327
- this.emitLineOnce('import "encoding/json"');
338
+ imports.add("encoding/json");
328
339
  }
329
- this.ensureBlankLine();
330
340
  }
341
+ this.emitImports(imports);
342
+ }
343
+ emitImports(imports) {
344
+ const sortedImports = Array.from(imports).sort((a, b) => a.localeCompare(b));
345
+ if (sortedImports.length === 0) {
346
+ return;
347
+ }
348
+ sortedImports.forEach(packageName => {
349
+ this.emitLineOnce(`import "${packageName}"`);
350
+ });
351
+ this.ensureBlankLine();
331
352
  }
332
353
  emitHelperFunctions() {
333
354
  if (this.haveNamedUnions) {
334
355
  this.startFile("JSONSchemaSupport");
335
- this.emitPackageDefinitons(true);
356
+ const imports = new Set();
336
357
  if (this._options.multiFileOutput) {
337
- this.emitLineOnce('import "bytes"');
338
- this.emitLineOnce('import "errors"');
358
+ imports.add("bytes");
359
+ imports.add("errors");
339
360
  }
361
+ this.emitPackageDefinitons(true, imports);
340
362
  this.ensureBlankLine();
341
363
  this
342
364
  .emitMultiline(`func unmarshalUnion(data []byte, pi **int64, pf **float64, pb **bool, ps **string, haveArray bool, pa interface{}, haveObject bool, pc interface{}, haveMap bool, pm interface{}, haveEnum bool, pe interface{}, nullable bool) (bool, error) {
@@ -461,6 +483,7 @@ func marshalUnion(pi *int64, pf *float64, pb *bool, ps *string, haveArray bool,
461
483
  this._options.justTypesAndPackage === false &&
462
484
  this.leadingComments === undefined) {
463
485
  this.emitSingleFileHeaderComments();
486
+ this.emitPackageDefinitons(false, this.collectAllImports());
464
487
  }
465
488
  this.forEachTopLevel("leading-and-interposing", (t, name) => this.emitTopLevel(t, name), t => !(this._options.justTypes || this._options.justTypesAndPackage) ||
466
489
  this.namedTypeToNameForTopLevel(t) === undefined);
@@ -472,5 +495,54 @@ func marshalUnion(pi *int64, pf *float64, pb *bool, ps *string, haveArray bool,
472
495
  }
473
496
  this.emitHelperFunctions();
474
497
  }
498
+ collectAllImports() {
499
+ let imports = new Set();
500
+ this.forEachObject("leading-and-interposing", (c, _className) => {
501
+ const classImports = this.collectClassImports(c);
502
+ imports = new Set([...imports, ...classImports]);
503
+ });
504
+ this.forEachUnion("leading-and-interposing", (u, _unionName) => {
505
+ const unionImports = this.collectUnionImports(u);
506
+ imports = new Set([...imports, ...unionImports]);
507
+ });
508
+ return imports;
509
+ }
510
+ collectClassImports(c) {
511
+ const usedTypes = new Set();
512
+ const mapping = new Map();
513
+ mapping.set("time.Time", "time");
514
+ mapping.set("*,time.Time", "time");
515
+ this.forEachClassProperty(c, "none", (_name, _jsonName, p) => {
516
+ const goType = this.propertyGoType(p);
517
+ usedTypes.add(goType.toString());
518
+ });
519
+ const imports = new Set();
520
+ usedTypes.forEach(k => {
521
+ const typeImport = mapping.get(k);
522
+ if (typeImport) {
523
+ imports.add(typeImport);
524
+ }
525
+ });
526
+ return imports;
527
+ }
528
+ collectUnionImports(u) {
529
+ const usedTypes = new Set();
530
+ const mapping = new Map();
531
+ mapping.set("time.Time", "time");
532
+ mapping.set("*,time.Time", "time");
533
+ this.forEachUnionMember(u, null, "none", null, (_fieldName, t) => {
534
+ const goType = this.nullableGoType(t, true);
535
+ usedTypes.add(goType.toString());
536
+ });
537
+ const imports = new Set();
538
+ usedTypes.forEach(k => {
539
+ const typeImport = mapping.get(k);
540
+ if (!typeImport) {
541
+ return;
542
+ }
543
+ imports.add(typeImport);
544
+ });
545
+ return imports;
546
+ }
475
547
  }
476
548
  exports.GoRenderer = GoRenderer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quicktype-core",
3
- "version": "23.0.98",
3
+ "version": "23.0.100",
4
4
  "description": "The quicktype engine as a library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",