Skip to content

Commit

Permalink
fix(internal/gengapic): write snippet output to cloud.google.com/go (#…
Browse files Browse the repository at this point in the history
…1313)

* Write snippets at the top level of the google-cloud-go namespace.
  • Loading branch information
quartzmo committed Apr 21, 2023
1 parent dd536ac commit dfc5ce2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/gengapic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 13 additions & 4 deletions internal/gengapic/snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
28 changes: 28 additions & 0 deletions internal/gengapic/snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit dfc5ce2

Please sign in to comment.