Skip to content

Commit da9d8f5

Browse files
authored
python: Update codegen templates to support struct enums (#1766)
2 parents d7fd583 + c580f31 commit da9d8f5

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed
+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# this file is @generated
22
{% if type.kind == "struct" -%}
33
{% include "types/struct.py.jinja" -%}
4-
{%- elif type.kind == "string_enum"-%}
4+
{% elif type.kind == "string_enum" -%}
55
{% include "types/string_enum.py.jinja" -%}
6-
{%- elif type.kind == "integer_enum" %}
7-
{%- include "types/integer_enum.py.jinja" -%}
8-
{%- endif %}
6+
{% elif type.kind == "integer_enum" -%}
7+
{% include "types/integer_enum.py.jinja" -%}
8+
{% elif type.kind == "struct_enum" -%}
9+
{% include "types/struct_enum.py.jinja" -%}
10+
{% endif %}

python/templates/types/struct.py.jinja

+2-25
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,10 @@ from .common import BaseModel
66

77
{% for c in referenced_components -%}
88
from . {{ c | to_snake_case }} import {{ c | to_upper_camel_case }}
9-
{% endfor -%}
9+
{% endfor %}
1010

1111
class {{ type.name | to_upper_camel_case }}(BaseModel):
1212
{%- if type.description is defined %}
1313
{{ type.description | to_doc_comment(style="python") | indent(4) }}
1414
{% endif %}
15-
{%- for field in type.fields %}
16-
{%- if field.required and not field.nullable %}
17-
{%- if field.name | to_lower_camel_case != field.name %}
18-
{{ field.name | to_snake_case }}: {{ field.type.to_python() }} = Field(alias="{{ field.name }}")
19-
{%- else %}
20-
{{ field.name | to_snake_case }}: {{ field.type.to_python() }}
21-
{%- endif %}
22-
{%- elif field.required and field.nullable %}
23-
{%- if field.name | to_lower_camel_case != field.name %}
24-
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}] = Field(alias="{{ field.name }}")
25-
{%- else %}
26-
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}]
27-
{%- endif %}
28-
{%- else %}
29-
{%- if field.name | to_lower_camel_case != field.name %}
30-
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}] = Field(default=None, alias="{{ field.name }}")
31-
{%- else %}
32-
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}] = None
33-
{%- endif %}
34-
{%- endif %}
35-
{%- if field.description is defined %}
36-
{{ field.description | to_doc_comment(style="python") | indent(4) }}
37-
{%- endif %}
38-
{% endfor %}
15+
{%- include "types/struct_fields.py.jinja" %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import typing as t
2+
from typing_extensions import Annotated
3+
4+
from pydantic import Field
5+
from datetime import datetime
6+
7+
from .common import BaseModel
8+
9+
{% for c in referenced_components %}
10+
from . {{ c | to_snake_case }} import {{ c | to_upper_camel_case }}
11+
{%- endfor %}
12+
13+
{%- set type_name = type.name | to_upper_camel_case %}
14+
15+
16+
{% for variant in type.variants %}
17+
class {{ type_name }}{{ variant.name | to_upper_camel_case }}(BaseModel):
18+
{{ type.discriminator_field | to_snake_case }}: t.Literal["{{ variant.name }}"]
19+
20+
{% include "types/struct_fields.py.jinja" %}
21+
{% if variant.schema_ref is defined -%}
22+
{{ type.content_field | to_snake_case }}: {{ variant.schema_ref | to_upper_camel_case -}}
23+
{% if type.content_field | to_lower_camel_case != type.content_field %}= Field(alias="{{ type.content_field }}"){% endif %}
24+
{% endif %}
25+
{%- endfor %}
26+
27+
28+
{{ type_name }} = Annotated[
29+
t.Union[
30+
{%- for variant in type.variants -%}
31+
{{ type_name }}{{ variant.name | to_upper_camel_case }}
32+
{%- if not loop.last %}, {% endif %}
33+
{%- endfor -%}
34+
],
35+
Field(discriminator="{{ type.discriminator_field }}")
36+
]
37+
{%- if type.description is defined %}
38+
{{ type.description | to_doc_comment(style="python") }}
39+
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% for field in type.fields %}
2+
{%- if field.required and not field.nullable %}
3+
{%- if field.name | to_lower_camel_case != field.name %}
4+
{{ field.name | to_snake_case }}: {{ field.type.to_python() }} = Field(alias="{{ field.name }}")
5+
{%- else %}
6+
{{ field.name | to_snake_case }}: {{ field.type.to_python() }}
7+
{%- endif %}
8+
{%- elif field.required and field.nullable %}
9+
{%- if field.name | to_lower_camel_case != field.name %}
10+
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}] = Field(alias="{{ field.name }}")
11+
{%- else %}
12+
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}]
13+
{%- endif %}
14+
{%- else %}
15+
{%- if field.name | to_lower_camel_case != field.name %}
16+
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}] = Field(default=None, alias="{{ field.name }}")
17+
{%- else %}
18+
{{ field.name | to_snake_case }}: t.Optional[{{ field.type.to_python() }}] = None
19+
{%- endif %}
20+
{%- endif %}
21+
{%- if field.description is defined %}
22+
{{ field.description | to_doc_comment(style="python") | indent(4) }}
23+
{%- endif %}
24+
{% endfor %}

0 commit comments

Comments
 (0)