45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
// 1
|
|
import { File } from '@asyncapi/generator-react-sdk';
|
|
// 2
|
|
import { PythonGenerator, PYTHON_PYDANTIC_PRESET } from '@asyncapi/modelina';
|
|
|
|
/**
|
|
* @typedef RenderArgument
|
|
* @type {object}
|
|
* @property {AsyncAPIDocument} asyncapi document object received from the generator.
|
|
*/
|
|
|
|
function generateModels(model) {
|
|
const properties = model.model?.properties ?? {};
|
|
const ownIncludes = Object.values(properties)
|
|
.filter(p => p.property.constructor.name === 'ConstrainedReferenceModel')
|
|
.map(p => `from ${p.property.ref.name} import ${p.property.ref.name}`).join('\n')
|
|
const imports = model.dependencies
|
|
.join('\n');
|
|
|
|
return (
|
|
<File name={`${model.modelName}.py`}>
|
|
{imports}
|
|
{'\n\n'}
|
|
{ownIncludes}
|
|
{'\n\n'}
|
|
{model.result}
|
|
</File>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Render all schema models
|
|
* @param {RenderArgument} param0
|
|
* @returns
|
|
*/
|
|
|
|
export default async function schemaRender({ asyncapi }) {
|
|
const pythonGenerator = new PythonGenerator({
|
|
presets: [PYTHON_PYDANTIC_PRESET],
|
|
processorOptions: {}
|
|
});
|
|
const models = await pythonGenerator.generate(asyncapi);
|
|
return models.map(model => generateModels(model))
|
|
}
|