Store type name of a field in event metadata (#654)

This commit is contained in:
German
2022-09-20 09:59:14 +01:00
committed by GitHub
parent 359c3dae41
commit 033ceb246f
2 changed files with 52 additions and 12 deletions
+43 -4
View File
@@ -297,6 +297,39 @@ impl PalletMetadata {
}
}
/// Metadata for specific field.
#[derive(Clone, Debug)]
pub struct EventFieldMetadata {
name: Option<String>,
type_name: Option<String>,
type_id: u32,
}
impl EventFieldMetadata {
pub fn new(name: Option<String>, type_name: Option<String>, type_id: u32) -> Self {
EventFieldMetadata {
name,
type_name,
type_id,
}
}
/// Get the name of the field.
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
// Get the type name of the field as it appears in the code
pub fn type_name(&self) -> Option<&str> {
self.type_name.as_deref()
}
/// Get the id of a type
pub fn type_id(&self) -> u32 {
self.type_id
}
}
/// Metadata for specific events.
#[derive(Clone, Debug)]
pub struct EventMetadata {
@@ -304,7 +337,7 @@ pub struct EventMetadata {
// behind an Arc to avoid lots of needless clones of it existing.
pallet: Arc<str>,
event: String,
fields: Vec<(Option<String>, u32)>,
fields: Vec<EventFieldMetadata>,
docs: Vec<String>,
}
@@ -319,8 +352,8 @@ impl EventMetadata {
&self.event
}
/// The names and types of each field in the event.
pub fn fields(&self) -> &[(Option<String>, u32)] {
/// The names, type names & types of each field in the event.
pub fn fields(&self) -> &[EventFieldMetadata] {
&self.fields
}
@@ -457,7 +490,13 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
fields: variant
.fields()
.iter()
.map(|f| (f.name().map(|n| n.to_owned()), f.ty().id()))
.map(|f| {
EventFieldMetadata::new(
f.name().map(|n| n.to_owned()),
f.type_name().map(|n| n.to_owned()),
f.ty().id(),
)
})
.collect(),
docs: variant.docs().to_vec(),
},