Adapt openstack_types to structable_derive 0.2

Rework StructTable imports to point directly to structable (with
additional Options import). On the way drop unnecessary
`#[serde(untagged)]` for StringEnum and `serialize` for `Option<String>`

Change-Id: Ic9b7b5b319e5a7ea881f7b5e3e9da986276d079b
This commit is contained in:
Artem Goncharov
2025-04-14 11:00:26 +02:00
parent 95c77151bc
commit abda7a8b6a
3 changed files with 17 additions and 14 deletions

View File

@@ -433,8 +433,15 @@ class StructFieldResponse(StructField):
and "status" not in struct.fields.keys()
):
macros.add("status")
if not isinstance(self.data_type, BasePrimitiveType) or isinstance(
self.data_type, JsonValue
if not (
# option of primitive
isinstance(self.data_type, Option)
and isinstance(self.data_type.item_type, BasePrimitiveType)
) and (
# not primitive
not isinstance(self.data_type, BasePrimitiveType)
# or explicitly Json
or isinstance(self.data_type, JsonValue)
):
macros.add("serialize")
return f"#[structable({', '.join(sorted(macros))})]"
@@ -445,7 +452,11 @@ class StructResponse(Struct):
@property
def imports(self):
imports: set[str] = {"serde::Deserialize", "serde::Serialize"}
imports: set[str] = {
"serde::Deserialize",
"serde::Serialize",
"structable::{StructTable, StructTableOptions}",
}
for field in self.fields.values():
imports.update(field.data_type.imports)
# In difference to the SDK and Input we do not currently handle

View File

@@ -80,19 +80,13 @@ class Enum(common_rust.Enum):
return "#[serde(untagged)]"
class StringEnum(common_rust.StringEnum):
@property
def serde_container_macros(self) -> str:
return "#[serde(untagged)]"
class ResponseTypeManager(common_rust.TypeManager):
primitive_type_mapping = {}
data_type_mapping = {
model.Struct: common_rust.StructResponse,
model.Enum: Enum,
}
string_enum_class: Type[StringEnum] | StringEnum = StringEnum
# string_enum_class: Type[StringEnum] | StringEnum = StringEnum
def get_model_name(self, model_ref: model.Reference | None) -> str:
"""Get the localized model type name

View File

@@ -15,14 +15,12 @@
// WARNING: This file is automatically generated from OpenAPI schema using
// `openstack-codegenerator`.
{% import 'rust_macros.j2' as macros with context -%}
//! Response type for the {{ method }} {{ url }} operation
//! Response type for the {{ method | upper }} `{{ url }}` operation
use serde::{Deserialize, Serialize};
use structable_derive::StructTable;
{% for mod in additional_imports | sort -%}
use {{ mod }};
{% endfor %}
use crate::common::{StructTable, OutputConfig};
{% with data_type = response_type_manager.get_root_data_type() %}
{%- if data_type.__class__.__name__ == "StructResponse" %}
@@ -78,7 +76,7 @@ use crate::common::{StructTable, OutputConfig};
{%- elif subtype.base_type == "enum" and subtype.__class__.__name__ == "StringEnum" %}
{{ macros.docstring(subtype.description, indent=0) }}
{{ subtype.derive_container_macros }}
{{ subtype.serde_container_macros }}
{%- if subtype.serde_container_macros %}{{ subtype.serde_container_macros }}{% endif %}
pub enum {{ subtype.name }} {
{% for kind in subtype.variants %}
// {{ kind or "Empty" }}