|
| 1 | +import type { Schema } from './ir'; |
| 2 | + |
| 3 | +export type OptimizeFn = (schema: Schema) => Schema; |
| 4 | + |
| 5 | +export function foldIntersection(schema: Schema, optimize: OptimizeFn): Schema { |
| 6 | + if (schema.type !== 'intersection') { |
| 7 | + return schema; |
| 8 | + } |
| 9 | + |
| 10 | + const innerSchemas = schema.schemas.map(optimize); |
| 11 | + let combinedObject: Schema & { type: 'object' } = { |
| 12 | + type: 'object', |
| 13 | + properties: {}, |
| 14 | + required: [], |
| 15 | + }; |
| 16 | + let result: Schema = combinedObject; |
| 17 | + innerSchemas.forEach((innerSchema) => { |
| 18 | + if (innerSchema.type === 'object') { |
| 19 | + Object.assign(combinedObject.properties, innerSchema.properties); |
| 20 | + combinedObject.required.push(...innerSchema.required); |
| 21 | + } else if (result.type === 'intersection') { |
| 22 | + result.schemas.push(innerSchema); |
| 23 | + } else { |
| 24 | + result = { |
| 25 | + type: 'intersection', |
| 26 | + schemas: [combinedObject, innerSchema], |
| 27 | + }; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + return result; |
| 32 | +} |
| 33 | + |
| 34 | +export function simplifyUnion(schema: Schema, optimize: OptimizeFn): Schema { |
| 35 | + if (schema.type !== 'union') { |
| 36 | + return schema; |
| 37 | + } else if (schema.schemas.length === 1) { |
| 38 | + return schema.schemas[0]!; |
| 39 | + } else if (schema.schemas.length === 0) { |
| 40 | + return { type: 'undefined' }; |
| 41 | + } |
| 42 | + |
| 43 | + const innerSchemas = schema.schemas.map(optimize); |
| 44 | + |
| 45 | + const literals: Record<(Schema & { type: 'primitive' })['value'], any[]> = { |
| 46 | + string: [], |
| 47 | + number: [], |
| 48 | + integer: [], |
| 49 | + boolean: [], |
| 50 | + null: [], |
| 51 | + }; |
| 52 | + const remainder: Schema[] = []; |
| 53 | + innerSchemas.forEach((innerSchema) => { |
| 54 | + if (innerSchema.type === 'primitive' && innerSchema.enum !== undefined) { |
| 55 | + literals[innerSchema.value].push(...innerSchema.enum); |
| 56 | + } else { |
| 57 | + remainder.push(innerSchema); |
| 58 | + } |
| 59 | + }); |
| 60 | + const result: Schema = { |
| 61 | + type: 'union', |
| 62 | + schemas: remainder, |
| 63 | + }; |
| 64 | + for (const [key, value] of Object.entries(literals)) { |
| 65 | + if (value.length > 0) { |
| 66 | + result.schemas.push({ |
| 67 | + type: 'primitive', |
| 68 | + value: key as any, |
| 69 | + enum: value, |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (result.schemas.length === 1) { |
| 75 | + return result.schemas[0]!; |
| 76 | + } else { |
| 77 | + return result; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export function filterUndefinedUnion(schema: Schema): [boolean, Schema] { |
| 82 | + if (schema.type !== 'union') { |
| 83 | + return [false, schema]; |
| 84 | + } |
| 85 | + |
| 86 | + const undefinedIndex = schema.schemas.findIndex((s) => s.type === 'undefined'); |
| 87 | + if (undefinedIndex < 0) { |
| 88 | + return [false, schema]; |
| 89 | + } |
| 90 | + |
| 91 | + const schemas = schema.schemas.filter((s) => s.type !== 'undefined'); |
| 92 | + if (schemas.length === 0) { |
| 93 | + return [true, { type: 'undefined' }]; |
| 94 | + } else if (schemas.length === 1) { |
| 95 | + return [true, schemas[0]!]; |
| 96 | + } else { |
| 97 | + return [true, { type: 'union', schemas }]; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export function optimize(schema: Schema): Schema { |
| 102 | + if (schema.type === 'object') { |
| 103 | + const properties: Record<string, Schema> = {}; |
| 104 | + const required: string[] = []; |
| 105 | + for (const [key, prop] of Object.entries(schema.properties)) { |
| 106 | + const optimized = optimize(prop); |
| 107 | + if (optimized.type === 'undefined') { |
| 108 | + continue; |
| 109 | + } |
| 110 | + const [isOptional, filteredSchema] = filterUndefinedUnion(optimized); |
| 111 | + properties[key] = filteredSchema; |
| 112 | + if (schema.required.indexOf(key) >= 0 && !isOptional) { |
| 113 | + required.push(key); |
| 114 | + } |
| 115 | + } |
| 116 | + return { type: 'object', properties, required }; |
| 117 | + } else if (schema.type === 'intersection') { |
| 118 | + return foldIntersection(schema, optimize); |
| 119 | + } else if (schema.type === 'union') { |
| 120 | + return simplifyUnion(schema, optimize); |
| 121 | + } else if (schema.type === 'array') { |
| 122 | + const optimized = optimize(schema.items); |
| 123 | + return { type: 'array', items: optimized }; |
| 124 | + } else if (schema.type === 'record') { |
| 125 | + return { type: 'record', codomain: optimize(schema.codomain) }; |
| 126 | + } else if (schema.type === 'tuple') { |
| 127 | + const schemas = schema.schemas.map(optimize); |
| 128 | + return { type: 'tuple', schemas }; |
| 129 | + } else { |
| 130 | + return schema; |
| 131 | + } |
| 132 | +} |
0 commit comments