/
types.go
490 lines (417 loc) · 10.5 KB
/
types.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package resolver
import (
"time"
exprv1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/mercari/grpc-federation/source"
"github.com/mercari/grpc-federation/types"
)
type Package struct {
Name string
Files Files
}
type CELPlugin struct {
Name string
Desc string
Functions []*CELFunction
}
type CELFunction struct {
Name string
ID string
Args []*Type
Return *Type
Receiver *Message
}
type File struct {
Package *Package
GoPackage *GoPackage
Name string
Desc *descriptorpb.FileDescriptorProto
ImportFiles []*File
Services []*Service
Messages []*Message
Enums []*Enum
CELPlugins []*CELPlugin
}
type Files []*File
type GoPackage struct {
Name string
ImportPath string
AliasName string
}
type Service struct {
File *File
Name string
Methods []*Method
Rule *ServiceRule
Messages []*Message
MessageArgs []*Message
CELPlugins []*CELPlugin
}
type Method struct {
Service *Service
Name string
Request *Message
Response *Message
Rule *MethodRule
}
type ServiceRule struct {
}
type ServiceDependency struct {
Name string
Service *Service
}
type MethodRule struct {
Timeout *time.Duration
}
type Message struct {
File *File
Name string
IsMapEntry bool
ParentMessage *Message
NestedMessages []*Message
Enums []*Enum
Fields []*Field
Oneofs []*Oneof
Rule *MessageRule
}
type Enum struct {
File *File
Name string
Values []*EnumValue
Message *Message
Rule *EnumRule
}
type EnumRule struct {
Alias *Enum
}
type EnumValue struct {
Value string
Enum *Enum
Rule *EnumValueRule
}
type EnumValueRule struct {
Default bool
Aliases []*EnumValue
}
type MessageRule struct {
MessageArgument *Message
CustomResolver bool
Alias *Message
DefSet *VariableDefinitionSet
}
type VariableDefinitionSet struct {
Defs VariableDefinitions
Groups []VariableDefinitionGroup
Graph *MessageDependencyGraph
}
type VariableDefinition struct {
Idx int
Name string
If *CELValue
AutoBind bool
Used bool
Expr *VariableExpr
builder *source.VariableDefinitionOptionBuilder
}
type VariableDefinitions []*VariableDefinition
type VariableDefinitionGroupType string
const (
SequentialVariableDefinitionGroupType VariableDefinitionGroupType = "sequential"
ConcurrentVariableDefinitionGroupType VariableDefinitionGroupType = "concurrent"
)
type VariableDefinitionGroup interface {
Type() VariableDefinitionGroupType
VariableDefinitions() VariableDefinitions
treeFormat(*variableDefinitionGroupTreeFormatContext) string
setTextMaxLength(*variableDefinitionGroupTreeFormatContext)
}
type variableDefinitionGroupTreeFormatContext struct {
depth int
depthToMaxLength map[int]int
depthToIndent map[int]int
lineDepth map[int]struct{}
}
type SequentialVariableDefinitionGroup struct {
Start VariableDefinitionGroup
End *VariableDefinition
}
func (g *SequentialVariableDefinitionGroup) Type() VariableDefinitionGroupType {
return SequentialVariableDefinitionGroupType
}
type ConcurrentVariableDefinitionGroup struct {
Starts []VariableDefinitionGroup
End *VariableDefinition
}
func (g *ConcurrentVariableDefinitionGroup) Type() VariableDefinitionGroupType {
return ConcurrentVariableDefinitionGroupType
}
type VariableExpr struct {
Type *Type
By *CELValue
Map *MapExpr
Call *CallExpr
Message *MessageExpr
Validation *ValidationExpr
}
type CallExpr struct {
Method *Method
Request *Request
Timeout *time.Duration
Retry *RetryPolicy
Errors []*GRPCError
}
type MapExpr struct {
Iterator *Iterator
Expr *MapIteratorExpr
}
type Iterator struct {
Name string
Source *VariableDefinition
}
type MapIteratorExpr struct {
Type *Type
By *CELValue
Message *MessageExpr
}
type MessageExpr struct {
Message *Message
Args []*Argument
}
type ValidationExpr struct {
Name string
Error *GRPCError
}
type GRPCError struct {
DefSet *VariableDefinitionSet
If *CELValue
Code code.Code
Message *CELValue
Details GRPCErrorDetails
Ignore bool
IgnoreAndResponse *CELValue
}
type GRPCErrorDetails []*GRPCErrorDetail
type GRPCErrorDetail struct {
If *CELValue
DefSet *VariableDefinitionSet
Messages *VariableDefinitionSet
PreconditionFailures []*PreconditionFailure
BadRequests []*BadRequest
LocalizedMessages []*LocalizedMessage
}
type PreconditionFailure struct {
Violations []*PreconditionFailureViolation
}
type PreconditionFailureViolation struct {
Type *CELValue
Subject *CELValue
Description *CELValue
}
type BadRequest struct {
FieldViolations []*BadRequestFieldViolation
}
type BadRequestFieldViolation struct {
Field *CELValue
Description *CELValue
}
type LocalizedMessage struct {
Locale string
Message *CELValue
}
type TypeConversionDecl struct {
From *Type
To *Type
}
type Oneof struct {
Name string
Message *Message
Fields []*Field
}
type Field struct {
Name string
Type *Type
Oneof *Oneof
Rule *FieldRule
Message *Message
}
type OneofField struct {
*Field
}
type AutoBindField struct {
VariableDefinition *VariableDefinition
Field *Field
}
type FieldRule struct {
// Value value to bind to field.
Value *Value
// CustomResolver whether `custom_resolver = true` is set in grpc.federation.field option.
CustomResolver bool
// MessageCustomResolver whether `custom_resolver = true` is set in grpc.federation.message option.
MessageCustomResolver bool
// Alias valid if `alias` is specified in grpc.federation.field option.
Alias *Field
// AutoBindField valid if `autobind = true` is specified in resolver.response of grpc.federation.message option.
AutoBindField *AutoBindField
// Oneof represents oneof for field option.
Oneof *FieldOneofRule
}
// FieldOneofRule represents grpc.federation.field.oneof.
type FieldOneofRule struct {
If *CELValue
Default bool
By *CELValue
DefSet *VariableDefinitionSet
}
type AllMessageDependencyGraph struct {
Roots []*AllMessageDependencyGraphNode
}
type AllMessageDependencyGraphNode struct {
Parent []*AllMessageDependencyGraphNode
Children []*AllMessageDependencyGraphNode
Message *Message
}
type MessageDependencyGraph struct {
Roots []*MessageDependencyGraphNode
}
type MessageDependencyGraphNode struct {
Parent []*MessageDependencyGraphNode
Children []*MessageDependencyGraphNode
ParentMap map[*MessageDependencyGraphNode]struct{}
ChildrenMap map[*MessageDependencyGraphNode]struct{}
BaseMessage *Message
VariableDefinition *VariableDefinition
}
type Type struct {
Kind types.Kind
Repeated bool
Message *Message
Enum *Enum
OneofField *OneofField
}
func (t *Type) Clone() *Type {
return &Type{
Kind: t.Kind,
Repeated: t.Repeated,
Message: t.Message,
Enum: t.Enum,
OneofField: t.OneofField,
}
}
func (t *Type) IsNumber() bool {
switch t.Kind {
case types.Double,
types.Float,
types.Int64,
types.Uint64,
types.Int32,
types.Fixed64,
types.Fixed32,
types.Uint32,
types.Sfixed32,
types.Sfixed64,
types.Sint32,
types.Sint64,
types.Enum:
return true
}
return false
}
type MethodCall struct {
Method *Method
Request *Request
Timeout *time.Duration
Retry *RetryPolicy
}
type RetryPolicy struct {
If *CELValue
Constant *RetryPolicyConstant
Exponential *RetryPolicyExponential
}
func (p *RetryPolicy) MaxRetries() uint64 {
if p.Constant != nil {
return p.Constant.MaxRetries
}
if p.Exponential != nil {
return p.Exponential.MaxRetries
}
return 0
}
type RetryPolicyConstant struct {
Interval time.Duration
MaxRetries uint64
}
type RetryPolicyExponential struct {
InitialInterval time.Duration
RandomizationFactor float64
Multiplier float64
MaxInterval time.Duration
MaxRetries uint64
MaxElapsedTime time.Duration
}
type Request struct {
Args []*Argument
Type *Message
}
type GRPCErrorIndexes struct {
DefIdx int
ErrDetailIdx int
}
type Argument struct {
Name string
Type *Type
Value *Value
}
type Value struct {
Inline bool
CEL *CELValue
Const *ConstValue
}
type CELValue struct {
Expr string
Out *Type
CheckedExpr *exprv1.CheckedExpr
UseContextLibrary bool
}
type ConstValue struct {
Type *Type
Value interface{}
}
type EnvKey string
var (
DoubleType = &Type{Kind: types.Double}
FloatType = &Type{Kind: types.Float}
Int32Type = &Type{Kind: types.Int32}
Int64Type = &Type{Kind: types.Int64}
Uint32Type = &Type{Kind: types.Uint32}
Uint64Type = &Type{Kind: types.Uint64}
Sint32Type = &Type{Kind: types.Sint32}
Sint64Type = &Type{Kind: types.Sint64}
Fixed32Type = &Type{Kind: types.Fixed32}
Fixed64Type = &Type{Kind: types.Fixed64}
Sfixed32Type = &Type{Kind: types.Sfixed32}
Sfixed64Type = &Type{Kind: types.Sfixed64}
BoolType = &Type{Kind: types.Bool}
StringType = &Type{Kind: types.String}
BytesType = &Type{Kind: types.Bytes}
EnumType = &Type{Kind: types.Enum}
EnvType = &Type{Kind: types.String}
DoubleRepeatedType = &Type{Kind: types.Double, Repeated: true}
FloatRepeatedType = &Type{Kind: types.Float, Repeated: true}
Int32RepeatedType = &Type{Kind: types.Int32, Repeated: true}
Int64RepeatedType = &Type{Kind: types.Int64, Repeated: true}
Uint32RepeatedType = &Type{Kind: types.Uint32, Repeated: true}
Uint64RepeatedType = &Type{Kind: types.Uint64, Repeated: true}
Sint32RepeatedType = &Type{Kind: types.Sint32, Repeated: true}
Sint64RepeatedType = &Type{Kind: types.Sint64, Repeated: true}
Fixed32RepeatedType = &Type{Kind: types.Fixed32, Repeated: true}
Fixed64RepeatedType = &Type{Kind: types.Fixed64, Repeated: true}
Sfixed32RepeatedType = &Type{Kind: types.Sfixed32, Repeated: true}
Sfixed64RepeatedType = &Type{Kind: types.Sfixed64, Repeated: true}
BoolRepeatedType = &Type{Kind: types.Bool, Repeated: true}
StringRepeatedType = &Type{Kind: types.String, Repeated: true}
BytesRepeatedType = &Type{Kind: types.Bytes, Repeated: true}
EnumRepeatedType = &Type{Kind: types.Enum, Repeated: true}
EnvRepeatedType = &Type{Kind: types.String, Repeated: true}
)