Skip to content

Commit 8d91818

Browse files
committed
clean lang selection leftovers
1 parent 0e7bccb commit 8d91818

File tree

4 files changed

+52
-118
lines changed

4 files changed

+52
-118
lines changed

accounts/abi/bind/bind.go

Lines changed: 45 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ import (
3333
"github.com/ethereum/go-ethereum/log"
3434
)
3535

36-
// Lang is a target programming language selector to generate bindings for.
37-
type Lang int
38-
39-
const (
40-
LangGo Lang = iota
41-
)
42-
4336
func isKeyWord(arg string) bool {
4437
switch arg {
4538
case "break":
@@ -81,38 +74,33 @@ func isKeyWord(arg string) bool {
8174
// to be used as is in client code, but rather as an intermediate struct which
8275
// enforces compile time type safety and naming convention opposed to having to
8376
// manually maintain hard coded strings that break on runtime.
84-
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (string, error) {
85-
data, err := bind(types, abis, bytecodes, fsigs, pkg, lang, libs, aliases)
77+
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, libs map[string]string, aliases map[string]string) (string, error) {
78+
data, err := bind(types, abis, bytecodes, fsigs, pkg, libs, aliases)
8679
if err != nil {
8780
return "", err
8881
}
8982
buffer := new(bytes.Buffer)
9083

9184
funcs := map[string]interface{}{
92-
"bindtype": bindType[lang],
93-
"bindtopictype": bindTopicType[lang],
94-
"namedtype": namedType[lang],
85+
"bindtype": bindType,
86+
"bindtopictype": bindTopicType,
9587
"capitalise": capitalise,
9688
"decapitalise": decapitalise,
9789
}
98-
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource[lang]))
90+
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource))
9991
if err := tmpl.Execute(buffer, data); err != nil {
10092
return "", err
10193
}
102-
// For Go bindings pass the code through gofmt to clean it up
103-
if lang == LangGo {
104-
code, err := format.Source(buffer.Bytes())
105-
if err != nil {
106-
return "", fmt.Errorf("%v\n%s", err, buffer)
107-
}
108-
return string(code), nil
94+
// Pass the code through gofmt to clean it up
95+
code, err := format.Source(buffer.Bytes())
96+
if err != nil {
97+
return "", fmt.Errorf("%v\n%s", err, buffer)
10998
}
110-
// For all others just return as is for now
111-
return buffer.String(), nil
99+
return string(code), nil
112100
}
113101

114-
func BindV2(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (string, error) {
115-
data, err := bind(types, abis, bytecodes, fsigs, pkg, lang, libs, aliases)
102+
func BindV2(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, libs map[string]string, aliases map[string]string) (string, error) {
103+
data, err := bind(types, abis, bytecodes, fsigs, pkg, libs, aliases)
116104
if err != nil {
117105
return "", err
118106
}
@@ -153,29 +141,24 @@ func BindV2(types []string, abis []string, bytecodes []string, fsigs []map[strin
153141
}
154142
buffer := new(bytes.Buffer)
155143
funcs := map[string]interface{}{
156-
"bindtype": bindType[lang],
157-
"bindtopictype": bindTopicType[lang],
158-
"namedtype": namedType[lang],
144+
"bindtype": bindType,
145+
"bindtopictype": bindTopicType,
159146
"capitalise": capitalise,
160147
"decapitalise": decapitalise,
161148
}
162-
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSourceV2[lang]))
149+
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSourceV2))
163150
if err := tmpl.Execute(buffer, data); err != nil {
164151
return "", err
165152
}
166-
// For Go bindings pass the code through gofmt to clean it up
167-
if lang == LangGo {
168-
code, err := format.Source(buffer.Bytes())
169-
if err != nil {
170-
return "", fmt.Errorf("%v\n%s", err, buffer)
171-
}
172-
return string(code), nil
153+
// Pass the code through gofmt to clean it up
154+
code, err := format.Source(buffer.Bytes())
155+
if err != nil {
156+
return "", fmt.Errorf("%v\n%s", err, buffer)
173157
}
174-
// For all others just return as is for now
175-
return buffer.String(), nil
158+
return string(code), nil
176159
}
177160

