@@ -16,6 +16,7 @@ package gengapic
16
16
17
17
import (
18
18
"fmt"
19
+ "regexp"
19
20
20
21
"github.com/golang/protobuf/protoc-gen-go/descriptor"
21
22
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
@@ -28,6 +29,17 @@ import (
28
29
)
29
30
30
31
func init () {
32
+ initMixinFiles ()
33
+ }
34
+
35
+ var apiVersionRegexp = regexp .MustCompile (`v\d+[a-z]*\d*[a-z]*\d*` )
36
+
37
+ var mixinFiles map [string ][]* descriptor.FileDescriptorProto
38
+
39
+ type mixins map [string ][]* descriptor.MethodDescriptorProto
40
+
41
+ // initMixinFiles allows test code to re-initialize the mixinFiles global.
42
+ func initMixinFiles () {
31
43
mixinFiles = map [string ][]* descriptor.FileDescriptorProto {
32
44
"google.cloud.location.Locations" : {
33
45
protodesc .ToFileDescriptorProto (location .File_google_cloud_location_locations_proto ),
@@ -43,10 +55,6 @@ func init() {
43
55
}
44
56
}
45
57
46
- var mixinFiles map [string ][]* descriptor.FileDescriptorProto
47
-
48
- type mixins map [string ][]* descriptor.MethodDescriptorProto
49
-
50
58
// collectMixins collects the configured mixin APIs from the Service config and
51
59
// gathers the appropriately configured mixin methods to generate for each.
52
60
func (g * generator ) collectMixins () {
@@ -246,9 +254,24 @@ func (g *generator) lookupHTTPOverride(fqn string, f func(h *annotations.HttpRul
246
254
return ""
247
255
}
248
256
249
- func (g * generator ) getOperationPathOverride () string {
257
+ // getOperationPathOverride looks up the google.api.http rule for LRO GetOperation
258
+ // and returns the path override. If no value is present, it synthesizes a path
259
+ // using the proto package client version, for example, "/v1/{name=operations/**}".
260
+ func (g * generator ) getOperationPathOverride (protoPkg string ) string {
250
261
get := func (h * annotations.HttpRule ) string { return h .GetGet () }
251
262
override := g .lookupHTTPOverride ("google.longrunning.Operations.GetOperation" , get )
263
+ if override == "" {
264
+ // extract httpInfo from "hot loaded" Operations.GetOperation MethodDescriptor
265
+ // Should be "/v1/{name=operations/**}"
266
+ file := mixinFiles ["google.longrunning.Operations" ][0 ]
267
+ mdp := getMethod (file .GetService ()[0 ], "GetOperation" )
268
+ getOperationPath := getHTTPInfo (mdp ).url
269
+
270
+ // extract client version from proto package with global regex
271
+ // replace version base path in GetOperation path with proto package version segment
272
+ version := apiVersionRegexp .FindStringSubmatch (protoPkg )
273
+ override = apiVersionRegexp .ReplaceAllStringFunc (getOperationPath , func (s string ) string { return version [0 ] })
274
+ }
252
275
override = httpPatternVarRegex .ReplaceAllStringFunc (override , func (s string ) string { return "%s" })
253
276
return override
254
277
}
0 commit comments