diff --git a/internal/gengapic/example_test.go b/internal/gengapic/example_test.go index 60eff343f..05a48c8ed 100644 --- a/internal/gengapic/example_test.go +++ b/internal/gengapic/example_test.go @@ -350,7 +350,7 @@ func TestGenSnippetFile(t *testing.T) { if err != nil { t.Fatal(err) } - g.commit(filepath.Join(g.opts.outDir, "temp", "snippets", "main.go"), "main") + g.commit(filepath.Join("cloud.google.com/go", "internal", "generated", "snippets", "bigquery", "main.go"), "main") if diff := cmp.Diff(g.imports, tst.imports); diff != "" { t.Errorf("TestExample(%s): imports got(-),want(+):\n%s", tst.tstName, diff) } diff --git a/internal/gengapic/snippets.go b/internal/gengapic/snippets.go index 280481fc4..7415020d4 100644 --- a/internal/gengapic/snippets.go +++ b/internal/gengapic/snippets.go @@ -17,6 +17,7 @@ package gengapic import ( "fmt" "path/filepath" + "strings" "github.com/golang/protobuf/protoc-gen-go/descriptor" plugin "github.com/golang/protobuf/protoc-gen-go/plugin" @@ -94,8 +95,7 @@ func (g *generator) genAndCommitSnippets(s *descriptor.ServiceDescriptorProto) e f := g.descInfo.ParentFile[m] // Get the original proto service for the method (different from `s` only for mixins). methodServ := (g.descInfo.ParentElement[m]).(*descriptor.ServiceDescriptorProto) - lineCount := g.commit(filepath.Join(g.opts.outDir, "internal", - "snippets", clientName, m.GetName(), "main.go"), "main") + lineCount := g.commit(filepath.Join(g.snippetsOutDir(), clientName, m.GetName(), "main.go"), "main") g.snippetMetadata.AddMethod(s.GetName(), m.GetName(), f.GetPackage(), methodServ.GetName(), lineCount-1) } return nil @@ -130,11 +130,20 @@ func (g *generator) genAndCommitSnippetMetadata(protoPkg string) error { if err != nil { return err } - file := filepath.Join(g.opts.outDir, "internal", "snippets", - fmt.Sprintf("snippet_metadata.%s.json", protoPkg)) + file := filepath.Join(g.snippetsOutDir(), fmt.Sprintf("snippet_metadata.%s.json", protoPkg)) g.resp.File = append(g.resp.File, &plugin.CodeGeneratorResponse_File{ Name: proto.String(file), Content: proto.String(string(json[:])), }) return nil } + +func (g *generator) snippetsOutDir() string { + if strings.Contains(g.opts.pkgPath, "cloud.google.com/go/") { + // Write snippet metadata at the top level of the google-cloud-go namespace, not at the client package. + // This matches the destination directory structure in google-cloud-go. + pkg := strings.TrimPrefix(g.opts.pkgPath, "cloud.google.com/go/") + return filepath.Join("cloud.google.com/go", "internal", "generated", "snippets", filepath.FromSlash(pkg)) + } + return filepath.Join(g.opts.outDir, "internal", "snippets") +} diff --git a/internal/gengapic/snippets_test.go b/internal/gengapic/snippets_test.go index c1975cca8..d39009da7 100644 --- a/internal/gengapic/snippets_test.go +++ b/internal/gengapic/snippets_test.go @@ -27,6 +27,34 @@ import ( "google.golang.org/protobuf/proto" ) +func TestSnippetsOutDir(t *testing.T) { + for _, tst := range []struct { + opts options + want string + }{ + { + opts: options{ + outDir: "cloud.google.com/go/video/stitcher/apiv1", + pkgPath: "cloud.google.com/go/video/stitcher/apiv1", + }, + want: "cloud.google.com/go/internal/generated/snippets/video/stitcher/apiv1", + }, + { + opts: options{ + outDir: "example.com/my/package", + pkgPath: "example.com/my/package", + }, + want: "example.com/my/package/internal/snippets", + }, + } { + var g generator + g.opts = &tst.opts + if s := g.snippetsOutDir(); s != tst.want { + t.Errorf("TestGenAndCommitSnippets(g.opts.pkgPath = %s): got %s, want %s", g.opts.pkgPath, s, tst.want) + } + } +} + func TestGenAndCommitSnippets(t *testing.T) { inputType := &descriptor.DescriptorProto{ Name: proto.String("InputType"),