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

Function convertToType

src/JSONSchemaInput.ts:375–410  ·  view source on GitHub ↗
(schema: StringMap, path: Ref, name: string, isInferred: boolean)

Source from the content-addressed store, hash-verified

373 }
374
375 function convertToType(schema: StringMap, path: Ref, name: string, isInferred: boolean): TypeRef {
376 [name, isInferred] = getName(schema, name, isInferred);
377
378 function convertOneOrAnyOf(cases: any, kind: PathElementKind.OneOf | PathElementKind.AnyOf): TypeRef {
379 if (!Array.isArray(cases)) {
380 return panic(`oneOf or anyOf is not an array: ${cases}`);
381 }
382 // FIXME: This cast shouldn't be necessary, but TypeScript forces our hand.
383 const types = cases.map((t, index) =>
384 toType(checkStringMap(t), path.push({ kind, index } as any), name, isInferred)
385 );
386 return unifyTypes(types, name, isInferred);
387 }
388
389 if (schema.$ref !== undefined) {
390 const [ref, refName] = parseRef(schema.$ref);
391 const [target, targetPath] = lookupRef(schema, path, ref);
392 return toType(target, targetPath, isInferred ? refName : name, isInferred);
393 } else if (schema.enum !== undefined) {
394 return typeBuilder.getEnumType(name, isInferred, OrderedSet(checkStringArray(schema.enum)));
395 } else if (schema.type !== undefined) {
396 const typeNames = checkTypeList(schema.type);
397 if (typeNames.size === 1) {
398 return fromTypeName(schema, path, name, defined(typeNames.first()));
399 } else {
400 const types = typeNames.map(n => fromTypeName(schema, path, name, n));
401 return unifyTypes(types.toArray(), name, isInferred);
402 }
403 } else if (schema.oneOf !== undefined) {
404 return convertOneOrAnyOf(schema.oneOf, PathElementKind.OneOf);
405 } else if (schema.anyOf !== undefined) {
406 return convertOneOrAnyOf(schema.anyOf, PathElementKind.AnyOf);
407 } else {
408 return typeBuilder.getPrimitiveType("any");
409 }
410 }
411
412 function toType(schema: StringMap, path: Ref, name: string, isInferred: boolean): TypeRef {
413 // FIXME: This fromJS thing is ugly and inefficient. Schemas aren't

Callers 1

toTypeFunction · 0.85

Calls 11

definedFunction · 0.90
getNameFunction · 0.85
parseRefFunction · 0.85
lookupRefFunction · 0.85
toTypeFunction · 0.85
checkStringArrayFunction · 0.85
checkTypeListFunction · 0.85
fromTypeNameFunction · 0.85
unifyTypesFunction · 0.85
convertOneOrAnyOfFunction · 0.85
mapMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…