Skip to content

Commit

Permalink
update python
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 22, 2023
1 parent 1121df6 commit 218d69c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Generator/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,20 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte
$code.= $this->indent . $property->getName()->getProperty() . ': ' . $property->getType() . "\n";
}

$code.= "\n";
$code.= "\n";

return $code;
}

protected function writeMap(Code\Name $name, string $type, MapType $origin): string
{
$subType = $this->generator->getType($origin->getAdditionalProperties());

$code = '@dataclass_json' . "\n";
$code.= '@dataclass' . "\n";
$code.= 'class ' . $name->getClass() . '(Dict[str, ' . $subType . ']):' . "\n";
$code = 'class ' . $name->getClass() . '(Dict[str, ' . $subType . ']):' . "\n";
$code.= ' pass' . "\n";
$code.= "\n";
$code.= "\n";

return $code;
}
Expand All @@ -94,6 +97,8 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
$code.= '@dataclass' . "\n";
$code.= 'class ' . $name->getClass() . '(' . $type . '):' . "\n";
$code.= ' pass' . "\n";
$code.= "\n";
$code.= "\n";

return $code;
}
Expand All @@ -113,6 +118,7 @@ protected function writeHeader(TypeAbstract $origin): string
$code.= "\n";
}

$code.= "\n";
$code.= "\n";

$comment = $origin->getDescription();
Expand Down
6 changes: 4 additions & 2 deletions tests/Generator/resource/python/python.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from dataclasses_json import dataclass_json


# Location of the person
@dataclass_json
@dataclass
Expand All @@ -11,6 +12,7 @@ class Location:
from dataclasses import dataclass
from dataclasses_json import dataclass_json


# An application
@dataclass_json
@dataclass
Expand All @@ -23,6 +25,7 @@ class Web:
from typing import List
from location import Location


# An simple author element with some description
@dataclass_json
@dataclass
Expand All @@ -36,8 +39,6 @@ class Author:
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from typing import Dict
@dataclass_json
@dataclass
class Meta(Dict[str, str]):
pass

Expand All @@ -52,6 +53,7 @@ class Meta(Dict[str, str]):
from location import Location
from web import Web


# An general news entry
@dataclass_json
@dataclass
Expand Down
15 changes: 15 additions & 0 deletions tests/Generator/resource/python/python_complex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from dataclasses_json import dataclass_json


# Represents a base type. Every type extends from this common type and shares the defined properties
@dataclass_json
@dataclass
Expand All @@ -15,6 +16,7 @@ class CommonType:
from dataclasses_json import dataclass_json
from common_type import CommonType


# Represents an any type
@dataclass_json
@dataclass
Expand All @@ -32,6 +34,7 @@ class AnyType(CommonType):
from generic_type import GenericType
from any_type import AnyType


# Represents an array type. An array type contains an ordered list of a specific type
@dataclass_json
@dataclass
Expand All @@ -47,6 +50,7 @@ class ArrayType(CommonType):
from typing import Union
from common_type import CommonType


# Represents a scalar type
@dataclass_json
@dataclass
Expand All @@ -59,6 +63,7 @@ class ScalarType(CommonType):
from dataclasses_json import dataclass_json
from scalar_type import ScalarType


# Represents a boolean type
@dataclass_json
@dataclass
Expand All @@ -69,6 +74,7 @@ class BooleanType(ScalarType):
from dataclasses_json import dataclass_json
from typing import Dict


# Adds support for polymorphism. The discriminator is an object name that is used to differentiate between other schemas which may satisfy the payload description
@dataclass_json
@dataclass
Expand All @@ -79,6 +85,7 @@ class Discriminator:
from dataclasses import dataclass
from dataclasses_json import dataclass_json


# Represents a generic type. A generic type can be used i.e. at a map or array which then can be replaced on reference via the $template keyword
@dataclass_json
@dataclass
Expand All @@ -90,6 +97,7 @@ class GenericType:
from typing import List
from reference_type import ReferenceType


# Represents an intersection type
@dataclass_json
@dataclass
Expand All @@ -111,6 +119,7 @@ class IntersectionType:
from generic_type import GenericType
from any_type import AnyType


# Represents a map type. A map type contains variable key value entries of a specific type
@dataclass_json
@dataclass
Expand All @@ -124,6 +133,7 @@ class MapType(CommonType):
from dataclasses_json import dataclass_json
from scalar_type import ScalarType


# Represents a number type (contains also integer)
@dataclass_json
@dataclass
Expand All @@ -139,6 +149,7 @@ class NumberType(ScalarType):
from dataclasses_json import dataclass_json
from typing import Dict


# Represents a reference type. A reference type points to a specific type at the definitions map
@dataclass_json
@dataclass
Expand All @@ -150,6 +161,7 @@ class ReferenceType:
from dataclasses_json import dataclass_json
from scalar_type import ScalarType


# Represents a string type
@dataclass_json
@dataclass
Expand All @@ -176,6 +188,7 @@ class StringType(ScalarType):
from reference_type import ReferenceType
from generic_type import GenericType


# Represents a struct type. A struct type contains a fix set of defined properties
@dataclass_json
@dataclass
Expand All @@ -194,6 +207,7 @@ class StructType(CommonType):
from map_type import MapType
from reference_type import ReferenceType


# The root TypeSchema
@dataclass_json
@dataclass
Expand All @@ -212,6 +226,7 @@ class TypeSchema:
from boolean_type import BooleanType
from reference_type import ReferenceType


# Represents an union type. An union type can contain one of the provided types
@dataclass_json
@dataclass
Expand Down

0 comments on commit 218d69c

Please sign in to comment.