From 69286b17c0571c2b7aad8a98a24521828bfea27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Wed, 17 Jul 2024 11:10:47 +0200 Subject: [PATCH] fix(schema): rename `type_` to `type` in schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marin Veršić --- client_cli/pytests/src/client_cli/iroha.py | 2 +- configs/swarm/genesis.json | 4 ++-- core/src/block.rs | 2 +- data_model/derive/src/id.rs | 22 +++++++++++----------- data_model/src/account.rs | 3 ++- data_model/src/asset.rs | 6 ++++-- data_model/src/domain.rs | 4 +++- data_model/src/parameter.rs | 2 +- data_model/src/peer.rs | 2 +- data_model/src/role.rs | 2 +- data_model/src/trigger.rs | 4 +++- docs/source/references/schema.json | 4 ++-- schema/derive/src/lib.rs | 12 +++++++++++- 13 files changed, 43 insertions(+), 26 deletions(-) diff --git a/client_cli/pytests/src/client_cli/iroha.py b/client_cli/pytests/src/client_cli/iroha.py index c12fe3dc1fc..9305c692138 100644 --- a/client_cli/pytests/src/client_cli/iroha.py +++ b/client_cli/pytests/src/client_cli/iroha.py @@ -114,7 +114,7 @@ def asset_definitions(self) -> Dict[str, str]: asset_definitions = {} for asset_def in asset_defs: - type_ = asset_def.get("type_") + type_ = asset_def.get("type") if type_: asset_definitions[asset_def["id"]] = type_ return asset_definitions diff --git a/configs/swarm/genesis.json b/configs/swarm/genesis.json index e72cafa70fb..feb8c89b0eb 100644 --- a/configs/swarm/genesis.json +++ b/configs/swarm/genesis.json @@ -84,7 +84,7 @@ "Register": { "AssetDefinition": { "id": "rose#wonderland", - "type_": "Numeric", + "type": "Numeric", "mintable": "Infinitely", "logo": null, "metadata": {} @@ -112,7 +112,7 @@ "Register": { "AssetDefinition": { "id": "cabbage#garden_of_live_flowers", - "type_": "Numeric", + "type": "Numeric", "mintable": "Infinitely", "logo": null, "metadata": {} diff --git a/core/src/block.rs b/core/src/block.rs index 6ed87ce18cc..4a9fc6e1630 100644 --- a/core/src/block.rs +++ b/core/src/block.rs @@ -428,7 +428,7 @@ mod valid { if let Err(error) = Self::validate_header(&block, topology, genesis_account, state_block) { - return WithEvents::new(Err((block, error.into()))); + return WithEvents::new(Err((block, error))); } if let Err(error) = diff --git a/data_model/derive/src/id.rs b/data_model/derive/src/id.rs index baaa45daa31..d35e6c42afe 100644 --- a/data_model/derive/src/id.rs +++ b/data_model/derive/src/id.rs @@ -85,29 +85,29 @@ pub fn impl_id_eq_ord_hash(emitter: &mut Emitter, input: &syn::DeriveInput) -> T quote! { #identifiable_derive - impl #impl_generics ::core::cmp::PartialOrd for #name #ty_generics #where_clause where Self: crate::Identifiable { + impl #impl_generics ::core::cmp::PartialOrd for #name #ty_generics #where_clause where Self: Identifiable { #[inline] fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> { Some(self.cmp(other)) } } - impl #impl_generics ::core::cmp::Ord for #name #ty_generics #where_clause where Self: crate::Identifiable { + impl #impl_generics ::core::cmp::Ord for #name #ty_generics #where_clause where Self: Identifiable { fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { - ::id(self).cmp(::id(other)) + ::id(self).cmp(::id(other)) } } - impl #impl_generics ::core::cmp::Eq for #name #ty_generics #where_clause where Self: crate::Identifiable {} - impl #impl_generics ::core::cmp::PartialEq for #name #ty_generics #where_clause where Self: crate::Identifiable { + impl #impl_generics ::core::cmp::Eq for #name #ty_generics #where_clause where Self: Identifiable {} + impl #impl_generics ::core::cmp::PartialEq for #name #ty_generics #where_clause where Self: Identifiable { fn eq(&self, other: &Self) -> bool { - ::id(self) == ::id(other) + ::id(self) == ::id(other) } } - impl #impl_generics ::core::hash::Hash for #name #ty_generics #where_clause where Self: crate::Identifiable { + impl #impl_generics ::core::hash::Hash for #name #ty_generics #where_clause where Self: Identifiable { fn hash(&self, state: &mut H) { - ::id(self).hash(state) + ::id(self).hash(state) } } } @@ -119,7 +119,7 @@ fn derive_identifiable(emitter: &mut Emitter, input: &IdDeriveInput) -> TokenStr let (id_type, id_expr) = get_id_type(emitter, input); quote! { - impl #impl_generics crate::Identifiable for #name #ty_generics #where_clause { + impl #impl_generics Identifiable for #name #ty_generics #where_clause { type Id = #id_type; #[inline] @@ -142,8 +142,8 @@ fn get_id_type(emitter: &mut Emitter, input: &IdDeriveInput) -> (syn::Type, syn: } IdAttr::Transparent => { return ( - parse_quote! {<#ty as crate::Identifiable>::Id}, - parse_quote! {crate::Identifiable::id(&self.#field_name)}, + parse_quote! {<#ty as Identifiable>::Id}, + parse_quote! {Identifiable::id(&self.#field_name)}, ); } IdAttr::Missing => { diff --git a/data_model/src/account.rs b/data_model/src/account.rs index 0717190102d..4ec006385c2 100644 --- a/data_model/src/account.rs +++ b/data_model/src/account.rs @@ -12,7 +12,8 @@ use serde_with::{DeserializeFromStr, SerializeDisplay}; pub use self::model::*; use crate::{ - domain::prelude::*, metadata::Metadata, HasMetadata, ParseError, PublicKey, Registered, + domain::prelude::*, metadata::Metadata, HasMetadata, Identifiable, ParseError, PublicKey, + Registered, }; #[model] diff --git a/data_model/src/asset.rs b/data_model/src/asset.rs index fcdeb50f63b..b1c1e1dd278 100644 --- a/data_model/src/asset.rs +++ b/data_model/src/asset.rs @@ -16,8 +16,8 @@ use serde_with::{DeserializeFromStr, SerializeDisplay}; pub use self::model::*; use crate::{ - account::prelude::*, domain::prelude::*, ipfs::IpfsPath, metadata::Metadata, HasMetadata, Name, - ParseError, Registered, + account::prelude::*, domain::prelude::*, ipfs::IpfsPath, metadata::Metadata, HasMetadata, + Identifiable, Name, ParseError, Registered, }; /// [`AssetTotalQuantityMap`] provides an API to work with collection of key([`AssetDefinitionId`])-value([`AssetValue`]) @@ -114,6 +114,7 @@ mod model { pub id: AssetDefinitionId, /// Type of [`AssetValue`] #[getset(get_copy = "pub")] + #[serde(rename = "type")] pub type_: AssetType, /// Is the asset mintable #[getset(get_copy = "pub")] @@ -162,6 +163,7 @@ mod model { /// The identification associated with the asset definition builder. pub id: AssetDefinitionId, /// The type value associated with the asset definition builder. + #[serde(rename = "type")] pub type_: AssetType, /// The mintablility associated with the asset definition builder. pub mintable: Mintable, diff --git a/data_model/src/domain.rs b/data_model/src/domain.rs index 8789ea47a34..89b24c545ea 100644 --- a/data_model/src/domain.rs +++ b/data_model/src/domain.rs @@ -11,7 +11,9 @@ use serde::{Deserialize, Serialize}; use serde_with::{DeserializeFromStr, SerializeDisplay}; pub use self::model::*; -use crate::{ipfs::IpfsPath, metadata::Metadata, prelude::*, HasMetadata, Name, Registered}; +use crate::{ + ipfs::IpfsPath, metadata::Metadata, prelude::*, HasMetadata, Identifiable, Name, Registered, +}; #[model] mod model { diff --git a/data_model/src/parameter.rs b/data_model/src/parameter.rs index afff07b46d7..546bb4a2455 100644 --- a/data_model/src/parameter.rs +++ b/data_model/src/parameter.rs @@ -10,7 +10,7 @@ use iroha_primitives::json::JsonString; use nonzero_ext::nonzero; pub use self::model::*; -use crate::name::Name; +use crate::{name::Name, Identifiable}; /// Collection of [`CustomParameter`]s pub(crate) type CustomParameters = btree_map::BTreeMap; diff --git a/data_model/src/peer.rs b/data_model/src/peer.rs index 72ab6b9a8e8..b11c3d469fc 100644 --- a/data_model/src/peer.rs +++ b/data_model/src/peer.rs @@ -13,7 +13,7 @@ use iroha_data_model_derive::model; use iroha_primitives::addr::SocketAddr; pub use self::model::*; -use crate::{PublicKey, Registered}; +use crate::{Identifiable, PublicKey, Registered}; #[model] mod model { diff --git a/data_model/src/role.rs b/data_model/src/role.rs index 834ef75e57b..871dae88246 100644 --- a/data_model/src/role.rs +++ b/data_model/src/role.rs @@ -8,7 +8,7 @@ use iroha_data_model_derive::model; pub use self::model::*; use crate::{ permission::{Permission, Permissions}, - Name, Registered, + Identifiable, Name, Registered, }; #[model] diff --git a/data_model/src/trigger.rs b/data_model/src/trigger.rs index 8be9660bc05..8893ff88084 100644 --- a/data_model/src/trigger.rs +++ b/data_model/src/trigger.rs @@ -17,7 +17,9 @@ use serde::{Deserialize, Serialize}; use serde_with::{DeserializeFromStr, SerializeDisplay}; pub use self::model::*; -use crate::{events::prelude::*, metadata::Metadata, transaction::Executable, Name, Registered}; +use crate::{ + events::prelude::*, metadata::Metadata, transaction::Executable, Identifiable, Name, Registered, +}; #[model] mod model { diff --git a/docs/source/references/schema.json b/docs/source/references/schema.json index 41442085923..e6a73f5bedc 100644 --- a/docs/source/references/schema.json +++ b/docs/source/references/schema.json @@ -244,7 +244,7 @@ "type": "AssetDefinitionId" }, { - "name": "type_", + "name": "type", "type": "AssetType" }, { @@ -2492,7 +2492,7 @@ "type": "AssetDefinitionId" }, { - "name": "type_", + "name": "type", "type": "AssetType" }, { diff --git a/schema/derive/src/lib.rs b/schema/derive/src/lib.rs index ee32e9bcf8c..9ca142f5073 100644 --- a/schema/derive/src/lib.rs +++ b/schema/derive/src/lib.rs @@ -129,6 +129,12 @@ impl FromVariant for IntoSchemaVariant { } } +#[derive(FromField, Default)] +#[darling(default, attributes(serde))] +struct SerdeFieldOpts { + rename: Option, +} + type IntoSchemaFields = darling::ast::Fields; #[derive(Debug, Clone)] @@ -140,7 +146,11 @@ struct IntoSchemaField { impl FromField for IntoSchemaField { fn from_field(field: &syn::Field) -> darling::Result { - let ident = field.ident.clone(); + let SerdeFieldOpts { rename } = SerdeFieldOpts::from_field(field).unwrap_or_default(); + + let ident = rename + .map(|rename| syn::Ident::new(&rename, Span::call_site())) + .or_else(|| field.ident.clone()); let ty = field.ty.clone(); let codec_attrs = CodecAttributes::from_attributes(&field.attrs)?;