-
Notifications
You must be signed in to change notification settings - Fork 607
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
Remove potential Go module versions from shortened names #571
Changes from 3 commits
88c96e4
011171f
f25590d
4b5efb1
5ca898f
b05cf5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,8 @@ var ( | |
// Removes package name and method arugments for Go function names. | ||
// See tests for examples. | ||
goRegExp = regexp.MustCompile(`^(?:[\w\-\.]+\/)+(.+)`) | ||
// Checks for a package name that could be a module version. | ||
goVerRegExp = regexp.MustCompile(`^v([2-9]|[1-9][0-9]+)\.`) | ||
// Strips C++ namespace prefix from a C++ function / method name. | ||
// NOTE: Make sure to keep the template parameters in the name. Normally, | ||
// template parameters are stripped from the C++ names but when | ||
|
@@ -442,12 +444,32 @@ func ShortenFunctionName(f string) string { | |
f = cppAnonymousPrefixRegExp.ReplaceAllString(f, "") | ||
for _, re := range []*regexp.Regexp{goRegExp, javaRegExp, cppRegExp} { | ||
if matches := re.FindStringSubmatch(f); len(matches) >= 2 { | ||
return strings.Join(matches[1:], "") | ||
name := strings.Join(matches[1:], "") | ||
if re == goRegExp { | ||
return shortenGoFunc(f, name) | ||
} | ||
return name | ||
} | ||
} | ||
return f | ||
} | ||
|
||
func shortenGoFunc(f string, name string) string { | ||
if !goVerRegExp.MatchString(name) { | ||
return name | ||
} | ||
|
||
// The shortened name could start with a module version (like "v2"). Go back one slash. | ||
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. Keep comments in 80 columns please. 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. Sure. |
||
end := len(f) - len(name) - 1 | ||
if end >= 0 { | ||
prefix := f[:end] | ||
if idx := strings.LastIndex(prefix, "/"); idx >= 0 { | ||
end = idx | ||
} | ||
} | ||
return f[end+1:] | ||
} | ||
|
||
// TrimTree trims a Graph in forest form, keeping only the nodes in kept. This | ||
// will not work correctly if even a single node has multiple parents. | ||
func (g *Graph) TrimTree(kept NodePtrSet) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -451,6 +451,30 @@ func TestShortenFunctionName(t *testing.T) { | |
"github.com/blah/blah/vendor/gopkg.in/redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm", | ||
"redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm", | ||
}, | ||
{ | ||
"github.com/foo/bar/v4.(*Foo).Bar", | ||
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. Curious - why is version string sometimes a separate subdirectory and sometimes a prefix of the package name? Is this something that the package owners choose? Are these options restricted at these two, or are there more? Oh, I guess it's a function of how deep below the versioning level the actual symbol is? 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. I think you're referring to the tests; some of the tests I've added are where the "version" isn't a version at all. The only valid version in paths are "v2", "v3", ... "v1234", etc. So you'd have But if it isn't a valid version part, then I don't want to treat it as one naively (i.e. "something.com/hello/v123xyz" isn't versioned, "something.com/hello/v123/xyz" is because the version is its own element). |
||
"bar/v4.(*Foo).Bar", | ||
}, | ||
{ | ||
"github.com/foo/bar/v4/baz.Foo.Bar", | ||
"baz.Foo.Bar", | ||
}, | ||
{ | ||
"github.com/foo/bar/v123.(*Foo).Bar", | ||
"bar/v123.(*Foo).Bar", | ||
}, | ||
{ | ||
"github.com/foobar/v0.(*Foo).Bar", | ||
"v0.(*Foo).Bar", | ||
}, | ||
{ | ||
"github.com/foobar/v1.(*Foo).Bar", | ||
"v1.(*Foo).Bar", | ||
}, | ||
{ | ||
"example.org/v2xyz.Foo", | ||
"v2xyz.Foo", | ||
}, | ||
{ | ||
"java.util.concurrent.ThreadPoolExecutor$Worker.run", | ||
"ThreadPoolExecutor$Worker.run", | ||
|
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 feel it might be simpler to first remove the version substring from the name, and then handle it just like before.
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.
Not sure if you saw the previous review comments, but if preferred this all can be removed and replaced with a single regex change.
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'm not sure how this is possible; the name here is extracted from the regex directly. If we remove the version suffix, you get the empty string.
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.
Oh, I see, you mean that if the name matches a version, remove the suffix from the whole path and then try again. It wouldn't distinguish two versions of the same module, but I guess it's no worse than any other name aliasing within the same graph. Would be short; I can do that if preferred.
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 think I misinterpreted again (sorry!), so I'll wait for clarification.
I think you meant just starting with something like
github.com/jackc/pgx/v4/foo.bar
, then replacing the first instance of/v4/
with/
, then running the regex again. Not quite a suffix, but functional enough. This is all heuristics 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.
Sorry,
/v[1-9][0-9]*[./]
.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 not all occurrences but at most one occurrence (assuming there can't be two version substrings in the name).
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.
All instances wouldn't work, because it's legal for me to write
github.com/foo/bar/v4/something/v8
or similar. First instance I believe would work as intended, though.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'll give it a try. Would be
v([2-9]|[1-9][0-9]+)\.
, though, asv0
andv1
don't exist.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.
Done, and retitled to match the new fix.
regexp
doesn't have a nice "just replace once", so I used the replace all with two capture groups method.