-
Notifications
You must be signed in to change notification settings - Fork 20.5k
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
accounts/abi: allow abi: tags when unpacking structs #16648
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package abi | |
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
// indirect recursively dereferences the value until it either gets the value | ||
|
@@ -111,18 +112,101 @@ func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind, | |
return nil | ||
} | ||
|
||
// requireUniqueStructFieldNames makes sure field names don't collide | ||
func requireUniqueStructFieldNames(args Arguments) error { | ||
exists := make(map[string]bool) | ||
// mapAbiToStringField maps abi to struct fields. | ||
// first round: for each Exportable field that contains a `abi:""` tag | ||
// and this field name exists in the arguments, pair them together. | ||
// second round: for each argument field that has not been already linked, | ||
// find what variable is expected to be mapped into, if it exists and has not been | ||
// used, pair them. | ||
func mapAbiToStructFields(args Arguments, value reflect.Value) (map[string]string, error) { | ||
|
||
typ := value.Type() | ||
|
||
abi2struct := make(map[string]string) | ||
struct2abi := make(map[string]string) | ||
|
||
// first round ~~~ | ||
for i := 0; i < typ.NumField(); i++ { | ||
structFieldName := typ.Field(i).Name | ||
|
||
// skip private struct fields. | ||
if structFieldName[:1] != strings.ToUpper(structFieldName[:1]) { | ||
continue | ||
} | ||
|
||
// skip fields that have no abi:"" tag. | ||
var ok bool | ||
var tagName string | ||
if tagName, ok = typ.Field(i).Tag.Lookup("abi"); !ok { | ||
continue | ||
} | ||
|
||
// check if tag is empty. | ||
if tagName == "" { | ||
return nil, fmt.Errorf("struct: abi tag in '%s' is empty", structFieldName) | ||
} | ||
|
||
// check which argument field matches with the abi tag. | ||
found := false | ||
for _, abiField := range args.NonIndexed() { | ||
if abiField.Name == tagName { | ||
if abi2struct[abiField.Name] != "" { | ||
return nil, fmt.Errorf("struct: abi tag in '%s' already mapped", structFieldName) | ||
} | ||
// pair them | ||
abi2struct[abiField.Name] = structFieldName | ||
struct2abi[structFieldName] = abiField.Name | ||
found = true | ||
} | ||
} | ||
|
||
// check if this tag has been mapped. | ||
if !found { | ||
return nil, fmt.Errorf("struct: abi tag '%s' defined but not found in abi", tagName) | ||
} | ||
|
||
} | ||
|
||
// second round ~~~ | ||
for _, arg := range args { | ||
field := capitalise(arg.Name) | ||
if field == "" { | ||
return fmt.Errorf("abi: purely underscored output cannot unpack to struct") | ||
|
||
abiFieldName := arg.Name | ||
structFieldName := capitalise(abiFieldName) | ||
|
||
if structFieldName == "" { | ||
return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct") | ||
} | ||
if exists[field] { | ||
return fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", field) | ||
|
||
// this abi has already been paired, skip... unless exists another not still assigned | ||
// struct field with the same field name, in the case raise an error: | ||
// abi: [ { "name": "value" } ] | ||
// struct { Value *big.Int , Value1 *big.Int `abi:"value"`} | ||
if abi2struct[abiFieldName] != "" { | ||
if abi2struct[abiFieldName] != structFieldName && | ||
struct2abi[structFieldName] == "" && | ||
value.FieldByName(structFieldName).IsValid() { | ||
return nil, fmt.Errorf("abi: multiple variables maps to the same abi field '%s'", abiFieldName) | ||
} | ||
continue | ||
} | ||
exists[field] = true | ||
|
||
// return an error if this struct field has already been paired. | ||
if struct2abi[structFieldName] != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you move this before line 181? It would simplify the condition check on line 184 |
||
return nil, fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", structFieldName) | ||
} | ||
|
||
if value.FieldByName(structFieldName).IsValid() { | ||
// pair them | ||
abi2struct[abiFieldName] = structFieldName | ||
struct2abi[structFieldName] = abiFieldName | ||
} else { | ||
// not paired, but annotate as used, to detect cases like | ||
// abi : [ { "name": "value" }, { "name": "_value" } ] | ||
// struct { Value *big.Int } | ||
struct2abi[structFieldName] = abiFieldName | ||
} | ||
|
||
} | ||
return nil | ||
|
||
return abi2struct, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apparently I missed this one:
unless there exists another, yet unassigned...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, thanks for the grammar checks @gballet , I'll change it! [ and I annotate to use a grammar checker next time for the comments :) ]
I have no strong opinion about the default value. No problem about implementing it. In which use case are you thinking about? A generic event / function argument parser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about default values for API calls, but it doesn't really make sense actually because that would be a Go-only behavior. Just fix the last grammar thing. @holiman and I will discuss the default value topic, but I don't think it's necessary after all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed so @AdriaMB please fix the last grammar issue and we'll merge your PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm, I just did