fix(abigen): safe ident field names (#989)

This commit is contained in:
oblique 2022-03-05 20:25:03 +02:00 committed by GitHub
parent 88262de9ee
commit 46d7afd65b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -50,6 +50,13 @@
## ethers-contract-abigen ## ethers-contract-abigen
### Unreleased
- Generate correct bindings of struct's field names that are reserved words
[#989](https://github.com/gakonst/ethers-rs/pull/989).
### 0.6.0
- Add `MultiAbigen` to generate a series of contract bindings that can be kept in the repo - Add `MultiAbigen` to generate a series of contract bindings that can be kept in the repo
[#724](https://github.com/gakonst/ethers-rs/pull/724). [#724](https://github.com/gakonst/ethers-rs/pull/724).
- Add provided `event_derives` to call and event enums as well - Add provided `event_derives` to call and event enums as well

View File

@ -107,7 +107,7 @@ impl Context {
if is_tuple { if is_tuple {
fields.push(ty); fields.push(ty);
} else { } else {
let field_name = util::ident(&field.name().to_snake_case()); let field_name = util::safe_ident(&field.name().to_snake_case());
fields.push(quote! { pub #field_name: #ty }); fields.push(quote! { pub #field_name: #ty });
} }
} }
@ -155,7 +155,7 @@ impl Context {
let mut fields = Vec::with_capacity(sol_struct.fields().len()); let mut fields = Vec::with_capacity(sol_struct.fields().len());
let mut param_types = Vec::with_capacity(sol_struct.fields().len()); let mut param_types = Vec::with_capacity(sol_struct.fields().len());
for field in sol_struct.fields() { for field in sol_struct.fields() {
let field_name = util::ident(&field.name().to_snake_case()); let field_name = util::safe_ident(&field.name().to_snake_case());
match field.r#type() { match field.r#type() {
FieldType::Elementary(ty) => { FieldType::Elementary(ty) => {
param_types.push(ty.clone()); param_types.push(ty.clone());

View File

@ -518,3 +518,15 @@ fn can_gen_multi_etherscan() {
let _contract = MyContract::new(Address::default(), Arc::clone(&provider)); let _contract = MyContract::new(Address::default(), Arc::clone(&provider));
let _contract = MyContract2::new(Address::default(), provider); let _contract = MyContract2::new(Address::default(), provider);
} }
#[test]
fn can_gen_reserved_word_field_names() {
abigen!(
Test,
r#"[
struct Foo { uint256 ref; }
]"#,
);
let _foo = Foo { ref_: U256::default() };
}