Skip to content

Fix generated file name error in some cases #351

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

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion protoc-gen-twirp/go_naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (t *twirp) goFileName(f *descriptor.FileDescriptorProto) string {
// otherwise, the directory is taken from the option go_package
if impPath, _, ok := goPackageOption(f); ok && impPath != "" {
if t.modulePrefix != "" {
impPath = strings.TrimPrefix(impPath, t.modulePrefix)
impPath = strings.TrimPrefix(strings.TrimPrefix(impPath, t.modulePrefix), "/")
}

// Replace the existing dirname with the import path from go_package
Expand Down
37 changes: 37 additions & 0 deletions protoc-gen-twirp/go_naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,40 @@ func TestGoPackageOption(t *testing.T) {
testcase("github.com/example/foo;bar", "github.com/example/foo", "bar", true))
t.Run("non dotted import with package", testcase("foo;bar", "foo", "bar", true))
}

func TestGoFileName(t *testing.T) {
testcase := func(srcrelpaths bool, modprefix, fname, gopkg, wantName string) func(t2 *testing.T) {
return func(t *testing.T) {
f := &descriptor.FileDescriptorProto{
Name: &fname,
Options: &descriptor.FileOptions{
GoPackage: &gopkg,
},
}

tw := &twirp{
sourceRelativePaths: srcrelpaths,
modulePrefix: modprefix,
}

if name := tw.goFileName(f); name != wantName {
t.Errorf("wrong goFileName, have=%q want=%q", name, wantName)
}
}
}

t.Run("paths=source_relative",
testcase(true, "",
"rpc/v1/service.proto", "example.com/module/package/rpc/v1",
"rpc/v1/service.twirp.go"))

t.Run("paths=import,module=example.com/module/package",
testcase(false, "example.com/module/package",
"rpc/v1/service.proto", "example.com/module/package/rpc/v1",
"rpc/v1/service.twirp.go"))

t.Run("paths=import,module=example.com/module/package/",
testcase(false, "example.com/module/package/",
"rpc/v1/service.proto", "example.com/module/package/rpc/v1",
"rpc/v1/service.twirp.go"))
}