178-
func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (*tmplData, error) {
161+
func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, libs map[string]string, aliases map[string]string) (*tmplData, error) {
179162
var (
180163
// contracts is the map of each individual contract requested binding
181164
contracts = make(map[string]*tmplContract)
@@ -219,14 +202,14 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
219202

220203
for _, input := range evmABI.Constructor.Inputs {
221204
if hasStruct(input.Type) {
222-
bindStructType[lang](input.Type, structs)
205+
bindStructType(input.Type, structs)
223206
}
224207
}
225208

226209
for _, original := range evmABI.Methods {
227210
// Normalize the method for capital cases and non-anonymous inputs/outputs
228211
normalized := original
229-
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
212+
normalizedName := methodNormalizer(alias(aliases, original.Name))
230213

231214
// Ensure there is no duplicated identifier
232215
var identifiers = callIdentifiers
@@ -246,7 +229,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
246229
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
247230
}
248231
if hasStruct(input.Type) {
249-
bindStructType[lang](input.Type, structs)
232+
bindStructType(input.Type, structs)
250233
}
251234
}
252235
normalized.Outputs = make([]abi.Argument, len(original.Outputs))
@@ -256,7 +239,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
256239
normalized.Outputs[j].Name = capitalise(output.Name)
257240
}
258241
if hasStruct(output.Type) {
259-
bindStructType[lang](output.Type, structs)
242+
bindStructType(output.Type, structs)
260243
}
261244
}
262245
// Append the methods to the call or transact lists
@@ -275,7 +258,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
275258
normalized := original
276259

277260
// Ensure there is no duplicated identifier
278-
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
261+
normalizedName := methodNormalizer(alias(aliases, original.Name))
279262
if eventIdentifiers[normalizedName] {
280263
return nil, fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
281264
}
@@ -299,7 +282,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
299282
normalized.Inputs[j].Name = fmt.Sprintf("%s%d", normalized.Inputs[j].Name, index)
300283
}
301284
if hasStruct(input.Type) {
302-
bindStructType[lang](input.Type, structs)
285+
bindStructType(input.Type, structs)
303286
}
304287
}
305288
// Append the event to the accumulator list
@@ -360,14 +343,8 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
360343
return data, nil
361344
}
362345

363-
// bindType is a set of type binders that convert Solidity types to some supported
364-
// programming language types.
365-
var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
366-
LangGo: bindTypeGo,
367-
}
368-
369-
// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones.
370-
func bindBasicTypeGo(kind abi.Type) string {
346+
// bindBasicType converts basic solidity types(except array, slice and tuple) to Go ones.
347+
func bindBasicType(kind abi.Type) string {
371348
switch kind.T {
372349
case abi.AddressTy:
373350
return "common.Address"
@@ -390,32 +367,26 @@ func bindBasicTypeGo(kind abi.Type) string {
390367
}
391368
}
392369

393-
// bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
370+
// bindType converts solidity types to Go ones. Since there is no clear mapping
394371
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
395372
// mapped will use an upscaled type (e.g. BigDecimal).
396-
func bindTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
373+
func bindType(kind abi.Type, structs map[string]*tmplStruct) string {
397374
switch kind.T {
398375
case abi.TupleTy:
399376
return structs[kind.TupleRawName+kind.String()].Name
400377
case abi.ArrayTy:
401-
return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem, structs)
378+
return fmt.Sprintf("[%d]", kind.Size) + bindType(*kind.Elem, structs)
402379
case abi.SliceTy:
403-
return "[]" + bindTypeGo(*kind.Elem, structs)
380+
return "[]" + bindType(*kind.Elem, structs)
404381
default:
405-
return bindBasicTypeGo(kind)
382+
return bindBasicType(kind)
406383
}
407384
}
408385

409-
// bindTopicType is a set of type binders that convert Solidity types to some
410-
// supported programming language topic types.
411-
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
412-
LangGo: bindTopicTypeGo,
413-
}
414-
415-
// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
386+
// bindTopicType converts a Solidity topic type to a Go one. It is almost the same
416387
// functionality as for simple types, but dynamic types get converted to hashes.
417-
func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
418-
bound := bindTypeGo(kind, structs)
388+
func bindTopicType(kind abi.Type, structs map[string]*tmplStruct) string {
389+
bound := bindType(kind, structs)
419390

420391
// todo(rjl493456442) according solidity documentation, indexed event
421392
// parameters that are not value types i.e. arrays and structs are not
@@ -429,16 +400,10 @@ func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
429400
return bound
430401
}
431402

