-
Notifications
You must be signed in to change notification settings - Fork 20.3k
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
cmd/abigen: change --exc to exclude by type name #22620
Merged
Merged
Changes from all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type nameFilter struct { | ||
fulls map[string]bool // path/to/contract.sol:Type | ||
files map[string]bool // path/to/contract.sol:* | ||
types map[string]bool // *:Type | ||
} | ||
|
||
func newNameFilter(patterns ...string) (*nameFilter, error) { | ||
f := &nameFilter{ | ||
fulls: make(map[string]bool), | ||
files: make(map[string]bool), | ||
types: make(map[string]bool), | ||
} | ||
for _, pattern := range patterns { | ||
if err := f.add(pattern); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return f, nil | ||
} | ||
|
||
func (f *nameFilter) add(pattern string) error { | ||
ft := strings.Split(pattern, ":") | ||
if len(ft) != 2 { | ||
// filenames and types must not include ':' symbol | ||
return fmt.Errorf("invalid pattern: %s", pattern) | ||
} | ||
|
||
file, typ := ft[0], ft[1] | ||
if file == "*" { | ||
f.types[typ] = true | ||
return nil | ||
} else if typ == "*" { | ||
f.files[file] = true | ||
return nil | ||
} | ||
f.fulls[pattern] = true | ||
return nil | ||
} | ||
|
||
func (f *nameFilter) Matches(name string) bool { | ||
ft := strings.Split(name, ":") | ||
if len(ft) != 2 { | ||
// If contract names are always of the fully-qualified form | ||
// <filePath>:<type>, then this case will never happen. | ||
return false | ||
} | ||
|
||
file, typ := ft[0], ft[1] | ||
// full paths > file paths > types | ||
return f.fulls[name] || f.files[file] || f.types[typ] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNameFilter(t *testing.T) { | ||
_, err := newNameFilter("Foo") | ||
require.Error(t, err) | ||
_, err = newNameFilter("too/many:colons:Foo") | ||
require.Error(t, err) | ||
|
||
f, err := newNameFilter("a/path:A", "*:B", "c/path:*") | ||
require.NoError(t, err) | ||
|
||
for _, tt := range []struct { | ||
name string | ||
match bool | ||
}{ | ||
{"a/path:A", true}, | ||
{"unknown/path:A", false}, | ||
{"a/path:X", false}, | ||
{"unknown/path:X", false}, | ||
{"any/path:B", true}, | ||
{"c/path:X", true}, | ||
{"c/path:foo:B", false}, | ||
} { | ||
match := f.Matches(tt.name) | ||
if tt.match { | ||
assert.True(t, match, "expected match") | ||
} else { | ||
assert.False(t, match, "expected no match") | ||
} | ||
} | ||
} |
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.
And I just looked at the diff again. Was it even possible before to have two contracts with the same name? The
types
slice would have duplicate strings, is this supported bybind.Bind
(called later with this slice)?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.
Would also be interested on your feedback here @karalabe . It was argued before that the full path allows ignoring specific contracts if the same contract name is reused in the same project. This logic here, also present in the old version, makes me wonder if it was even at all supported before to reuse the same contract name.