Skip to content

Commit

Permalink
fix sanitize func
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Jul 3, 2024
1 parent f94bf1b commit decf642
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
13 changes: 0 additions & 13 deletions ethfull/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ethfull
import (
"encoding/hex"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -123,18 +122,6 @@ 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
20 changes: 20 additions & 0 deletions ethfull/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ethfull

import (
"regexp"
"strings"
)

func sanitizeABIStructName(rustABIStructName string) string {
reg := regexp.MustCompile("_+")

result := reg.ReplaceAllStringFunc(rustABIStructName, func(s string) string {
count := len(s)

replacement := strings.Repeat("_u", count-1) + "_"

return replacement
})

return result
}
42 changes: 42 additions & 0 deletions ethfull/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ethfull

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestSanitizeStructABI(t *testing.T) {
cases := []struct {
name string
inputStructAbiName string
expectedName string
}{
{
inputStructAbiName: "foo_bar",
expectedName: "foo_bar",
},
{
inputStructAbiName: "foo___bar",
expectedName: "foo_u_u_bar",
},
{
inputStructAbiName: "__foo_bar",
expectedName: "_u_foo_bar",
},
{
inputStructAbiName: "foobar__",
expectedName: "foobar_u_",
},
{
inputStructAbiName: "__foobar",
expectedName: "_u_foobar",
},
}

for _, c := range cases {
t.Run(c.inputStructAbiName, func(t *testing.T) {
result := sanitizeABIStructName(c.inputStructAbiName)
require.Equal(t, c.expectedName, result)
})
}
}
8 changes: 3 additions & 5 deletions ethfull/templates/build.rs.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ fn main() -> Result<(), anyhow::Error> {
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()
}
let count = caps[0].len();
let replacement = format!("{}_", "_u".repeat(count - 1));
replacement
});

Abigen::from_bytes("Contract", re_sanitized_abi_file.as_bytes())?
Expand Down

0 comments on commit decf642

Please sign in to comment.