@@ -17,6 +17,7 @@ package gengapic
17
17
import (
18
18
"fmt"
19
19
"path/filepath"
20
+ "strings"
20
21
21
22
"github.com/golang/protobuf/protoc-gen-go/descriptor"
22
23
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
@@ -94,8 +95,7 @@ func (g *generator) genAndCommitSnippets(s *descriptor.ServiceDescriptorProto) e
94
95
f := g .descInfo .ParentFile [m ]
95
96
// Get the original proto service for the method (different from `s` only for mixins).
96
97
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" )
99
99
g .snippetMetadata .AddMethod (s .GetName (), m .GetName (), f .GetPackage (), methodServ .GetName (), lineCount - 1 )
100
100
}
101
101
return nil
@@ -130,11 +130,20 @@ func (g *generator) genAndCommitSnippetMetadata(protoPkg string) error {
130
130
if err != nil {
131
131
return err
132
132
}
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 ))
135
134
g .resp .File = append (g .resp .File , & plugin.CodeGeneratorResponse_File {
136
135
Name : proto .String (file ),
137
136
Content : proto .String (string (json [:])),
138
137
})
139
138
return nil
140
139
}
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
+ }
0 commit comments