MCPcopy Index your code
hub / github.com/glideapps/quicktype / schemaToType

Function schemaToType

src/JSONSchemaInput.ts:196–428  ·  view source on GitHub ↗
(typeBuilder: TypeGraphBuilder, topLevelName: string, rootJson: any)

Source from the content-addressed store, hash-verified

194}
195
196export function schemaToType(typeBuilder: TypeGraphBuilder, topLevelName: string, rootJson: any): TypeRef {
197 const root = checkStringMap(rootJson);
198 let typeForPath = Map<List<any>, TypeRef>();
199
200 function setTypeForPath(path: Ref, t: TypeRef): void {
201 typeForPath = typeForPath.set(makeImmutablePath(path), t);
202 }
203
204 function unifyTypes(typesToUnify: TypeRef[], typeName: string, isInferred: boolean): TypeRef {
205 if (typesToUnify.length === 0) {
206 return panic("Cannot unify empty list of types");
207 } else if (typesToUnify.length === 1) {
208 return typesToUnify[0];
209 } else {
210 const unionBuilder = new UnifyUnionBuilder(typeBuilder, typeName, isInferred, unifyTypes);
211
212 const registerType = (t: Type): void => {
213 matchTypeExhaustive<void>(
214 t,
215 _anyType => unionBuilder.addAny(),
216 _nullType => unionBuilder.addNull(),
217 _boolType => unionBuilder.addBool(),
218 _integerType => unionBuilder.addInteger(),
219 _doubleType => unionBuilder.addDouble(),
220 _stringType => unionBuilder.addStringType("string"),
221 arrayType => unionBuilder.addArray(arrayType.items.typeRef),
222 classType => unionBuilder.addClass(classType.typeRef),
223 mapType => unionBuilder.addMap(mapType.values.typeRef),
224 enumType => enumType.cases.forEach(s => unionBuilder.addEnumCase(s)),
225 unionType => unionType.members.forEach(registerType),
226 _dateType => unionBuilder.addStringType("date"),
227 _timeType => unionBuilder.addStringType("time"),
228 _dateTimeType => unionBuilder.addStringType("date-time")
229 );
230 };
231
232 for (const t of typesToUnify) {
233 registerType(getHopefullyFinishedType(typeBuilder, t));
234 }
235
236 return unionBuilder.buildUnion(false);
237 }
238 }
239
240 function lookupRef(local: StringMap, localPath: Ref, ref: Ref): [StringMap, Ref] {
241 const first = ref.first();
242 if (first === undefined) {
243 return [local, localPath];
244 }
245 const rest = ref.rest();
246 if (first.kind === PathElementKind.Root) {
247 return lookupRef(root, List([first]), ref.rest());
248 }
249 localPath = localPath.push(first);
250 switch (first.kind) {
251 case PathElementKind.Definition:
252 return lookupRef(lookupDefinition(local, first.name), localPath, rest);
253 case PathElementKind.OneOf:

Callers 1

RunClass · 0.90

Calls 2

checkStringMapFunction · 0.90
toTypeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…