(schema: StringMap, path: Ref, name: string, isInferred: boolean)
| 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 |
no test coverage detected
searching dependent graphs…