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

fix(client/v2): fix comment parsing #19377

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions client/v2/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func DescriptorKebabName(descriptor protoreflect.Descriptor) string {
}

// DescriptorDocs returns the leading comments of the descriptor.
// TODO this does not work, to fix.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to fix this, which never worked it seems, so that we can use latest api module into release/v0.50 (#19376)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that SourceCodeInfo is never included in the descriptor: golang/protobuf#1134 and neither in our pulsar generator.
We should add them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func DescriptorDocs(descriptor protoreflect.Descriptor) string {
return descriptor.ParentFile().SourceLocations().ByDescriptor(descriptor).LeadingComments
}
Expand Down Expand Up @@ -52,6 +51,10 @@ func isSupportedVersion(input string, buildInfo *debug.BuildInfo) bool {
}

moduleName, version := parseSinceComment(input)
if moduleName == "" || version == "" {
return true // if no comment consider it's supported
}

for _, dep := range buildInfo.Deps {
if !strings.Contains(dep.Path, moduleName) {
continue
Expand All @@ -60,7 +63,11 @@ func isSupportedVersion(input string, buildInfo *debug.BuildInfo) bool {
return version <= dep.Version
}

return true // if cannot find the module consider it's supported
// if cannot find the module consider it isn't supported
// for instance the x/gov module wasn't extracted in v0.50
// so it isn't present in the build info, however, that means
// it isn't supported in v0.50.
return false
}

var sinceCommentRegex = regexp.MustCompile(`\/\/\s*since: (\S+) (\S+)`)
Expand All @@ -77,7 +84,7 @@ func parseSinceComment(input string) (string, string) {

matches := sinceCommentRegex.FindStringSubmatch(input)
if len(matches) >= 3 {
moduleName, version = matches[1], matches[2]
moduleName, version = strings.TrimPrefix(matches[1], "x/"), matches[2]

if !strings.HasPrefix(version, "v") {
version = "v" + version
Expand Down
35 changes: 17 additions & 18 deletions client/v2/internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,12 @@ func TestIsSupportedVersion(t *testing.T) {
}

for _, tc := range cases {
resp := isSupportedVersion(tc.input, mockBuildInfo)
if resp != tc.expected {
t.Errorf("expected %v, got %v", tc.expected, resp)
}

resp = isSupportedVersion(tc.input, &debug.BuildInfo{})
if !resp {
t.Errorf("expected %v, got %v", true, resp)
}
t.Run(tc.input, func(t *testing.T) {
resp := isSupportedVersion(tc.input, mockBuildInfo)
if resp != tc.expected {
t.Errorf("expected %v, got %v", tc.expected, resp)
}
})
}
}

Expand Down Expand Up @@ -139,23 +136,25 @@ func TestParseSinceComment(t *testing.T) {
},
{
input: "// Since: x/feegrant v0.1.0",
expectedModuleName: "x/feegrant",
expectedModuleName: "feegrant",
expectedVersion: "v0.1.0",
},
{
input: "// since: x/feegrant 0.1",
expectedModuleName: "x/feegrant",
expectedModuleName: "feegrant",
expectedVersion: "v0.1",
},
}

for _, tc := range cases {
moduleName, version := parseSinceComment(tc.input)
if moduleName != tc.expectedModuleName {
t.Errorf("expected module name %s, got %s", tc.expectedModuleName, moduleName)
}
if version != tc.expectedVersion {
t.Errorf("expected version %s, got %s", tc.expectedVersion, version)
}
t.Run(tc.input, func(t *testing.T) {
moduleName, version := parseSinceComment(tc.input)
if moduleName != tc.expectedModuleName {
t.Errorf("expected module name %s, got %s", tc.expectedModuleName, moduleName)
}
if version != tc.expectedVersion {
t.Errorf("expected version %s, got %s", tc.expectedVersion, version)
}
})
}
}
Loading