From abda7a8b6ab2f72edd19827ba5ae0d444b975d60 Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Mon, 14 Apr 2025 11:00:26 +0200 Subject: [PATCH] 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` Change-Id: Ic9b7b5b319e5a7ea881f7b5e3e9da986276d079b --- codegenerator/common/rust.py | 17 ++++++++++++++--- codegenerator/rust_types.py | 8 +------- codegenerator/templates/rust_types/impl.rs.j2 | 6 ++---- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/codegenerator/common/rust.py b/codegenerator/common/rust.py index 843a42e..f8b3c64 100644 --- a/codegenerator/common/rust.py +++ b/codegenerator/common/rust.py @@ -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 diff --git a/codegenerator/rust_types.py b/codegenerator/rust_types.py index 2215fb4..4b782f7 100644 --- a/codegenerator/rust_types.py +++ b/codegenerator/rust_types.py @@ -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 diff --git a/codegenerator/templates/rust_types/impl.rs.j2 b/codegenerator/templates/rust_types/impl.rs.j2 index 7ff75dc..05d34a8 100644 --- a/codegenerator/templates/rust_types/impl.rs.j2 +++ b/codegenerator/templates/rust_types/impl.rs.j2 @@ -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" }}