Skip to content

Commit

Permalink
codegen: fix compact unnamed fields (paritytech#327)
Browse files Browse the repository at this point in the history
* codegen: fix compact unnamed fields

* Fmt

* Regenerate polkadot codegen example.

* Ignore clippy for generated code

* Limit the number of test threads

* Revert "Limit the number of test threads"

This reverts commit f1947dc.

* Delete duplicate node_runtime metadata

* Update node_runtime metadata

* Update balances events
  • Loading branch information
ascjones authored Nov 25, 2021
1 parent d6840df commit e2ab01d
Show file tree
Hide file tree
Showing 5 changed files with 15,003 additions and 1 deletion.
67 changes: 67 additions & 0 deletions codegen/src/types/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,73 @@ fn generate_enum() {
)
}

#[test]
fn compact_fields() {
#[allow(unused)]
#[derive(TypeInfo)]
struct S {
#[codec(compact)]
a: u32,
}

#[allow(unused)]
#[derive(TypeInfo)]
struct TupleStruct(#[codec(compact)] u32);

#[allow(unused)]
#[derive(TypeInfo)]
enum E {
A {
#[codec(compact)]
a: u32,
},
B(#[codec(compact)] u32),
}

let mut registry = Registry::new();
registry.register_type(&meta_type::<S>());
registry.register_type(&meta_type::<TupleStruct>());
registry.register_type(&meta_type::<E>());
let portable_types: PortableRegistry = registry.into();

let type_gen = TypeGenerator::new(
&portable_types,
"root",
Default::default(),
Default::default(),
);
let types = type_gen.generate_types_mod();
let tests_mod = get_mod(&types, MOD_PATH).unwrap();

assert_eq!(
tests_mod.into_token_stream().to_string(),
quote! {
pub mod tests {
use super::root;
#[derive(::subxt::codec::Encode, ::subxt::codec::Decode)]
pub enum E {
# [codec (index = 0)]
A {
#[codec(compact)]
a: ::core::primitive::u32,
},
# [codec (index = 1)]
B( #[codec(compact)] ::core::primitive::u32,),
}

#[derive(::subxt::codec::Encode, ::subxt::codec::Decode)]
pub struct S {
#[codec(compact)] pub a: ::core::primitive::u32,
}

#[derive(::subxt::codec::Encode, ::subxt::codec::Decode)]
pub struct TupleStruct(#[codec(compact)] pub ::core::primitive::u32,);
}
}
.to_string()
)
}

#[test]
fn generate_array_field() {
#[allow(unused)]
Expand Down
7 changes: 6 additions & 1 deletion codegen/src/types/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl<'a> TypeDefGen<'a> {
let mut fields_tokens = type_paths
.iter()
.map(|(ty, ty_name)| {
match ty_name {
let field_type = match ty_name {
Some(ty_name) => {
let ty = ty_toks(ty_name, ty);
if is_struct {
Expand All @@ -291,6 +291,11 @@ impl<'a> TypeDefGen<'a> {
None => {
quote! { #ty }
}
};
if ty.is_compact() {
quote!( #[codec(compact)] #field_type )
} else {
quote!( #field_type )
}
})
.collect::<Vec<_>>();
Expand Down
1 change: 1 addition & 0 deletions tests/integration/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
/// - run `polkadot --dev --tmp` node locally
/// - `cargo run --release -p subxt-cli -- codegen | rustfmt --edition=2018 --emit=stdout > tests/integration/codegen/polkadot.rs`
#[rustfmt::skip]
#[allow(clippy::all)]
mod polkadot;
Loading

0 comments on commit e2ab01d

Please sign in to comment.