quicktype-core 23.0.108 → 23.0.110
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,5 +1,5 @@
|
|
|
1
|
-
import { ClassProperty, Type } from "../Type";
|
|
2
|
-
import { Namer } from "../Naming";
|
|
1
|
+
import { ClassProperty, ObjectType, Type } from "../Type";
|
|
2
|
+
import { Name, Namer } from "../Naming";
|
|
3
3
|
import { RenderContext } from "../Renderer";
|
|
4
4
|
import { BooleanOption, Option, OptionValues } from "../RendererOptions";
|
|
5
5
|
import { TargetLanguage } from "../TargetLanguage";
|
|
@@ -27,9 +27,11 @@ export declare class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer
|
|
|
27
27
|
private importStatement;
|
|
28
28
|
protected emitImports(): void;
|
|
29
29
|
typeMapTypeForProperty(p: ClassProperty): Sourcelike;
|
|
30
|
+
emittedObjects: Set<Name>;
|
|
30
31
|
typeMapTypeFor(t: Type, required?: boolean): Sourcelike;
|
|
31
32
|
private emitObject;
|
|
32
33
|
private emitEnum;
|
|
34
|
+
protected walkObjectNames(type: ObjectType): Name[];
|
|
33
35
|
protected emitSchemas(): void;
|
|
34
36
|
protected emitSourceStructure(): void;
|
|
35
37
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TypeScriptEffectSchemaRenderer = exports.TypeScriptEffectSchemaTargetLanguage = exports.typeScriptEffectSchemaOptions = void 0;
|
|
4
4
|
const collection_utils_1 = require("collection-utils");
|
|
5
|
+
const Type_1 = require("../Type");
|
|
5
6
|
const TypeUtils_1 = require("../TypeUtils");
|
|
6
7
|
const Naming_1 = require("../Naming");
|
|
7
8
|
const RendererOptions_1 = require("../RendererOptions");
|
|
@@ -30,6 +31,7 @@ class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer_1.ConvenienceRe
|
|
|
30
31
|
constructor(targetLanguage, renderContext, _options) {
|
|
31
32
|
super(targetLanguage, renderContext);
|
|
32
33
|
this._options = _options;
|
|
34
|
+
this.emittedObjects = new Set();
|
|
33
35
|
}
|
|
34
36
|
forbiddenNamesForGlobalNamespace() {
|
|
35
37
|
return ["Class", "Date", "Object", "String", "Array", "JSON", "Error"];
|
|
@@ -63,8 +65,12 @@ class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer_1.ConvenienceRe
|
|
|
63
65
|
return p.isOptional ? ["S.optional(", typeMap, ")"] : typeMap;
|
|
64
66
|
}
|
|
65
67
|
typeMapTypeFor(t, required = true) {
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
+
if (t.kind === "class" || t.kind === "object" || t.kind === "enum") {
|
|
69
|
+
const name = this.nameForNamedType(t);
|
|
70
|
+
if (this.emittedObjects.has(name)) {
|
|
71
|
+
return [name];
|
|
72
|
+
}
|
|
73
|
+
return ["S.suspend(() => ", name, ")"];
|
|
68
74
|
}
|
|
69
75
|
const match = (0, TypeUtils_1.matchType)(t, _anyType => "S.any", _nullType => "S.null", _boolType => "S.boolean", _integerType => "S.number", _doubleType => "S.number", _stringType => "S.string", arrayType => ["S.array(", this.typeMapTypeFor(arrayType.items, false), ")"], _classType => (0, Support_1.panic)("Should already be handled."), _mapType => ["S.record(S.string, ", this.typeMapTypeFor(_mapType.values, false), ")"], _enumType => (0, Support_1.panic)("Should already be handled."), unionType => {
|
|
70
76
|
const children = Array.from(unionType.getChildren()).map((type) => this.typeMapTypeFor(type, false));
|
|
@@ -78,37 +84,55 @@ class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer_1.ConvenienceRe
|
|
|
78
84
|
return match;
|
|
79
85
|
}
|
|
80
86
|
emitObject(name, t) {
|
|
87
|
+
this.emittedObjects.add(name);
|
|
81
88
|
this.ensureBlankLine();
|
|
82
|
-
|
|
83
|
-
this.emitLine("\nexport const ", name, " = S.struct({");
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
this.emitLine("\nconst ", name, "_ = S.struct({");
|
|
87
|
-
}
|
|
89
|
+
this.emitLine("\nexport class ", name, " extends S.Class<", name, '>("', name, '")({');
|
|
88
90
|
this.indent(() => {
|
|
89
91
|
this.forEachClassProperty(t, "none", (_, jsonName, property) => {
|
|
90
92
|
this.emitLine(`"${(0, Strings_1.utf16StringEscape)(jsonName)}"`, ": ", this.typeMapTypeForProperty(property), ",");
|
|
91
93
|
});
|
|
92
94
|
});
|
|
93
|
-
this.emitLine("})
|
|
94
|
-
if (!this._options.justSchema) {
|
|
95
|
-
this.emitLine("export interface ", name, " extends S.Schema.To<typeof ", name, "_> {}");
|
|
96
|
-
this.emitLine("export const ", name, ": S.Schema<S.Schema.From<typeof ", name, "_>, ", name, "> = ", name, "_;");
|
|
97
|
-
}
|
|
95
|
+
this.emitLine("}) {}");
|
|
98
96
|
}
|
|
99
97
|
emitEnum(e, enumName) {
|
|
98
|
+
this.emittedObjects.add(enumName);
|
|
100
99
|
this.ensureBlankLine();
|
|
101
100
|
this.emitDescription(this.descriptionForType(e));
|
|
102
|
-
this.emitLine("\nexport const ", enumName, " = ", "S.
|
|
101
|
+
this.emitLine("\nexport const ", enumName, " = ", "S.literal(");
|
|
103
102
|
this.indent(() => this.forEachEnumCase(e, "none", (_, jsonName) => {
|
|
104
|
-
|
|
105
|
-
this.emitLine('"', name, '": "', name, '",');
|
|
103
|
+
this.emitLine('"', (0, Strings_1.stringEscape)(jsonName), '",');
|
|
106
104
|
}));
|
|
107
|
-
this.emitLine("
|
|
105
|
+
this.emitLine(");");
|
|
108
106
|
if (!this._options.justSchema) {
|
|
109
|
-
this.emitLine("export type ", enumName, " = S.Schema.
|
|
107
|
+
this.emitLine("export type ", enumName, " = S.Schema.Type<typeof ", enumName, ">;");
|
|
110
108
|
}
|
|
111
109
|
}
|
|
110
|
+
walkObjectNames(type) {
|
|
111
|
+
const names = [];
|
|
112
|
+
const recurse = (type) => {
|
|
113
|
+
if (type.kind === "object" || type.kind === "class") {
|
|
114
|
+
names.push(this.nameForNamedType(type));
|
|
115
|
+
this.forEachClassProperty(type, "none", (_, __, prop) => {
|
|
116
|
+
recurse(prop.type);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
else if (type instanceof Type_1.ArrayType) {
|
|
120
|
+
recurse(type.items);
|
|
121
|
+
}
|
|
122
|
+
else if (type instanceof Type_1.MapType) {
|
|
123
|
+
recurse(type.values);
|
|
124
|
+
}
|
|
125
|
+
else if (type instanceof Type_1.EnumType) {
|
|
126
|
+
for (const t of type.getChildren()) {
|
|
127
|
+
recurse(t);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
this.forEachClassProperty(type, "none", (_, __, prop) => {
|
|
132
|
+
recurse(prop.type);
|
|
133
|
+
});
|
|
134
|
+
return names;
|
|
135
|
+
}
|
|
112
136
|
emitSchemas() {
|
|
113
137
|
this.ensureBlankLine();
|
|
114
138
|
this.forEachEnum("leading-and-interposing", (u, enumName) => {
|
|
@@ -119,14 +143,14 @@ class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer_1.ConvenienceRe
|
|
|
119
143
|
const mapValue = [];
|
|
120
144
|
this.forEachObject("none", (type, name) => {
|
|
121
145
|
mapKey.push(name);
|
|
122
|
-
mapValue.push(
|
|
146
|
+
mapValue.push(type);
|
|
123
147
|
});
|
|
124
148
|
mapKey.forEach((_, index) => {
|
|
125
149
|
// assume first
|
|
126
150
|
let ordinal = 0;
|
|
127
151
|
// pull out all names
|
|
128
152
|
const source = mapValue[index];
|
|
129
|
-
const names =
|
|
153
|
+
const names = this.walkObjectNames(source);
|
|
130
154
|
// must be behind all these names
|
|
131
155
|
for (let i = 0; i < names.length; i++) {
|
|
132
156
|
const depName = names[i];
|
|
@@ -143,7 +167,7 @@ class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer_1.ConvenienceRe
|
|
|
143
167
|
order.splice(ordinal, 0, index);
|
|
144
168
|
});
|
|
145
169
|
// now emit ordered source
|
|
146
|
-
order.forEach(i => this.emitGatheredSource(mapValue[i]));
|
|
170
|
+
order.forEach(i => this.emitGatheredSource(this.gatherSource(() => this.emitObject(mapKey[i], mapValue[i]))));
|
|
147
171
|
}
|
|
148
172
|
emitSourceStructure() {
|
|
149
173
|
if (this.leadingComments !== undefined) {
|