fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 82 lines 2.7 kB view raw
1import { applyNaming, pathToName } from '@hey-api/shared'; 2 3// import { createSchemaComment } from '../../../plugins/shared/utils/schema'; 4import { $ } from '../../../py-dsl'; 5import type { ProcessorContext } from './processor'; 6// import { identifiers } from '../v2/constants'; 7// import { pipesToNode } from './pipes'; 8import type { Ast, IrSchemaToAstOptions } from './types'; 9 10export function exportAst({ 11 meta, 12 naming, 13 namingAnchor, 14 path, 15 plugin, 16 tags, 17}: Pick<IrSchemaToAstOptions, 'state'> & 18 ProcessorContext & { 19 ast: Ast; 20 }): void { 21 const name = pathToName(path, { anchor: namingAnchor }); 22 const symbol = plugin.symbol(applyNaming(name, naming), { 23 meta: { 24 category: 'schema', 25 path, 26 tags, 27 tool: 'pydantic', 28 ...meta, 29 }, 30 }); 31 32 const baseModel = plugin.external('pydantic.BaseModel'); 33 const classDef = $.class(symbol).extends(baseModel); 34 // .export() 35 // .$if(plugin.config.comments && createSchemaComment(schema), (c, v) => c.doc(v)) 36 // .$if(state.hasLazyExpression['~ref'], (c) => 37 // c.type($.type(v).attr(ast.typeName || identifiers.types.GenericSchema)), 38 // ) 39 // .assign(pipesToNode(ast.pipes, plugin)); 40 plugin.node(classDef); 41 // if (schema.type === 'object' && schema.properties) { 42 // const baseModelSymbol = plugin.external('pydantic.BaseModel'); 43 // const fieldSymbol = plugin.external('pydantic.Field'); 44 // const classDef = $.class(symbol).extends(baseModelSymbol); 45 46 // if (plugin.config.comments && schema.description) { 47 // classDef.doc(schema.description); 48 // } 49 50 // for (const name in schema.properties) { 51 // const property = schema.properties[name]!; 52 // const isOptional = !schema.required?.includes(name); 53 54 // const propertyAst = irSchemaToAst({ 55 // optional: isOptional, 56 // plugin, 57 // schema: property, 58 // state: { 59 // ...state, 60 // path: ref([...fromRef(state.path), 'properties', name]), 61 // }, 62 // }); 63 64 // let typeAnnotation = propertyAst.typeAnnotation; 65 66 // if (isOptional && !typeAnnotation.startsWith('Optional[')) { 67 // typeAnnotation = `Optional[${typeAnnotation}]`; 68 // } 69 70 // if (propertyAst.fieldConstraints && Object.keys(propertyAst.fieldConstraints).length > 0) { 71 // const constraints = Object.entries(propertyAst.fieldConstraints) 72 // .map(([key, value]) => `${key}=${JSON.stringify(value)}`) 73 // .join(', '); 74 // classDef.do($.stmt($.expr(`${name}: ${typeAnnotation} = Field(${constraints})`))); 75 // } else { 76 // classDef.do($.stmt($.expr(`${name}: ${typeAnnotation}`))); 77 // } 78 // } 79 80 // plugin.node(classDef); 81 // } 82}