Skip to content

Commit

Permalink
sanitize abi struct Name
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Jul 3, 2024
1 parent 06f47e0 commit f94bf1b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
19 changes: 19 additions & 0 deletions ethfull/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ethfull
import (
"encoding/hex"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -82,6 +83,9 @@ func (a *ABI) BuildEventModels() (out []codegenEvent, err error) {
}
event.Signature()

// Sanitize abi struct name base on rust proto-gen sanitizer
rustABIStructName = sanitizeABIStructName(rustABIStructName)

protoFieldName := xstrings.ToSnakeCase(pluralizerSingleton.Plural(rustABIStructName))
// prost will do a to_lower_camel_case() on any struct name
rustGeneratedStructName := xstrings.ToCamelCase(xstrings.ToSnakeCase(rustABIStructName))
Expand Down Expand Up @@ -119,6 +123,18 @@ func (a *ABI) BuildEventModels() (out []codegenEvent, err error) {
return
}

func sanitizeABIStructName(rustABIStructName string) string {
reg := regexp.MustCompile("(_+)")
rustABIStructName = reg.ReplaceAllStringFunc(rustABIStructName, func(s string) string {
if len(s) > 1 {
return "_u"
}
return s
})

return rustABIStructName
}

func (a *ABI) BuildCallModels() (out []codegenCall, err error) {
abi := a.abi
names := maps.Keys(abi.FunctionsByNameMap)
Expand Down Expand Up @@ -149,6 +165,9 @@ func (a *ABI) BuildCallModels() (out []codegenCall, err error) {
}
}

// Sanitize abi struct name base on rust proto-gen sanitizer
rustABIStructName = sanitizeABIStructName(rustABIStructName)

protoFieldName := "call_" + xstrings.ToSnakeCase(pluralizerSingleton.Plural(rustABIStructName))
// prost will do a to_lower_camel_case() on any struct name
rustGeneratedStructName := xstrings.ToCamelCase(xstrings.ToSnakeCase(rustABIStructName))
Expand Down
13 changes: 12 additions & 1 deletion ethfull/templates/build.rs.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ fn main() -> Result<(), anyhow::Error> {
let regex = Regex::new(r#"("\w+"\s?:\s?")_(\w+")"#).unwrap();
let sanitized_abi_file = regex.replace_all(contents.as_str(), "${1}u_${2}");

Abigen::from_bytes("Contract", sanitized_abi_file.as_bytes())?
// sanitize fields and attributes with multiple consecutive underscores
let re = Regex::new(r"_+").unwrap();

let re_sanitized_abi_file = re.replace_all(&sanitized_abi_file, |caps: &regex::Captures| {
if caps[0].len() > 1 {
"_u".to_string()
} else {
caps[0].to_string()
}
});

Abigen::from_bytes("Contract", re_sanitized_abi_file.as_bytes())?
.generate()?
.write_to_file(file_output_names[i])?;

Expand Down

0 comments on commit f94bf1b

Please sign in to comment.