Skip to content

Commit

Permalink
Merge branch 'next' into fix_python_type_andinit
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Mar 4, 2024
2 parents 09eaf63 + 663191f commit 23760b6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Array [
"
class ObjProperty:
def __init__(self, input: dict):
if hasattr(input, \\"number\\"):
self._number: float = input[\\"number\\"]
if hasattr(input, \\"additionalProperties\\"):
self._additionalProperties: dict[Any, Any] = input[\\"additionalProperties\\"]
def __init__(self, input):
if hasattr(input, 'number'):
self._number = input.number
if hasattr(input, 'additionalProperties'):
self._additionalProperties = input.additionalProperties
@property
def number(self):
Expand All @@ -33,11 +33,11 @@ Array [
"
class ObjProperty:
def __init__(self, input: dict):
if hasattr(input, \\"number\\"):
self._number: float = input[\\"number\\"]
if hasattr(input, \\"additionalProperties\\"):
self._additionalProperties: dict[Any, Any] = input[\\"additionalProperties\\"]
def __init__(self, input):
if hasattr(input, 'number'):
self._number = input.number
if hasattr(input, 'additionalProperties'):
self._additionalProperties = input.additionalProperties
@property
def number(self):
Expand All @@ -61,11 +61,11 @@ Array [
"from ObjProperty import ObjProperty
class Root:
def __init__(self, input: dict):
if hasattr(input, \\"email\\"):
self._email: str = input[\\"email\\"]
if hasattr(input, \\"objProperty\\"):
self._objProperty: ObjProperty = ObjProperty(input[\\"objProperty\\"])
def __init__(self, input):
if hasattr(input, 'email'):
self._email = input.email
if hasattr(input, 'objProperty'):
self._objProperty = input.objProperty
@property
def email(self):
Expand All @@ -89,11 +89,11 @@ Array [
"from ObjProperty import ObjProperty
class Root:
def __init__(self, input: dict):
if hasattr(input, \\"email\\"):
self._email: str = input[\\"email\\"]
if hasattr(input, \\"objProperty\\"):
self._objProperty: ObjProperty = ObjProperty(input[\\"objProperty\\"])
def __init__(self, input):
if hasattr(input, 'email'):
self._email = input.email
if hasattr(input, 'objProperty'):
self._objProperty = input.objProperty
@property
def email(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
exports[`Should be able to render python models and should log expected output to console 1`] = `
Array [
"class Root:
def __init__(self, input: dict):
if hasattr(input, \\"email\\"):
self._email: str = input[\\"email\\"]
def __init__(self, input):
if hasattr(input, 'email'):
self._email = input.email
@property
def email(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
exports[`Should be able to render JSON serialization and deserialization functions and should log expected output to console 1`] = `
Array [
"class Root:
def __init__(self, input: dict):
if hasattr(input, \\"email\\"):
self._email: str = input[\\"email\\"]
if hasattr(input, \\"additionalProperties\\"):
self._additionalProperties: dict[Any, Any] = input[\\"additionalProperties\\"]
def __init__(self, input):
if hasattr(input, 'email'):
self._email = input.email
if hasattr(input, 'additionalProperties'):
self._additionalProperties = input.additionalProperties
@property
def email(self):
Expand Down
48 changes: 1 addition & 47 deletions src/generators/python/constrainer/ConstantConstrainer.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
import {
ConstrainedMetaModel,
ConstrainedEnumModel,
ConstrainedMetaModelOptionsConst,
ConstrainedReferenceModel,
ConstrainedStringModel
} from '../../../models';
import { PythonConstantConstraint } from '../PythonGenerator';

const getConstrainedEnumModelConstant = (args: {
constrainedMetaModel: ConstrainedMetaModel;
constrainedEnumModel: ConstrainedEnumModel;
constOptions: ConstrainedMetaModelOptionsConst;
}) => {
const constrainedEnumValueModel = args.constrainedEnumModel.values.find(
(value) => value.originalInput === args.constOptions.originalInput
);

if (constrainedEnumValueModel) {
return `${args.constrainedMetaModel.type}.${constrainedEnumValueModel.key}`;
}
};

export function defaultConstantConstraints(): PythonConstantConstraint {
return ({ constrainedMetaModel }) => {
const constOptions = constrainedMetaModel.options.const;

if (!constOptions) {
return undefined;
}

if (
constrainedMetaModel instanceof ConstrainedReferenceModel &&
constrainedMetaModel.ref instanceof ConstrainedEnumModel
) {
return getConstrainedEnumModelConstant({
constrainedMetaModel,
constrainedEnumModel: constrainedMetaModel.ref,
constOptions
});
} else if (constrainedMetaModel instanceof ConstrainedEnumModel) {
return getConstrainedEnumModelConstant({
constrainedMetaModel,
constrainedEnumModel: constrainedMetaModel,
constOptions
});
} else if (constrainedMetaModel instanceof ConstrainedStringModel) {
return `"${constOptions.originalInput}"`;
}

return () => {
return undefined;
};
}
24 changes: 6 additions & 18 deletions src/generators/python/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { PythonRenderer } from '../PythonRenderer';
import {
ConstrainedObjectModel,
ConstrainedObjectPropertyModel,
ConstrainedReferenceModel
ConstrainedObjectPropertyModel
} from '../../../models';
import { PythonOptions } from '../PythonGenerator';
import { ClassPresetType } from '../PythonPreset';
Expand Down Expand Up @@ -82,37 +81,26 @@ export const PYTHON_DEFAULT_CLASS_PRESET: ClassPresetType<PythonOptions> = {
const properties = model.properties || {};
let body = '';
if (Object.keys(properties).length > 0) {
const assignments = Object.values(properties).map((property) => {
let assignment: string;
if (property.property instanceof ConstrainedReferenceModel) {
assignment = `self._${property.propertyName}: ${property.property.type} = ${property.property.type}(input["${property.propertyName}"])`;
} else {
assignment = `self._${property.propertyName}: ${property.property.type} = input["${property.propertyName}"]`;
}
const assigments = Object.values(properties).map((property) => {
if (!property.required) {
return `if hasattr(input, "${property.propertyName}"):\n\t${assignment}`;
return `if hasattr(input, '${property.propertyName}'):\n\tself._${property.propertyName} = input.${property.propertyName}`;
}
return assignment;
return `self._${property.propertyName} = input.${property.propertyName}`;
});
body = renderer.renderBlock(assignments);
body = renderer.renderBlock(assigments);
} else {
body = `"""
No properties
"""`;
}
return `def __init__(self, input: dict):
return `def __init__(self, input):
${renderer.indent(body)}`;
},
getter({ property }) {
return `@property
def ${property.propertyName}(self):\n\treturn self._${property.propertyName}`;
},
setter({ property }) {
// if const value exists we should not render a setter
if (property.property.options.const?.value) {
return '';
}

return `@${property.propertyName}.setter
def ${property.propertyName}(self, ${property.propertyName}):\n\tself._${property.propertyName} = ${property.propertyName}`;
}
Expand Down
46 changes: 23 additions & 23 deletions test/generators/python/__snapshots__/PythonGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

exports[`PythonGenerator Class should not render reserved keyword 1`] = `
"class Address:
def __init__(self, input: dict):
if hasattr(input, \\"reservedReservedDel\\"):
self._reservedReservedDel: str = input[\\"reservedReservedDel\\"]
if hasattr(input, \\"reservedDel\\"):
self._reservedDel: str = input[\\"reservedDel\\"]
def __init__(self, input):
if hasattr(input, 'reservedReservedDel'):
self._reservedReservedDel = input.reservedReservedDel
if hasattr(input, 'reservedDel'):
self._reservedDel = input.reservedDel
@property
def reservedReservedDel(self):
Expand All @@ -26,18 +26,18 @@ exports[`PythonGenerator Class should not render reserved keyword 1`] = `

exports[`PythonGenerator Class should render \`class\` type 1`] = `
"class Address:
def __init__(self, input: dict):
self._streetName: str = input[\\"streetName\\"]
self._city: str = input[\\"city\\"]
self._state: str = input[\\"state\\"]
self._houseNumber: float = input[\\"houseNumber\\"]
if hasattr(input, \\"marriage\\"):
self._marriage: bool = input[\\"marriage\\"]
if hasattr(input, \\"members\\"):
self._members: str | float | bool = input[\\"members\\"]
self._arrayType: list[str | float | Any] = input[\\"arrayType\\"]
if hasattr(input, \\"additionalProperties\\"):
self._additionalProperties: dict[Any | str, Any | str] = input[\\"additionalProperties\\"]
def __init__(self, input):
self._streetName = input.streetName
self._city = input.city
self._state = input.state
self._houseNumber = input.houseNumber
if hasattr(input, 'marriage'):
self._marriage = input.marriage
if hasattr(input, 'members'):
self._members = input.members
self._arrayType = input.arrayType
if hasattr(input, 'additionalProperties'):
self._additionalProperties = input.additionalProperties
@property
def streetName(self):
Expand Down Expand Up @@ -104,11 +104,11 @@ exports[`PythonGenerator Class should work with custom preset for \`class\` type
test1
def __init__(self, input: dict):
if hasattr(input, \\"property\\"):
self._property: str = input[\\"property\\"]
if hasattr(input, \\"additionalProperties\\"):
self._additionalProperties: dict[Any, Any] = input[\\"additionalProperties\\"]
def __init__(self, input):
if hasattr(input, 'property'):
self._property = input.property
if hasattr(input, 'additionalProperties'):
self._additionalProperties = input.additionalProperties
test2
@property
Expand All @@ -132,7 +132,7 @@ exports[`PythonGenerator Class should work with custom preset for \`class\` type

exports[`PythonGenerator Class should work with empty objects 1`] = `
"class CustomClass:
def __init__(self, input: dict):
def __init__(self, input):
\\"\\"\\"
No properties
\\"\\"\\"
Expand Down

0 comments on commit 23760b6

Please sign in to comment.