Skip to content

Commit dfc5ce2

Browse files
authored
fix(internal/gengapic): write snippet output to cloud.google.com/go (#1313)
* Write snippets at the top level of the google-cloud-go namespace.
1 parent dd536ac commit dfc5ce2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

internal/gengapic/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func TestGenSnippetFile(t *testing.T) {
350350
if err != nil {
351351
t.Fatal(err)
352352
}
353-
g.commit(filepath.Join(g.opts.outDir, "temp", "snippets", "main.go"), "main")
353+
g.commit(filepath.Join("cloud.google.com/go", "internal", "generated", "snippets", "bigquery", "main.go"), "main")
354354
if diff := cmp.Diff(g.imports, tst.imports); diff != "" {
355355
t.Errorf("TestExample(%s): imports got(-),want(+):\n%s", tst.tstName, diff)
356356
}

internal/gengapic/snippets.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package gengapic
1717
import (
1818
"fmt"
1919
"path/filepath"
20+
"strings"
2021

2122
"github.com/golang/protobuf/protoc-gen-go/descriptor"
2223
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
@@ -94,8 +95,7 @@ func (g *generator) genAndCommitSnippets(s *descriptor.ServiceDescriptorProto) e
9495
f := g.descInfo.ParentFile[m]
9596
// Get the original proto service for the method (different from `s` only for mixins).
9697
methodServ := (g.descInfo.ParentElement[m]).(*descriptor.ServiceDescriptorProto)
97-
lineCount := g.commit(filepath.Join(g.opts.outDir, "internal",
98-
"snippets", clientName, m.GetName(), "main.go"), "main")
98+
lineCount := g.commit(filepath.Join(g.snippetsOutDir(), clientName, m.GetName(), "main.go"), "main")
9999
g.snippetMetadata.AddMethod(s.GetName(), m.GetName(), f.GetPackage(), methodServ.GetName(), lineCount-1)
100100
}
101101
return nil
@@ -130,11 +130,20 @@ func (g *generator) genAndCommitSnippetMetadata(protoPkg string) error {
130130
if err != nil {
131131
return err
132132
}
133-
file := filepath.Join(g.opts.outDir, "internal", "snippets",
134-
fmt.Sprintf("snippet_metadata.%s.json", protoPkg))
133+
file := filepath.Join(g.snippetsOutDir(), fmt.Sprintf("snippet_metadata.%s.json", protoPkg))
135134
g.resp.File = append(g.resp.File, &plugin.CodeGeneratorResponse_File{
136135
Name: proto.String(file),
137136
Content: proto.String(string(json[:])),
138137
})
139138
return nil
140139
}
140+
141+
func (g *generator) snippetsOutDir() string {
142+
if strings.Contains(g.opts.pkgPath, "cloud.google.com/go/") {
143+
// Write snippet metadata at the top level of the google-cloud-go namespace, not at the client package.
144+
// This matches the destination directory structure in google-cloud-go.
145+
pkg := strings.TrimPrefix(g.opts.pkgPath, "cloud.google.com/go/")
146+
return filepath.Join("cloud.google.com/go", "internal", "generated", "snippets", filepath.FromSlash(pkg))
147+
}
148+
return filepath.Join(g.opts.outDir, "internal", "snippets")
149+
}

internal/gengapic/snippets_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ import (
2727
"google.golang.org/protobuf/proto"
2828
)
2929

30+
func TestSnippetsOutDir(t *testing.T) {
31+
for _, tst := range []struct {
32+
opts options
33+
want string
34+
}{
35+
{
36+
opts: options{
37+
outDir: "cloud.google.com/go/video/stitcher/apiv1",
38+
pkgPath: "cloud.google.com/go/video/stitcher/apiv1",
39+
},
40+
want: "cloud.google.com/go/internal/generated/snippets/video/stitcher/apiv1",
41+
},
42+
{
43+
opts: options{
44+
outDir: "example.com/my/package",
45+
pkgPath: "example.com/my/package",
46+
},
47+
want: "example.com/my/package/internal/snippets",
48+
},
49+
} {
50+
var g generator
51+
g.opts = &tst.opts
52+
if s := g.snippetsOutDir(); s != tst.want {
53+
t.Errorf("TestGenAndCommitSnippets(g.opts.pkgPath = %s): got %s, want %s", g.opts.pkgPath, s, tst.want)
54+
}
55+
}
56+
}
57+
3058
func TestGenAndCommitSnippets(t *testing.T) {
3159
inputType := &descriptor.DescriptorProto{
3260
Name: proto.String("InputType"),

0 commit comments

Comments
 (0)