-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Replace dataclasses and dataclasses_json with pydantic
- Loading branch information
1 parent
121bfb4
commit 45631d5
Showing
4 changed files
with
130 additions
and
206 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,44 +1,38 @@ | ||
# This code is @generated by cw-schema-codegen. Do not modify this manually. | ||
from dataclasses import dataclass, field | ||
from dataclasses_json import dataclass_json, config | ||
from typing import Optional, Iterable | ||
|
||
enum_field = lambda: field(default=None, metadata=config(exclude=lambda x: x is None)) | ||
import typing | ||
from pydantic import BaseModel, RootModel | ||
|
||
@dataclass_json | ||
@dataclass | ||
class {{ name }}: | ||
'''{% for doc in docs %} | ||
class {{ name }}(RootModel): | ||
"""{% for doc in docs %} | ||
{{ doc }} | ||
{% endfor %}''' | ||
|
||
class VariantIndicator: | ||
''' | ||
This structure is an indicator of the simple enum variant that is currently contained | ||
in this enum structure. It's used only for the enum variants that does not contain | ||
any inner structure. It's constructed automatically and it's not intend to be manually | ||
used by the user. | ||
''' | ||
pass | ||
|
||
{% endfor %}""" | ||
|
||
{% for variant in variants %} | ||
'''{% for doc in variant.docs %} | ||
{{ doc }} | ||
{% endfor %}''' | ||
{% match variant.ty %} | ||
{% when TypeTemplate::Unit %} | ||
{{ variant.name }}: Optional[VariantIndicator] = enum_field() | ||
{% when TypeTemplate::Tuple with (types) %} | ||
{{ variant.name }}: Optional[tuple[{{ types|join(", ") }}]] = enum_field() | ||
{% when TypeTemplate::Named with { fields } %} | ||
class {{ variant.name }}Type: | ||
{% for field in fields %} | ||
'''{% for doc in field.docs %} | ||
# {{ doc }} | ||
{% endfor %}''' | ||
{{ field.name }}: {{ field.ty }} | ||
{% endfor %} | ||
{{ variant.name }}: Optional[{{ variant.name }}Type] = enum_field() | ||
{% endmatch %} | ||
{% match variant.ty %} | ||
{% when TypeTemplate::Unit %} | ||
class {{ variant.name }}(RootModel): | ||
"""{% for doc in variant.docs %} | ||
{{ doc }} | ||
{% endfor %}""" | ||
root: None | ||
{% when TypeTemplate::Tuple with (types) %} | ||
class {{ variant.name }}(BaseModel): | ||
"""{% for doc in variant.docs %} | ||
{{ doc }} | ||
{% endfor %}""" | ||
{{ variant.name }}: typing.Tuple[{{ types|join(", ") }}] | ||
{% when TypeTemplate::Named with { fields } %} | ||
class __Inner: | ||
"""{% for doc in variant.docs %} | ||
{{ doc }} | ||
{% endfor %}""" | ||
{% for field in fields %} | ||
{{ field.name }}: {{ field.ty }} | ||
"""{% for doc in field.docs %} | ||
# {{ doc }} | ||
{% endfor %}""" | ||
{% endfor %} | ||
{{ variant.name }}: __Inner | ||
{% endmatch %} | ||
{% endfor %} |
72 changes: 24 additions & 48 deletions
72
packages/cw-schema-codegen/templates/python/struct.tpl.py
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 |
---|---|---|
@@ -1,55 +1,31 @@ | ||
# This code is @generated by cw-schema-codegen. Do not modify this manually. | ||
|
||
/** | ||
{% for doc in docs %} | ||
* {{ doc }} | ||
{% endfor %} | ||
*/ | ||
|
||
type {{ name }} = | ||
{% match ty %} | ||
{% when TypeTemplate::Unit %} | ||
void | ||
{% when TypeTemplate::Tuple with (types) %} | ||
[{{ types|join(", ") }}] | ||
{% when TypeTemplate::Named with { fields } %} | ||
{ | ||
{% for field in fields %} | ||
/** | ||
{% for doc in field.docs %} | ||
* {{ doc }} | ||
{% endfor %} | ||
*/ | ||
|
||
{{ field.name }}: {{ field.ty }}; | ||
{% endfor %} | ||
} | ||
{% endmatch %} | ||
|
||
import typing | ||
from pydantic import BaseModel, RootModel | ||
|
||
|
||
# This code is @generated by cw-schema-codegen. Do not modify this manually. | ||
from dataclasses import dataclass, field | ||
from dataclasses_json import dataclass_json, config | ||
from typing import Optional, Iterable | ||
|
||
@dataclass_json | ||
@dataclass | ||
class {{ name }}: | ||
{% match ty %} | ||
{% when TypeTemplate::Unit %} | ||
class {{ name }}(RootModel): | ||
'''{% for doc in docs %} | ||
{{ doc }} | ||
{% endfor %}''' | ||
|
||
{% match ty %} | ||
{% when TypeTemplate::Unit %} | ||
pass | ||
{% when TypeTemplate::Tuple with (types) %} | ||
{{ variant.name }}: tuple[{{ types|join(", ") }}] | ||
{% when TypeTemplate::Named with { fields } %} | ||
{% for field in fields %} | ||
'''{% for doc in field.docs %} | ||
# {{ doc }} | ||
{% endfor %}''' | ||
{{ field.name }}: {{ field.ty }} | ||
{% endfor %} | ||
{% endmatch %} | ||
root: None | ||
{% when TypeTemplate::Tuple with (types) %} | ||
class {{ name }}(RootModel): | ||
'''{% for doc in docs %} | ||
{{ doc }} | ||
{% endfor %}''' | ||
root: typing.Tuple[{{ types|join(", ") }}] | ||
{% when TypeTemplate::Named with { fields } %} | ||
class {{ name }}(BaseModel): | ||
'''{% for doc in docs %} | ||
{{ doc }} | ||
{% endfor %}''' | ||
{% for field in fields %} | ||
{{ field.name }}: {{ field.ty }} | ||
'''{% for doc in field.docs %} | ||
# {{ doc }} | ||
{% endfor %}''' | ||
{% endfor %} | ||
{% endmatch %} |
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