Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: report schema type id in no_extra_or_missing_schemas test #5054

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions crates/iroha_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ use core::{
pub use iroha_schema_derive::*;
use serde::Serialize;

/// An entry in the schema map
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MetaMapEntry {
/// A unique identifier of the type
pub type_id: String,
/// The name under which the type is exposed in the schema
pub type_name: String,
/// Details about the type representation
pub metadata: Metadata,
}

/// Helper struct for building a full schema
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MetaMap(pub(crate) btree_map::BTreeMap<core::any::TypeId, (String, Metadata)>);
pub struct MetaMap(pub(crate) btree_map::BTreeMap<core::any::TypeId, MetaMapEntry>);

impl PartialEq<btree_map::BTreeMap<core::any::TypeId, (String, Metadata)>> for MetaMap {
fn eq(&self, other: &btree_map::BTreeMap<core::any::TypeId, (String, Metadata)>) -> bool {
impl PartialEq<btree_map::BTreeMap<core::any::TypeId, MetaMapEntry>> for MetaMap {
fn eq(&self, other: &btree_map::BTreeMap<core::any::TypeId, MetaMapEntry>) -> bool {
self.0.eq(other)
}
}
Expand All @@ -53,20 +64,26 @@ impl MetaMap {
self.0.remove(&Self::key::<K>()).is_some()
}
/// Insert a key-value pair into the map.
pub fn insert<K: IntoSchema>(&mut self, v: Metadata) -> bool {
pub fn insert<K: IntoSchema>(&mut self, metadata: Metadata) -> bool {
self.0
.insert(Self::key::<K>(), (K::type_name(), v))
.insert(Self::key::<K>(), {
MetaMapEntry {
type_id: K::id(),
type_name: K::type_name(),
metadata,
}
})
.is_none()
}
/// Return a reference to the value corresponding to the [`core::any::TypeId`] of the schema type
pub fn get<K: 'static>(&self) -> Option<&Metadata> {
self.0.get(&Self::key::<K>()).map(|(_, schema)| schema)
self.0.get(&Self::key::<K>()).map(|value| &value.metadata)
}
}

impl IntoIterator for MetaMap {
type Item = (core::any::TypeId, (String, Metadata));
type IntoIter = btree_map::IntoIter<core::any::TypeId, (String, Metadata)>;
type Item = (core::any::TypeId, MetaMapEntry);
type IntoIter = btree_map::IntoIter<core::any::TypeId, MetaMapEntry>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand Down
9 changes: 7 additions & 2 deletions crates/iroha_schema/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<T: ?Sized> WithContext<'_, '_, T> {
.0
.get(&type_id)
.unwrap_or_else(|| panic!("Failed to find type id `{:?}`", type_id))
.0
.type_name
}
}

Expand Down Expand Up @@ -315,7 +315,12 @@ impl Serialize for MetaMap {
let mut duplicates = BTreeMap::new();

let mut sorted_map = BTreeMap::new();
for (type_name, schema) in self.0.values() {
for MetaMapEntry {
type_name,
metadata: schema,
..
} in self.0.values()
{
if let Some(duplicate) = sorted_map.insert(type_name, schema) {
// NOTE: It's ok to serialize two types to the same name if they
// are represented by the same schema, i.e. for transparent types
Expand Down
42 changes: 31 additions & 11 deletions crates/iroha_schema/tests/enum_with_default_discriminants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,37 @@ fn default_discriminants() {
let expected = vec![
(
TypeId::of::<core::result::Result<bool, alloc::string::String>>(),
(
"Result<bool, String>".to_owned(),
Result(ResultMeta {
MetaMapEntry {
type_id: "Result<bool, String>".to_owned(),
type_name: "Result<bool, String>".to_owned(),
metadata: Result(ResultMeta {
ok: TypeId::of::<bool>(),
err: TypeId::of::<alloc::string::String>(),
}),
),
},
),
(
TypeId::of::<alloc::string::String>(),
("String".to_owned(), String),
MetaMapEntry {
type_id: "String".to_owned(),
type_name: "String".to_owned(),
metadata: String,
},
),
(
TypeId::of::<bool>(),
MetaMapEntry {
type_id: "bool".to_owned(),
type_name: "bool".to_owned(),
metadata: Bool,
},
),
(TypeId::of::<bool>(), ("bool".to_owned(), Bool)),
(
TypeId::of::<Foo>(),
(
"Foo".to_owned(),
Enum(EnumMeta {
MetaMapEntry {
type_id: "Foo".to_owned(),
type_name: "Foo".to_owned(),
metadata: Enum(EnumMeta {
variants: vec![
EnumVariant {
tag: "Variant1".to_owned(),
Expand All @@ -68,9 +81,16 @@ fn default_discriminants() {
},
],
}),
),
},
),
(
TypeId::of::<i32>(),
MetaMapEntry {
type_id: "i32".to_owned(),
type_name: "i32".to_owned(),
metadata: Int(FixedWidth),
},
),
(TypeId::of::<i32>(), ("i32".to_owned(), Int(FixedWidth))),
]
.into_iter()
.collect::<BTreeMap<_, _>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ fn discriminant() {

let expected_meta = vec![(
core::any::TypeId::of::<Foo>(),
(
"Foo".to_owned(),
Metadata::Enum(EnumMeta {
MetaMapEntry {
type_id: "Foo".to_owned(),
type_name: "Foo".to_owned(),
metadata: Metadata::Enum(EnumMeta {
variants: vec![
EnumVariant {
tag: "A".to_owned(),
Expand All @@ -44,7 +45,7 @@ fn discriminant() {
},
],
}),
),
},
)]
.into_iter()
.collect::<BTreeMap<_, _>>();
Expand Down
9 changes: 5 additions & 4 deletions crates/iroha_schema/tests/fieldless_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ fn discriminant() {

let expected_meta = vec![(
core::any::TypeId::of::<Foo>(),
(
"Foo".to_owned(),
Metadata::Enum(EnumMeta {
MetaMapEntry {
type_id: "Foo".to_owned(),
type_name: "Foo".to_owned(),
metadata: Metadata::Enum(EnumMeta {
variants: vec![
EnumVariant {
tag: "A".to_owned(),
Expand All @@ -42,7 +43,7 @@ fn discriminant() {
},
],
}),
),
},
)]
.into_iter()
.collect::<BTreeMap<_, _>>();
Expand Down
84 changes: 70 additions & 14 deletions crates/iroha_schema/tests/numbers_compact_and_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,50 @@ fn compact() {
let expected = vec![
(
TypeId::of::<iroha_schema::Compact<u128>>(),
("Compact<u128>".to_owned(), Int(IntMode::Compact)),
MetaMapEntry {
type_id: "Compact<u128>".to_owned(),
type_name: "Compact<u128>".to_owned(),
metadata: Int(Compact),
},
),
(
TypeId::of::<iroha_schema::Compact<u16>>(),
("Compact<u16>".to_owned(), Int(IntMode::Compact)),
MetaMapEntry {
type_id: "Compact<u16>".to_owned(),
type_name: "Compact<u16>".to_owned(),
metadata: Int(Compact),
},
),
(
TypeId::of::<iroha_schema::Compact<u32>>(),
("Compact<u32>".to_owned(), Int(IntMode::Compact)),
MetaMapEntry {
type_id: "Compact<u32>".to_owned(),
type_name: "Compact<u32>".to_owned(),
metadata: Int(Compact),
},
),
(
TypeId::of::<iroha_schema::Compact<u64>>(),
("Compact<u64>".to_owned(), Int(IntMode::Compact)),
MetaMapEntry {
type_id: "Compact<u64>".to_owned(),
type_name: "Compact<u64>".to_owned(),
metadata: Int(Compact),
},
),
(
TypeId::of::<iroha_schema::Compact<u8>>(),
("Compact<u8>".to_owned(), Int(IntMode::Compact)),
MetaMapEntry {
type_id: "Compact<u8>".to_owned(),
type_name: "Compact<u8>".to_owned(),
metadata: Int(Compact),
},
),
(
TypeId::of::<Foo>(),
(
"Foo".to_owned(),
Struct(NamedFieldsMeta {
MetaMapEntry {
type_id: "Foo".to_owned(),
type_name: "Foo".to_owned(),
metadata: Struct(NamedFieldsMeta {
declarations: vec![
Declaration {
name: "u8_compact".to_owned(),
Expand Down Expand Up @@ -100,13 +121,48 @@ fn compact() {
},
],
}),
),
},
),
(
TypeId::of::<u128>(),
MetaMapEntry {
type_id: "u128".to_owned(),
type_name: "u128".to_owned(),
metadata: Int(FixedWidth),
},
),
(
TypeId::of::<u16>(),
MetaMapEntry {
type_id: "u16".to_owned(),
type_name: "u16".to_owned(),
metadata: Int(FixedWidth),
},
),
(
TypeId::of::<u32>(),
MetaMapEntry {
type_id: "u32".to_owned(),
type_name: "u32".to_owned(),
metadata: Int(FixedWidth),
},
),
(
TypeId::of::<u64>(),
MetaMapEntry {
type_id: "u64".to_owned(),
type_name: "u64".to_owned(),
metadata: Int(FixedWidth),
},
),
(
TypeId::of::<u8>(),
MetaMapEntry {
type_id: "u8".to_owned(),
type_name: "u8".to_owned(),
metadata: Int(FixedWidth),
},
),
(TypeId::of::<u128>(), ("u128".to_owned(), Int(FixedWidth))),
(TypeId::of::<u16>(), ("u16".to_owned(), Int(FixedWidth))),
(TypeId::of::<u32>(), ("u32".to_owned(), Int(FixedWidth))),
(TypeId::of::<u64>(), ("u64".to_owned(), Int(FixedWidth))),
(TypeId::of::<u8>(), ("u8".to_owned(), Int(FixedWidth))),
]
.into_iter()
.collect::<BTreeMap<_, _>>();
Expand Down
21 changes: 18 additions & 3 deletions crates/iroha_schema/tests/struct_with_generic_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,29 @@ fn check_generic() {
}],
});
let expected = vec![
(TypeId::of::<bool>(), ("bool".to_owned(), Bool)),
(
TypeId::of::<bool>(),
MetaMapEntry {
type_id: "bool".to_owned(),
type_name: "bool".to_owned(),
metadata: Bool,
},
),
(
TypeId::of::<core::option::Option<bool>>(),
("Option<bool>".to_owned(), Option(TypeId::of::<bool>())),
MetaMapEntry {
type_id: "Option<bool>".to_owned(),
type_name: "Option<bool>".to_owned(),
metadata: Option(TypeId::of::<bool>()),
},
),
(
TypeId::of::<Foo<bool>>(),
("Foo<bool>".to_owned(), expected_struct),
MetaMapEntry {
type_id: "Foo<bool>".to_owned(),
type_name: "Foo<bool>".to_owned(),
metadata: expected_struct,
},
),
]
.into_iter()
Expand Down
30 changes: 23 additions & 7 deletions crates/iroha_schema/tests/struct_with_named_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,37 @@ fn named_fields() {
let expected = vec![
(
TypeId::of::<alloc::string::String>(),
("String".to_owned(), String),
MetaMapEntry {
type_id: "String".to_owned(),
type_name: "String".to_owned(),
metadata: String,
},
),
(
TypeId::of::<alloc::vec::Vec<alloc::string::String>>(),
(
"Vec<String>".to_owned(),
Vec(VecMeta {
MetaMapEntry {
type_id: "Vec<String>".to_owned(),
type_name: "Vec<String>".to_owned(),
metadata: Vec(VecMeta {
ty: TypeId::of::<alloc::string::String>(),
}),
),
},
),
(
TypeId::of::<i32>(),
MetaMapEntry {
type_id: "i32".to_owned(),
type_name: "i32".to_owned(),
metadata: Int(FixedWidth),
},
),
(TypeId::of::<i32>(), ("i32".to_owned(), Int(FixedWidth))),
(
TypeId::of::<Command>(),
("Command".to_owned(), expected_struct),
MetaMapEntry {
type_id: "Command".to_owned(),
type_name: "Command".to_owned(),
metadata: expected_struct,
},
),
]
.into_iter()
Expand Down
Loading
Loading