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

Normalize function name to eliminate underscores #22

Merged
merged 1 commit into from
Mar 30, 2020
Merged
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
31 changes: 21 additions & 10 deletions tools/generators/ethereum/contract_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func buildContractInfo(
payableMethods := make(map[string]struct{})
for _, methodPayableInfo := range payableInfo {
if methodPayableInfo.Payable {
name := methodPayableInfo.Name
_, ok := payableMethods[name]
normalizedName := toCamelCase(methodPayableInfo.Name)
_, ok := payableMethods[normalizedName]
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", methodPayableInfo.Name, idx)
_, ok = payableMethods[name]
normalizedName = fmt.Sprintf("%s%d", normalizedName, idx)
_, ok = payableMethods[normalizedName]
}
payableMethods[name] = struct{}{}
payableMethods[normalizedName] = struct{}{}
}
}

Expand Down Expand Up @@ -138,12 +138,13 @@ func buildMethodInfo(
constMethods = make([]methodInfo, 0, len(methodsByName))

for name, method := range methodsByName {
normalizedName := toCamelCase(name)
dashedName := strings.ToLower(string(shortVarRegexp.ReplaceAll(
[]byte(name),
[]byte(normalizedName),
[]byte("-$0"),
)))

_, payable := payableMethods[name]
_, payable := payableMethods[normalizedName]
commandCallable := true

modifiers := make([]string, 0, 0)
Expand Down Expand Up @@ -195,7 +196,7 @@ func buildMethodInfo(
returned := returnInfo{}
if len(method.Outputs) > 1 {
returned.Multi = true
returned.Type = strings.Replace(name, "get", "", 1)
returned.Type = strings.Replace(normalizedName, "get", "", 1)

for _, output := range method.Outputs {
goType := output.Type.Type.String()
Expand All @@ -216,8 +217,8 @@ func buildMethodInfo(
}

info := methodInfo{
uppercaseFirst(name),
lowercaseFirst(name),
uppercaseFirst(normalizedName),
lowercaseFirst(normalizedName),
dashedName,
modifierString,
payable,
Expand Down Expand Up @@ -280,3 +281,13 @@ func uppercaseFirst(str string) string {
func lowercaseFirst(str string) string {
return strings.ToLower(str[0:1]) + str[1:]
}

func toCamelCase(input string) string {
parts := strings.Split(input, "_")
for i, s := range parts {
if len(s) > 0 {
parts[i] = strings.ToUpper(s[:1]) + s[1:]
}
}
return strings.Join(parts, "")
}