/
service.go
388 lines (336 loc) · 12.5 KB
/
service.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package builder
import (
"fmt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/internal"
)
// ServiceBuilder is a builder used to construct a desc.ServiceDescriptor.
//
// To create a new ServiceBuilder, use NewService.
type ServiceBuilder struct {
baseBuilder
Options *descriptorpb.ServiceOptions
methods []*MethodBuilder
symbols map[string]*MethodBuilder
}
// NewService creates a new ServiceBuilder for a service with the given name.
func NewService(name string) *ServiceBuilder {
return &ServiceBuilder{
baseBuilder: baseBuilderWithName(name),
symbols: map[string]*MethodBuilder{},
}
}
// FromService returns a ServiceBuilder that is effectively a copy of the given
// descriptor.
//
// Note that it is not just the given service that is copied but its entire
// file. So the caller can get the parent element of the returned builder and
// the result would be a builder that is effectively a copy of the service
// descriptor's parent file.
//
// This means that service builders created from descriptors do not need to be
// explicitly assigned to a file in order to preserve the original service's
// package name.
func FromService(sd *desc.ServiceDescriptor) (*ServiceBuilder, error) {
if fb, err := FromFile(sd.GetFile()); err != nil {
return nil, err
} else if sb, ok := fb.findFullyQualifiedElement(sd.GetFullyQualifiedName()).(*ServiceBuilder); ok {
return sb, nil
} else {
return nil, fmt.Errorf("could not find service %s after converting file %q to builder", sd.GetFullyQualifiedName(), sd.GetFile().GetName())
}
}
func fromService(sd *desc.ServiceDescriptor) (*ServiceBuilder, error) {
sb := NewService(sd.GetName())
sb.Options = sd.GetServiceOptions()
setComments(&sb.comments, sd.GetSourceInfo())
for _, mtd := range sd.GetMethods() {
if mtb, err := fromMethod(mtd); err != nil {
return nil, err
} else if err := sb.TryAddMethod(mtb); err != nil {
return nil, err
}
}
return sb, nil
}
// SetName changes this service's name, returning the service builder for method
// chaining. If the given new name is not valid (e.g. TrySetName would have
// returned an error) then this method will panic.
func (sb *ServiceBuilder) SetName(newName string) *ServiceBuilder {
if err := sb.TrySetName(newName); err != nil {
panic(err)
}
return sb
}
// TrySetName changes this service's name. It will return an error if the given
// new name is not a valid protobuf identifier or if the parent file builder
// already has an element with the given name.
func (sb *ServiceBuilder) TrySetName(newName string) error {
return sb.baseBuilder.setName(sb, newName)
}
// SetComments sets the comments associated with the service. This method
// returns the service builder, for method chaining.
func (sb *ServiceBuilder) SetComments(c Comments) *ServiceBuilder {
sb.comments = c
return sb
}
// GetChildren returns any builders assigned to this service builder. These will
// be the service's methods.
func (sb *ServiceBuilder) GetChildren() []Builder {
var ch []Builder
for _, mtb := range sb.methods {
ch = append(ch, mtb)
}
return ch
}
func (sb *ServiceBuilder) findChild(name string) Builder {
return sb.symbols[name]
}
func (sb *ServiceBuilder) removeChild(b Builder) {
if p, ok := b.GetParent().(*ServiceBuilder); !ok || p != sb {
return
}
sb.methods = deleteBuilder(b.GetName(), sb.methods).([]*MethodBuilder)
delete(sb.symbols, b.GetName())
b.setParent(nil)
}
func (sb *ServiceBuilder) renamedChild(b Builder, oldName string) error {
if p, ok := b.GetParent().(*ServiceBuilder); !ok || p != sb {
return nil
}
if err := sb.addSymbol(b.(*MethodBuilder)); err != nil {
return err
}
delete(sb.symbols, oldName)
return nil
}
func (sb *ServiceBuilder) addSymbol(b *MethodBuilder) error {
if _, ok := sb.symbols[b.GetName()]; ok {
return fmt.Errorf("service %s already contains method named %q", GetFullyQualifiedName(sb), b.GetName())
}
sb.symbols[b.GetName()] = b
return nil
}
// GetMethod returns the method with the given name. If no such method exists in
// the service, nil is returned.
func (sb *ServiceBuilder) GetMethod(name string) *MethodBuilder {
return sb.symbols[name]
}
// RemoveMethod removes the method with the given name. If no such method exists
// in the service, this is a no-op. This returns the service builder, for method
// chaining.
func (sb *ServiceBuilder) RemoveMethod(name string) *ServiceBuilder {
sb.TryRemoveMethod(name)
return sb
}
// TryRemoveMethod removes the method with the given name and returns false if
// the service has no such method.
func (sb *ServiceBuilder) TryRemoveMethod(name string) bool {
if mtb, ok := sb.symbols[name]; ok {
sb.removeChild(mtb)
return true
}
return false
}
// AddMethod adds the given method to this servuce. If an error prevents the
// method from being added, this method panics. This returns the service
// builder, for method chaining.
func (sb *ServiceBuilder) AddMethod(mtb *MethodBuilder) *ServiceBuilder {
if err := sb.TryAddMethod(mtb); err != nil {
panic(err)
}
return sb
}
// TryAddMethod adds the given field to this service, returning any error that
// prevents the field from being added (such as a name collision with another
// method already added to the service).
func (sb *ServiceBuilder) TryAddMethod(mtb *MethodBuilder) error {
if err := sb.addSymbol(mtb); err != nil {
return err
}
Unlink(mtb)
mtb.setParent(sb)
sb.methods = append(sb.methods, mtb)
return nil
}
// SetOptions sets the service options for this service and returns the service,
// for method chaining.
func (sb *ServiceBuilder) SetOptions(options *descriptorpb.ServiceOptions) *ServiceBuilder {
sb.Options = options
return sb
}
func (sb *ServiceBuilder) buildProto(path []int32, sourceInfo *descriptorpb.SourceCodeInfo) (*descriptorpb.ServiceDescriptorProto, error) {
addCommentsTo(sourceInfo, path, &sb.comments)
methods := make([]*descriptorpb.MethodDescriptorProto, 0, len(sb.methods))
for _, mtb := range sb.methods {
path := append(path, internal.Service_methodsTag, int32(len(methods)))
if mtd, err := mtb.buildProto(path, sourceInfo); err != nil {
return nil, err
} else {
methods = append(methods, mtd)
}
}
return &descriptorpb.ServiceDescriptorProto{
Name: proto.String(sb.name),
Options: sb.Options,
Method: methods,
}, nil
}
// Build constructs a service descriptor based on the contents of this service
// builder. If there are any problems constructing the descriptor, including
// resolving symbols referenced by the builder or failing to meet certain
// validation rules, an error is returned.
func (sb *ServiceBuilder) Build() (*desc.ServiceDescriptor, error) {
sd, err := sb.BuildDescriptor()
if err != nil {
return nil, err
}
return sd.(*desc.ServiceDescriptor), nil
}
// BuildDescriptor constructs a service descriptor based on the contents of this
// service builder. Most usages will prefer Build() instead, whose return type
// is a concrete descriptor type. This method is present to satisfy the Builder
// interface.
func (sb *ServiceBuilder) BuildDescriptor() (desc.Descriptor, error) {
return doBuild(sb, BuilderOptions{})
}
// MethodBuilder is a builder used to construct a desc.MethodDescriptor. A
// method builder *must* be added to a service before calling its Build()
// method.
//
// To create a new MethodBuilder, use NewMethod.
type MethodBuilder struct {
baseBuilder
Options *descriptorpb.MethodOptions
ReqType *RpcType
RespType *RpcType
}
// NewMethod creates a new MethodBuilder for a method with the given name and
// request and response types.
func NewMethod(name string, req, resp *RpcType) *MethodBuilder {
return &MethodBuilder{
baseBuilder: baseBuilderWithName(name),
ReqType: req,
RespType: resp,
}
}
// FromMethod returns a MethodBuilder that is effectively a copy of the given
// descriptor.
//
// Note that it is not just the given method that is copied but its entire file.
// So the caller can get the parent element of the returned builder and the
// result would be a builder that is effectively a copy of the method
// descriptor's parent service.
//
// This means that method builders created from descriptors do not need to be
// explicitly assigned to a file in order to preserve the original method's
// package name.
func FromMethod(mtd *desc.MethodDescriptor) (*MethodBuilder, error) {
if fb, err := FromFile(mtd.GetFile()); err != nil {
return nil, err
} else if mtb, ok := fb.findFullyQualifiedElement(mtd.GetFullyQualifiedName()).(*MethodBuilder); ok {
return mtb, nil
} else {
return nil, fmt.Errorf("could not find method %s after converting file %q to builder", mtd.GetFullyQualifiedName(), mtd.GetFile().GetName())
}
}
func fromMethod(mtd *desc.MethodDescriptor) (*MethodBuilder, error) {
req := RpcTypeImportedMessage(mtd.GetInputType(), mtd.IsClientStreaming())
resp := RpcTypeImportedMessage(mtd.GetOutputType(), mtd.IsServerStreaming())
mtb := NewMethod(mtd.GetName(), req, resp)
mtb.Options = mtd.GetMethodOptions()
setComments(&mtb.comments, mtd.GetSourceInfo())
return mtb, nil
}
// SetName changes this method's name, returning the method builder for method
// chaining. If the given new name is not valid (e.g. TrySetName would have
// returned an error) then this method will panic.
func (mtb *MethodBuilder) SetName(newName string) *MethodBuilder {
if err := mtb.TrySetName(newName); err != nil {
panic(err)
}
return mtb
}
// TrySetName changes this method's name. It will return an error if the given
// new name is not a valid protobuf identifier or if the parent service builder
// already has a method with the given name.
func (mtb *MethodBuilder) TrySetName(newName string) error {
return mtb.baseBuilder.setName(mtb, newName)
}
// SetComments sets the comments associated with the method. This method
// returns the method builder, for method chaining.
func (mtb *MethodBuilder) SetComments(c Comments) *MethodBuilder {
mtb.comments = c
return mtb
}
// GetChildren returns nil, since methods cannot have child elements. It is
// present to satisfy the Builder interface.
func (mtb *MethodBuilder) GetChildren() []Builder {
// methods do not have children
return nil
}
func (mtb *MethodBuilder) findChild(name string) Builder {
// methods do not have children
return nil
}
func (mtb *MethodBuilder) removeChild(b Builder) {
// methods do not have children
}
func (mtb *MethodBuilder) renamedChild(b Builder, oldName string) error {
// methods do not have children
return nil
}
// SetOptions sets the method options for this method and returns the method,
// for method chaining.
func (mtb *MethodBuilder) SetOptions(options *descriptorpb.MethodOptions) *MethodBuilder {
mtb.Options = options
return mtb
}
// SetRequestType changes the request type for the method and then returns the
// method builder, for method chaining.
func (mtb *MethodBuilder) SetRequestType(t *RpcType) *MethodBuilder {
mtb.ReqType = t
return mtb
}
// SetResponseType changes the response type for the method and then returns the
// method builder, for method chaining.
func (mtb *MethodBuilder) SetResponseType(t *RpcType) *MethodBuilder {
mtb.RespType = t
return mtb
}
func (mtb *MethodBuilder) buildProto(path []int32, sourceInfo *descriptorpb.SourceCodeInfo) (*descriptorpb.MethodDescriptorProto, error) {
addCommentsTo(sourceInfo, path, &mtb.comments)
mtd := &descriptorpb.MethodDescriptorProto{
Name: proto.String(mtb.name),
Options: mtb.Options,
InputType: proto.String("." + mtb.ReqType.GetTypeName()),
OutputType: proto.String("." + mtb.RespType.GetTypeName()),
}
if mtb.ReqType.IsStream {
mtd.ClientStreaming = proto.Bool(true)
}
if mtb.RespType.IsStream {
mtd.ServerStreaming = proto.Bool(true)
}
return mtd, nil
}
// Build constructs a method descriptor based on the contents of this method
// builder. If there are any problems constructing the descriptor, including
// resolving symbols referenced by the builder or failing to meet certain
// validation rules, an error is returned.
func (mtb *MethodBuilder) Build() (*desc.MethodDescriptor, error) {
mtd, err := mtb.BuildDescriptor()
if err != nil {
return nil, err
}
return mtd.(*desc.MethodDescriptor), nil
}
// BuildDescriptor constructs a method descriptor based on the contents of this
// method builder. Most usages will prefer Build() instead, whose return type is
// a concrete descriptor type. This method is present to satisfy the Builder
// interface.
func (mtb *MethodBuilder) BuildDescriptor() (desc.Descriptor, error) {
return doBuild(mtb, BuilderOptions{})
}