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

feat(sol-macro): expand fields with attrs #263

Merged
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
2 changes: 1 addition & 1 deletion crates/sol-macro/src/expand/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, error: &ItemError) -> Result<TokenStream>
#[allow(non_camel_case_types, non_snake_case)]
#[derive(Clone)]
pub struct #name {
#(pub #fields,)*
#(#fields),*
}

#[allow(non_camel_case_types, non_snake_case, clippy::style)]
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-macro/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
#[allow(non_camel_case_types, non_snake_case)]
#[derive(Clone)]
pub struct #call_name {
#(pub #call_fields,)*
#(#call_fields),*
}

#(#return_attrs)*
#[allow(non_camel_case_types, non_snake_case)]
#[derive(Clone)]
pub struct #return_name {
#(pub #return_fields,)*
#(#return_fields),*
}

#[allow(non_camel_case_types, non_snake_case, clippy::style)]
Expand Down
12 changes: 9 additions & 3 deletions crates/sol-macro/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,15 +492,21 @@ fn expand_fields<P>(params: &Parameters<P>) -> impl Iterator<Item = TokenStream>
params
.iter()
.enumerate()
.map(|(i, var)| expand_field(i, &var.ty, var.name.as_ref()))
.map(|(i, var)| expand_field(i, &var.ty, var.name.as_ref(), var.attrs.as_ref()))
}

/// Expands a single parameter into a struct field.
fn expand_field(i: usize, ty: &Type, name: Option<&SolIdent>) -> TokenStream {
fn expand_field(
i: usize,
ty: &Type,
name: Option<&SolIdent>,
attrs: &Vec<Attribute>,
) -> TokenStream {
let name = anon_name((i, name));
let ty = expand_type(ty);
quote! {
#name: <#ty as ::alloy_sol_types::SolType>::RustType
#(#attrs)*
pub #name: <#ty as ::alloy_sol_types::SolType>::RustType
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sol-macro/src/expand/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, s: &ItemStruct) -> Result<TokenStream> {
#[allow(non_camel_case_types, non_snake_case)]
#[derive(Clone)]
pub struct #name {
#(pub #fields),*
#(#fields),*
}

#[allow(non_camel_case_types, non_snake_case, clippy::style)]
Expand Down
4 changes: 3 additions & 1 deletion crates/sol-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ hex.workspace = true
serde = { workspace = true, optional = true, features = ["derive"] }

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["arbitrary"] }
alloy-primitives = { workspace = true, features = ["arbitrary", "serde"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

hex-literal.workspace = true
proptest.workspace = true
Expand Down
25 changes: 25 additions & 0 deletions crates/sol-types/tests/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,31 @@ fn abigen_sol_multicall() {
);
}

#[test]
fn struct_derive_with_field_attrs() {
use serde::Serialize;
use serde_json::{self, Value};
sol! {
#[derive(Serialize, Default)]
struct MyStruct {
#[serde(skip)]
uint256 a;
bytes32 b;
address[] c;
}
}

assert_eq!(
serde_json::from_str::<Value>(
serde_json::to_string(&MyStruct::default())
.unwrap()
.as_str()
)
.unwrap()["a"],
Value::Null
);
}

#[test]
#[cfg(feature = "json")]
fn abigen_json_large_array() {
Expand Down