forked from googleapis/gapic-generator-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlro.go
259 lines (226 loc) · 8.56 KB
/
lro.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gengapic
import (
"fmt"
"strings"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"google.golang.org/genproto/googleapis/longrunning"
)
func (g *generator) lroCall(servName string, m *descriptor.MethodDescriptorProto) error {
s := g.descInfo.ParentElement[m]
sFQN := fmt.Sprintf("%s.%s", g.descInfo.ParentFile[s].GetPackage(), s.GetName())
inType := g.descInfo.Type[m.GetInputType()]
outType := g.descInfo.Type[m.GetOutputType()]
inSpec, err := g.descInfo.ImportSpec(inType)
if err != nil {
return err
}
outSpec, err := g.descInfo.ImportSpec(outType)
if err != nil {
return err
}
lroType := lroTypeName(m.GetName())
p := g.printf
p("func (c *%sClient) %s(ctx context.Context, req *%s.%s, opts ...gax.CallOption) (*%s, error) {",
servName, m.GetName(), inSpec.Name, inType.GetName(), lroType)
g.deadline(sFQN, m.GetName())
err = g.insertMetadata(m)
if err != nil {
return err
}
g.appendCallOpts(m)
p(" var resp *%s.%s", outSpec.Name, outType.GetName())
p(" err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {")
p(" var err error")
p(" resp, err = %s", grpcClientCall(servName, m.GetName()))
p(" return err")
p(" }, opts...)")
p(" if err != nil {")
p(" return nil, err")
p(" }")
p(" return &%s{", lroType)
p(" lro: longrunning.InternalNewOperation(c.LROClient, resp),")
p(" }, nil")
p("}")
p("")
g.imports[pbinfo.ImportSpec{Path: "cloud.google.com/go/longrunning"}] = true
g.imports[inSpec] = true
g.imports[outSpec] = true
return nil
}
func (g *generator) lroType(servName string, serv *descriptor.ServiceDescriptorProto, m *descriptor.MethodDescriptorProto) error {
mFQN := fmt.Sprintf("%s.%s.%s", g.descInfo.ParentFile[serv].GetPackage(), serv.GetName(), m.GetName())
lroType := lroTypeName(m.GetName())
p := g.printf
eLRO, err := proto.GetExtension(m.Options, longrunning.E_OperationInfo)
if err != nil {
return fmt.Errorf("rpc %q returns google.longrunning.Operation but is missing option google.longrunning.operation_info", mFQN)
}
opInfo := eLRO.(*longrunning.OperationInfo)
fullName := opInfo.GetResponseType()
if fullName == "" {
return fmt.Errorf("rpc %q has google.longrunning.operation_info but is missing option google.longrunning.operation_info.response_type", mFQN)
}
var respType string
{
// eLRO.ResponseType is either fully-qualified or top-level in the same package as the method.
//
// TODO(ndietz) this won't work with nested message types in the same package;
// migrating to protoreflect will help remove from semantic meaning in the names.
if strings.IndexByte(fullName, '.') < 0 {
fullName = g.descInfo.ParentFile[serv].GetPackage() + "." + fullName
}
// When we build a map[name]Type in pbinfo, we prefix names with '.' to signify that they are fully qualified.
// The string in ResponseType does not have the prefix, so we add it.
fullName = "." + fullName
typ := g.descInfo.Type[fullName]
name, respSpec, err := g.descInfo.NameSpec(typ)
if err != nil {
return fmt.Errorf("unable to resolve google.longrunning.operation_info.response_type value %q in rpc %q", opInfo.GetResponseType(), mFQN)
}
if fullName != emptyType {
g.imports[respSpec] = true
respType = fmt.Sprintf("%s.%s", respSpec.Name, name)
}
}
hasMeta := opInfo.GetMetadataType() != ""
var metaType string
if hasMeta {
fullName := opInfo.GetMetadataType()
// TODO(ndietz) this won't work with nested message types in the same package;
// migrating to protoreflect will help remove from semantic meaning in the names.
if strings.IndexByte(fullName, '.') < 0 {
fullName = g.descInfo.ParentFile[serv].GetPackage() + "." + fullName
}
fullName = "." + fullName
typ := g.descInfo.Type[fullName]
name, meta, err := g.descInfo.NameSpec(typ)
if err != nil {
return fmt.Errorf("unable to resolve google.longrunning.operation_info.metadata_type value %q in rpc %q", opInfo.GetMetadataType(), mFQN)
}
g.imports[meta] = true
metaType = fmt.Sprintf("%s.%s", meta.Name, name)
}
// Type definition
{
p("// %s manages a long-running operation from %s.", lroType, m.GetName())
p("type %s struct {", lroType)
p(" lro *longrunning.Operation")
p("}")
p("")
}
// LRO from name
{
p("// %[1]s returns a new %[1]s from a given name.", lroType)
p("// The name must be that of a previously created %s, possibly from a different process.", lroType)
p("func (c *%sClient) %[2]s(name string) *%[2]s {", servName, lroType)
p(" return &%s{", lroType)
p(" lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),")
p(" }")
p("}")
p("")
g.imports[pbinfo.ImportSpec{Name: "longrunningpb", Path: "google.golang.org/genproto/googleapis/longrunning"}] = true
}
// Wait
{
p("// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.")
p("//")
p("// See documentation of Poll for error-handling information.")
if opInfo.GetResponseType() == emptyValue {
p("func (op *%s) Wait(ctx context.Context, opts ...gax.CallOption) error {", lroType)
p(" return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...)")
} else {
p("func (op *%s) Wait(ctx context.Context, opts ...gax.CallOption) (*%s, error) {", lroType, respType)
p(" var resp %s", respType)
p(" if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {")
p(" return nil, err")
p(" }")
p(" return &resp, nil")
}
p("}")
p("")
g.imports[pbinfo.ImportSpec{Path: "time"}] = true
}
// Poll
{
p("// Poll fetches the latest state of the long-running operation.")
p("//")
if hasMeta {
p("// Poll also fetches the latest metadata, which can be retrieved by Metadata.")
p("//")
}
p("// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and")
p("// the operation has completed with failure, the error is returned and op.Done will return true.")
p("// If Poll succeeds and the operation has completed successfully,")
p("// op.Done will return true, and the response of the operation is returned.")
p("// If Poll succeeds and the operation has not completed, the returned response and error are both nil.")
if opInfo.GetResponseType() == emptyValue {
p("func (op *%s) Poll(ctx context.Context, opts ...gax.CallOption) error {", lroType)
p(" return op.lro.Poll(ctx, nil, opts...)")
} else {
p("func (op *%s) Poll(ctx context.Context, opts ...gax.CallOption) (*%s, error) {", lroType, respType)
p(" var resp %s", respType)
p(" if err := op.lro.Poll(ctx, &resp, opts...); err != nil {")
p(" return nil, err")
p(" }")
p(" if !op.Done() {")
p(" return nil, nil")
p(" }")
p(" return &resp, nil")
}
p("}")
p("")
}
// Metadata
if hasMeta {
p("// Metadata returns metadata associated with the long-running operation.")
p("// Metadata itself does not contact the server, but Poll does.")
p("// To get the latest metadata, call this method after a successful call to Poll.")
p("// If the metadata is not available, the returned metadata and error are both nil.")
p("func (op *%s) Metadata() (*%s, error) {", lroType, metaType)
p(" var meta %s", metaType)
p(" if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {")
p(" return nil, nil")
p(" } else if err != nil {")
p(" return nil, err")
p(" }")
p(" return &meta, nil")
p("}")
p("")
}
// Done
{
p("// Done reports whether the long-running operation has completed.")
p("func (op *%s) Done() bool {", lroType)
p("return op.lro.Done()")
p("}")
p("")
}
// Name
{
p("// Name returns the name of the long-running operation.")
p("// The name is assigned by the server and is unique within the service from which the operation is created.")
p("func (op *%s) Name() string {", lroType)
p("return op.lro.Name()")
p("}")
p("")
}
return nil
}
func lroTypeName(methodName string) string {
return methodName + "Operation"
}