Skip to content

Commit d965040

Browse files
committed
Output function name with the first letter lowercase from camelCase
We introduced `toCamelCase` to use the same function name normalization strategy as go-ethereum and support functions like `__beaconCallback`. This change broke `ethereum` command. The subcommands for ethereum methods were being prefixed by `-`. urfave/cli, the library we use for CLI definition and parsing, does not fail if subcommands prefixed by - are used. However, it does fail to recognize them; instead, it tries to recognize them as flags, and flags with that name have not been defined. `toCamelCase` used to uppercase the first letter. Later, in `buildMethodInfo` where we create `dashedName` we were prefixing the function name with `-` with uppercase-first letter. The fix is easy, we need to ensure the first letter stays lowercase during the normalization. Later, in `buildMethodInfo`, we specify explicitly where do we want lowercase and uppercase with `uppercaseFirst` and `lowercaseFirst`.
1 parent bf7dfa6 commit d965040

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

tools/generators/ethereum/contract_parsing.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func buildContractInfo(
9595
payableMethods := make(map[string]struct{})
9696
for _, methodPayableInfo := range payableInfo {
9797
if methodPayableInfo.Payable {
98-
normalizedName := toCamelCase(methodPayableInfo.Name)
98+
normalizedName := camelCase(methodPayableInfo.Name)
9999
_, ok := payableMethods[normalizedName]
100100
for idx := 0; ok; idx++ {
101101
normalizedName = fmt.Sprintf("%s%d", normalizedName, idx)
@@ -138,7 +138,7 @@ func buildMethodInfo(
138138
constMethods = make([]methodInfo, 0, len(methodsByName))
139139

140140
for name, method := range methodsByName {
141-
normalizedName := toCamelCase(name)
141+
normalizedName := camelCase(name)
142142
dashedName := strings.ToLower(string(shortVarRegexp.ReplaceAll(
143143
[]byte(normalizedName),
144144
[]byte("-$0"),
@@ -278,24 +278,22 @@ func uppercaseFirst(str string) string {
278278
if len(str) == 0 {
279279
return str
280280
}
281-
282281
return strings.ToUpper(str[0:1]) + str[1:]
283282
}
284283

285284
func lowercaseFirst(str string) string {
286285
if len(str) == 0 {
287286
return str
288287
}
289-
290288
return strings.ToLower(str[0:1]) + str[1:]
291289
}
292290

293-
func toCamelCase(input string) string {
291+
func camelCase(input string) string {
294292
parts := strings.Split(input, "_")
295293
for i, s := range parts {
296294
if len(s) > 0 {
297295
parts[i] = strings.ToUpper(s[:1]) + s[1:]
298296
}
299297
}
300-
return strings.Join(parts, "")
298+
return lowercaseFirst(strings.Join(parts, ""))
301299
}

tools/generators/ethereum/contract_parsing_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,44 @@ func TestUppercaseFirst(t *testing.T) {
6767
})
6868
}
6969
}
70+
71+
func TestCamelCase(t *testing.T) {
72+
var tests = map[string]struct {
73+
input string
74+
expected string
75+
}{
76+
"empty string": {
77+
input: "",
78+
expected: "",
79+
},
80+
"no underscores": {
81+
input: "HelloWorld",
82+
expected: "helloWorld",
83+
},
84+
"with underscores": {
85+
input: "hello_world",
86+
expected: "helloWorld",
87+
},
88+
"one underscore first": {
89+
input: "_beacon_callback",
90+
expected: "beaconCallback",
91+
},
92+
"multiple underscores first": {
93+
input: "__beacon_callback",
94+
expected: "beaconCallback",
95+
},
96+
}
97+
98+
for testName, test := range tests {
99+
t.Run(testName, func(t *testing.T) {
100+
actual := camelCase(test.input)
101+
if actual != test.expected {
102+
t.Errorf(
103+
"unexpected output\nexpected: [%v]\nactual: [%v]",
104+
test.expected,
105+
actual,
106+
)
107+
}
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)