432-
// bindStructType is a set of type binders that convert Solidity tuple types to some supported
433-
// programming language struct definition.
434-
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
435-
LangGo: bindStructTypeGo,
436-
}
437-
438-
// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
403+
// bindStructType converts a Solidity tuple type to a Go one and records the mapping
439404
// in the given map.
440405
// Notably, this function will resolve and record nested struct recursively.
441-
func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
406+
func bindStructType(kind abi.Type, structs map[string]*tmplStruct) string {
442407
switch kind.T {
443408
case abi.TupleTy:
444409
// We compose a raw struct name and a canonical parameter expression
@@ -459,7 +424,7 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
459424
name := capitalise(kind.TupleRawNames[i])
460425
name = abi.ResolveNameConflict(name, func(s string) bool { return names[s] })
461426
names[name] = true
462-
fields = append(fields, &tmplField{Type: bindStructTypeGo(*elem, structs), Name: name, SolKind: *elem})
427+
fields = append(fields, &tmplField{Type: bindStructType(*elem, structs), Name: name, SolKind: *elem})
463428
}
464429
name := kind.TupleRawName
465430
if name == "" {
@@ -473,20 +438,14 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
473438
}
474439
return name
475440
case abi.ArrayTy:
476-
return fmt.Sprintf("[%d]", kind.Size) + bindStructTypeGo(*kind.Elem, structs)
441+
return fmt.Sprintf("[%d]", kind.Size) + bindStructType(*kind.Elem, structs)
477442
case abi.SliceTy:
478-
return "[]" + bindStructTypeGo(*kind.Elem, structs)
443+
return "[]" + bindStructType(*kind.Elem, structs)
479444
default:
480-
return bindBasicTypeGo(kind)
445+
return bindBasicType(kind)
481446
}
482447
}
483448

484-
// namedType is a set of functions that transform language specific types to
485-
// named versions that may be used inside method names.
486-
var namedType = map[Lang]func(string, abi.Type) string{
487-
LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") },
488-
}
489-
490449
// alias returns an alias of the given string based on the aliasing rules
491450
// or returns itself if no rule is matched.
492451
func alias(aliases map[string]string, n string) string {
@@ -497,10 +456,8 @@ func alias(aliases map[string]string, n string) string {
497456
}
498457

499458
// methodNormalizer is a name transformer that modifies Solidity method names to
500-
// conform to target language naming conventions.
501-
var methodNormalizer = map[Lang]func(string) string{
502-
LangGo: abi.ToCamelCase,
503-
}
459+
// conform to Go naming conventions.
460+
var methodNormalizer = abi.ToCamelCase
504461

505462
// capitalise makes a camel-case string which starts with an upper case character.
506463
var capitalise = abi.ToCamelCase

accounts/abi/bind/template.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,9 @@ type tmplStruct struct {
7272
Fields []*tmplField // Struct fields definition depends on the binding language.
7373
}
7474

75-
// tmplSource is language to template mapping containing all the supported
76-
// programming languages the package can generate to.
77-
var tmplSource = map[Lang]string{
78-
LangGo: tmplSourceGo,
79-
}
80-
81-
var tmplSourceV2 = map[Lang]string{
82-
LangGo: tmplSourceGoV2,
83-
}
84-
85-
// tmplSourceGo is the Go source template that the generated Go contract binding
75+
// tmplSource is the Go source template that the generated Go contract binding
8676
// is based on.
87-
const tmplSourceGo = `
77+
const tmplSource = `
8878
// Code generated - DO NOT EDIT.
8979
// This file is a generated binding and any manual changes will be lost.
9080

accounts/abi/bind/template2.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/abigen/main.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ var (
6363
Name: "out",
6464
Usage: "Output file for the generated binding (default = stdout)",
6565
}
66-
langFlag = &cli.StringFlag{
67-
Name: "lang",
68-
Usage: "Destination language for the bindings (go)",
69-
Value: "go",
70-
}
7166
aliasFlag = &cli.StringFlag{
7267
Name: "alias",
7368
Usage: "Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2",
@@ -90,7 +85,6 @@ func init() {
9085
excFlag,
9186
pkgFlag,
9287
outFlag,
93-
langFlag,
9488
aliasFlag,
9589
v2Flag,
9690
}
@@ -103,13 +97,6 @@ func abigen(c *cli.Context) error {
10397
if c.String(pkgFlag.Name) == "" {
10498
utils.Fatalf("No destination package specified (--pkg)")
10599
}
106-
var lang bind.Lang
107-
switch c.String(langFlag.Name) {
108-
case "go":
109-
lang = bind.LangGo
110-
default:
111-
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.String(langFlag.Name))
112-
}
113100
// If the entire solidity code was specified, build and bind based on that
114101
var (
115102
abis []string
@@ -226,9 +213,9 @@ func abigen(c *cli.Context) error {
226213
err error
227214
)
228215
if c.IsSet(v2Flag.Name) {
229-
code, err = bind.BindV2(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases)
216+
code, err = bind.BindV2(types, abis, bins, sigs, c.String(pkgFlag.Name), libs, aliases)
230217
} else {
231-
code, err = bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases)
218+
code, err = bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), libs, aliases)
232219
}
233220
if err != nil {
234221
utils.Fatalf("Failed to generate ABI binding: %v", err)

0 commit comments

Comments
 (0)