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(internal/gengapic): write snippet output to cloud.google.com/go #1313

Merged
merged 3 commits into from
Apr 21, 2023
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 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")
}
Comment on lines +141 to +149
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great idea, Chris.

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