-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
94 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const _ = require('lodash'); | ||
const getTypeInfo = require('../helpers/all').getTypeInfo; | ||
const indent1 = require('../helpers/all').indent1; | ||
const indent2 = require('../helpers/all').indent2; | ||
const indent3 = require('../helpers/all').indent3; | ||
const indent4 = require('../helpers/all').indent4; | ||
|
||
function modelClass(name, properties, required, indentLevel = 1) { | ||
const className = _.upperFirst(name); | ||
|
||
let classDefinition = `${indent1(indentLevel)}class ${className}(Entity):\n`; | ||
|
||
Object.entries(properties).forEach(([propName, prop]) => { | ||
const typeInfo = getTypeInfo([propName, prop]); | ||
|
||
if (typeInfo.recursive) { | ||
classDefinition += modelClass( | ||
typeInfo.innerType, typeInfo.properties, prop.required(), indentLevel + 1 | ||
); | ||
} else if (typeInfo.generalType === 'enum') { | ||
classDefinition += `${indent2(indentLevel)}class ${typeInfo.type}(str, Enum):\n`; | ||
typeInfo.enum.forEach((value) => { | ||
classDefinition += `${indent3(indentLevel)}${value} = '${value}'\n`; | ||
}); | ||
classDefinition += `${indent2(indentLevel)}\n`; | ||
} | ||
}); | ||
|
||
classDefinition += `${indent2(indentLevel)}def __init__(self,\n`; | ||
|
||
Object.entries(properties).forEach(([propName, prop], index) => { | ||
const typeInfo = getTypeInfo([propName, prop]); | ||
const separator = index === 0 ? '' : ',\n'; | ||
classDefinition += `${separator}${indent4(indentLevel)}${typeInfo.pythonName}: ${typeInfo.pythonType}`; | ||
}); | ||
|
||
classDefinition += `):\n`; | ||
|
||
Object.entries(properties).forEach(([propName, prop]) => { | ||
const typeInfo = getTypeInfo([propName, prop]); | ||
classDefinition += `${indent3(indentLevel)}self.${typeInfo.pythonName} = ${typeInfo.pythonName}\n`; | ||
}); | ||
|
||
if (Object.keys(properties).length === 0) { | ||
classDefinition += `${indent3(indentLevel)}pass\n`; | ||
} | ||
|
||
classDefinition += `${indent1(indentLevel)}\n`; | ||
return classDefinition; | ||
} | ||
|
||
module.exports = { modelClass } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { File } = require('@asyncapi/generator-react-sdk'); | ||
const { modelClass } = require('../partials/model-class.js') | ||
const getImports = require('../helpers/all.js').getImports | ||
const convertToFileName = require('../helpers/utils.js') | ||
|
||
function generateModelCode(schemaName, schema) { | ||
let code = `from enum import Enum | ||
from typing import Sequence | ||
from entity import Entity | ||
`; | ||
|
||
const code_imports = getImports(schema); | ||
code += code_imports + '\n'; | ||
|
||
code += modelClass(schemaName, schema.properties(), schema.required() | [] , 0); | ||
|
||
return code; | ||
} | ||
|
||
function ModelFile({ asyncapi }) { | ||
const files = []; | ||
const schemas = asyncapi.components().schemas() | ||
|
||
Object.entries(schemas).forEach(([schemaName, schema]) => { | ||
const generatedCode = generateModelCode(schemaName, schema); | ||
files.push( | ||
<File name={`${convertToFileName(schemaName)}.py`}> | ||
{generatedCode} | ||
</File> | ||
); | ||
}); | ||
return files; | ||
} | ||
|
||
module.exports = ModelFile; |