forked from jhump/protoreflect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
659 lines (583 loc) · 16.6 KB
/
field.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package ast
import "fmt"
// FieldDeclNode is a node in the AST that defines a field. This includes
// normal message fields as well as extensions. There are multiple types
// of AST nodes that declare fields:
// - *FieldNode
// - *GroupNode
// - *MapFieldNode
// - *SyntheticMapField
//
// This also allows NoSourceNode to be used in place of one of the above
// for some usages.
type FieldDeclNode interface {
Node
FieldLabel() Node
FieldName() Node
FieldType() Node
FieldTag() Node
FieldExtendee() Node
GetGroupKeyword() Node
GetOptions() *CompactOptionsNode
}
var _ FieldDeclNode = (*FieldNode)(nil)
var _ FieldDeclNode = (*GroupNode)(nil)
var _ FieldDeclNode = (*MapFieldNode)(nil)
var _ FieldDeclNode = (*SyntheticMapField)(nil)
var _ FieldDeclNode = NoSourceNode{}
// FieldNode represents a normal field declaration (not groups or maps). It
// can represent extension fields as well as non-extension fields (both inside
// of messages and inside of one-ofs). Example:
//
// optional string foo = 1;
type FieldNode struct {
compositeNode
Label FieldLabel
FldType IdentValueNode
Name *IdentNode
Equals *RuneNode
Tag *UintLiteralNode
Options *CompactOptionsNode
Semicolon *RuneNode
// This is an up-link to the containing *ExtendNode for fields
// that are defined inside of "extend" blocks.
Extendee *ExtendNode
}
func (*FieldNode) msgElement() {}
func (*FieldNode) oneOfElement() {}
func (*FieldNode) extendElement() {}
// NewFieldNode creates a new *FieldNode. The label and options arguments may be
// nil but the others must be non-nil.
// - label: The token corresponding to the label keyword if present ("optional",
// "required", or "repeated").
// - fieldType: The token corresponding to the field's type.
// - name: The token corresponding to the field's name.
// - equals: The token corresponding to the '=' rune after the name.
// - tag: The token corresponding to the field's tag number.
// - opts: Optional set of field options.
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewFieldNode(label *KeywordNode, fieldType IdentValueNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, semicolon *RuneNode) *FieldNode {
if fieldType == nil {
panic("fieldType is nil")
}
if name == nil {
panic("name is nil")
}
if equals == nil {
panic("equals is nil")
}
if tag == nil {
panic("tag is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
numChildren := 5
if label != nil {
numChildren++
}
if opts != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
if label != nil {
children = append(children, label)
}
children = append(children, fieldType, name, equals, tag)
if opts != nil {
children = append(children, opts)
}
children = append(children, semicolon)
return &FieldNode{
compositeNode: compositeNode{
children: children,
},
Label: newFieldLabel(label),
FldType: fieldType,
Name: name,
Equals: equals,
Tag: tag,
Options: opts,
Semicolon: semicolon,
}
}
func (n *FieldNode) FieldLabel() Node {
// proto3 fields and fields inside one-ofs will not have a label and we need
// this check in order to return a nil node -- otherwise we'd return a
// non-nil node that has a nil pointer value in it :/
if n.Label.KeywordNode == nil {
return nil
}
return n.Label.KeywordNode
}
func (n *FieldNode) FieldName() Node {
return n.Name
}
func (n *FieldNode) FieldType() Node {
return n.FldType
}
func (n *FieldNode) FieldTag() Node {
return n.Tag
}
func (n *FieldNode) FieldExtendee() Node {
if n.Extendee != nil {
return n.Extendee.Extendee
}
return nil
}
func (n *FieldNode) GetGroupKeyword() Node {
return nil
}
func (n *FieldNode) GetOptions() *CompactOptionsNode {
return n.Options
}
// FieldLabel represents the label of a field, which indicates its cardinality
// (i.e. whether it is optional, required, or repeated).
type FieldLabel struct {
*KeywordNode
Repeated bool
Required bool
}
func newFieldLabel(lbl *KeywordNode) FieldLabel {
repeated, required := false, false
if lbl != nil {
repeated = lbl.Val == "repeated"
required = lbl.Val == "required"
}
return FieldLabel{
KeywordNode: lbl,
Repeated: repeated,
Required: required,
}
}
// IsPresent returns true if a label keyword was present in the declaration
// and false if it was absent.
func (f *FieldLabel) IsPresent() bool {
return f.KeywordNode != nil
}
// GroupNode represents a group declaration, which doubles as a field and inline
// message declaration. It can represent extension fields as well as
// non-extension fields (both inside of messages and inside of one-ofs).
// Example:
//
// optional group Key = 4 {
// optional uint64 id = 1;
// optional string name = 2;
// }
type GroupNode struct {
compositeNode
Label FieldLabel
Keyword *KeywordNode
Name *IdentNode
Equals *RuneNode
Tag *UintLiteralNode
Options *CompactOptionsNode
MessageBody
// This is an up-link to the containing *ExtendNode for groups
// that are defined inside of "extend" blocks.
Extendee *ExtendNode
}
func (*GroupNode) msgElement() {}
func (*GroupNode) oneOfElement() {}
func (*GroupNode) extendElement() {}
// NewGroupNode creates a new *GroupNode. The label and options arguments may be
// nil but the others must be non-nil.
// - label: The token corresponding to the label keyword if present ("optional",
// "required", or "repeated").
// - keyword: The token corresponding to the "group" keyword.
// - name: The token corresponding to the field's name.
// - equals: The token corresponding to the '=' rune after the name.
// - tag: The token corresponding to the field's tag number.
// - opts: Optional set of field options.
// - openBrace: The token corresponding to the "{" rune that starts the body.
// - decls: All declarations inside the group body.
// - closeBrace: The token corresponding to the "}" rune that ends the body.
func NewGroupNode(label *KeywordNode, keyword *KeywordNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, openBrace *RuneNode, decls []MessageElement, closeBrace *RuneNode) *GroupNode {
if keyword == nil {
panic("fieldType is nil")
}
if name == nil {
panic("name is nil")
}
if equals == nil {
panic("equals is nil")
}
if tag == nil {
panic("tag is nil")
}
if openBrace == nil {
panic("openBrace is nil")
}
if closeBrace == nil {
panic("closeBrace is nil")
}
numChildren := 6 + len(decls)
if label != nil {
numChildren++
}
if opts != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
if label != nil {
children = append(children, label)
}
children = append(children, keyword, name, equals, tag)
if opts != nil {
children = append(children, opts)
}
children = append(children, openBrace)
for _, decl := range decls {
children = append(children, decl)
}
children = append(children, closeBrace)
ret := &GroupNode{
compositeNode: compositeNode{
children: children,
},
Label: newFieldLabel(label),
Keyword: keyword,
Name: name,
Equals: equals,
Tag: tag,
Options: opts,
}
populateMessageBody(&ret.MessageBody, openBrace, decls, closeBrace)
return ret
}
func (n *GroupNode) FieldLabel() Node {
if n.Label.KeywordNode == nil {
// return nil interface to indicate absence, not a typed nil
return nil
}
return n.Label.KeywordNode
}
func (n *GroupNode) FieldName() Node {
return n.Name
}
func (n *GroupNode) FieldType() Node {
return n.Keyword
}
func (n *GroupNode) FieldTag() Node {
return n.Tag
}
func (n *GroupNode) FieldExtendee() Node {
if n.Extendee != nil {
return n.Extendee.Extendee
}
return nil
}
func (n *GroupNode) GetGroupKeyword() Node {
return n.Keyword
}
func (n *GroupNode) GetOptions() *CompactOptionsNode {
return n.Options
}
func (n *GroupNode) MessageName() Node {
return n.Name
}
// OneOfDeclNode is a node in the AST that defines a oneof. There are
// multiple types of AST nodes that declare oneofs:
// - *OneOfNode
// - *SyntheticOneOf
//
// This also allows NoSourceNode to be used in place of one of the above
// for some usages.
type OneOfDeclNode interface {
Node
OneOfName() Node
}
// OneOfNode represents a one-of declaration. Example:
//
// oneof query {
// string by_name = 2;
// Type by_type = 3;
// Address by_address = 4;
// Labels by_label = 5;
// }
type OneOfNode struct {
compositeNode
Keyword *KeywordNode
Name *IdentNode
OpenBrace *RuneNode
Decls []OneOfElement
CloseBrace *RuneNode
}
func (*OneOfNode) msgElement() {}
// NewOneOfNode creates a new *OneOfNode. All arguments must be non-nil. While
// it is technically allowed for decls to be nil or empty, the resulting node
// will not be a valid oneof, which must have at least one field.
// - keyword: The token corresponding to the "oneof" keyword.
// - name: The token corresponding to the oneof's name.
// - openBrace: The token corresponding to the "{" rune that starts the body.
// - decls: All declarations inside the oneof body.
// - closeBrace: The token corresponding to the "}" rune that ends the body.
func NewOneOfNode(keyword *KeywordNode, name *IdentNode, openBrace *RuneNode, decls []OneOfElement, closeBrace *RuneNode) *OneOfNode {
if keyword == nil {
panic("keyword is nil")
}
if name == nil {
panic("name is nil")
}
if openBrace == nil {
panic("openBrace is nil")
}
if closeBrace == nil {
panic("closeBrace is nil")
}
children := make([]Node, 0, 4+len(decls))
children = append(children, keyword, name, openBrace)
for _, decl := range decls {
children = append(children, decl)
}
children = append(children, closeBrace)
for _, decl := range decls {
switch decl := decl.(type) {
case *OptionNode, *FieldNode, *GroupNode, *EmptyDeclNode:
default:
panic(fmt.Sprintf("invalid OneOfElement type: %T", decl))
}
}
return &OneOfNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Name: name,
OpenBrace: openBrace,
Decls: decls,
CloseBrace: closeBrace,
}
}
func (n *OneOfNode) OneOfName() Node {
return n.Name
}
// SyntheticOneOf is not an actual node in the AST but a synthetic node
// that implements OneOfDeclNode. These are used to represent the implicit
// oneof declarations that enclose "proto3 optional" fields.
type SyntheticOneOf struct {
Field *FieldNode
}
// NewSyntheticOneOf creates a new *SyntheticOneOf that corresponds to the
// given proto3 optional field.
func NewSyntheticOneOf(field *FieldNode) *SyntheticOneOf {
return &SyntheticOneOf{Field: field}
}
func (n *SyntheticOneOf) Start() *SourcePos {
return n.Field.Start()
}
func (n *SyntheticOneOf) End() *SourcePos {
return n.Field.End()
}
func (n *SyntheticOneOf) LeadingComments() []Comment {
return nil
}
func (n *SyntheticOneOf) TrailingComments() []Comment {
return nil
}
func (n *SyntheticOneOf) OneOfName() Node {
return n.Field.FieldName()
}
// OneOfElement is an interface implemented by all AST nodes that can
// appear in the body of a oneof declaration.
type OneOfElement interface {
Node
oneOfElement()
}
var _ OneOfElement = (*OptionNode)(nil)
var _ OneOfElement = (*FieldNode)(nil)
var _ OneOfElement = (*GroupNode)(nil)
var _ OneOfElement = (*EmptyDeclNode)(nil)
// MapTypeNode represents the type declaration for a map field. It defines
// both the key and value types for the map. Example:
//
// map<string, Values>
type MapTypeNode struct {
compositeNode
Keyword *KeywordNode
OpenAngle *RuneNode
KeyType *IdentNode
Comma *RuneNode
ValueType IdentValueNode
CloseAngle *RuneNode
}
// NewMapTypeNode creates a new *MapTypeNode. All arguments must be non-nil.
// - keyword: The token corresponding to the "map" keyword.
// - openAngle: The token corresponding to the "<" rune after the keyword.
// - keyType: The token corresponding to the key type for the map.
// - comma: The token corresponding to the "," rune between key and value types.
// - valType: The token corresponding to the value type for the map.
// - closeAngle: The token corresponding to the ">" rune that ends the declaration.
func NewMapTypeNode(keyword *KeywordNode, openAngle *RuneNode, keyType *IdentNode, comma *RuneNode, valType IdentValueNode, closeAngle *RuneNode) *MapTypeNode {
if keyword == nil {
panic("keyword is nil")
}
if openAngle == nil {
panic("openAngle is nil")
}
if keyType == nil {
panic("keyType is nil")
}
if comma == nil {
panic("comma is nil")
}
if valType == nil {
panic("valType is nil")
}
if closeAngle == nil {
panic("closeAngle is nil")
}
children := []Node{keyword, openAngle, keyType, comma, valType, closeAngle}
return &MapTypeNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
OpenAngle: openAngle,
KeyType: keyType,
Comma: comma,
ValueType: valType,
CloseAngle: closeAngle,
}
}
// MapFieldNode represents a map field declaration. Example:
//
// map<string,string> replacements = 3 [deprecated = true];
type MapFieldNode struct {
compositeNode
MapType *MapTypeNode
Name *IdentNode
Equals *RuneNode
Tag *UintLiteralNode
Options *CompactOptionsNode
Semicolon *RuneNode
}
func (*MapFieldNode) msgElement() {}
// NewMapFieldNode creates a new *MapFieldNode. All arguments must be non-nil
// except opts, which may be nil.
// - mapType: The token corresponding to the map type.
// - name: The token corresponding to the field's name.
// - equals: The token corresponding to the '=' rune after the name.
// - tag: The token corresponding to the field's tag number.
// - opts: Optional set of field options.
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewMapFieldNode(mapType *MapTypeNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, semicolon *RuneNode) *MapFieldNode {
if mapType == nil {
panic("mapType is nil")
}
if name == nil {
panic("name is nil")
}
if equals == nil {
panic("equals is nil")
}
if tag == nil {
panic("tag is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
numChildren := 5
if opts != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
children = append(children, mapType, name, equals, tag)
if opts != nil {
children = append(children, opts)
}
children = append(children, semicolon)
return &MapFieldNode{
compositeNode: compositeNode{
children: children,
},
MapType: mapType,
Name: name,
Equals: equals,
Tag: tag,
Options: opts,
Semicolon: semicolon,
}
}
func (n *MapFieldNode) FieldLabel() Node {
return nil
}
func (n *MapFieldNode) FieldName() Node {
return n.Name
}
func (n *MapFieldNode) FieldType() Node {
return n.MapType
}
func (n *MapFieldNode) FieldTag() Node {
return n.Tag
}
func (n *MapFieldNode) FieldExtendee() Node {
return nil
}
func (n *MapFieldNode) GetGroupKeyword() Node {
return nil
}
func (n *MapFieldNode) GetOptions() *CompactOptionsNode {
return n.Options
}
func (n *MapFieldNode) MessageName() Node {
return n.Name
}
func (n *MapFieldNode) KeyField() *SyntheticMapField {
return NewSyntheticMapField(n.MapType.KeyType, 1)
}
func (n *MapFieldNode) ValueField() *SyntheticMapField {
return NewSyntheticMapField(n.MapType.ValueType, 2)
}
// SyntheticMapField is not an actual node in the AST but a synthetic node
// that implements FieldDeclNode. These are used to represent the implicit
// field declarations of the "key" and "value" fields in a map entry.
type SyntheticMapField struct {
Ident IdentValueNode
Tag *UintLiteralNode
}
// NewSyntheticMapField creates a new *SyntheticMapField for the given
// identifier (either a key or value type in a map declaration) and tag
// number (1 for key, 2 for value).
func NewSyntheticMapField(ident IdentValueNode, tagNum uint64) *SyntheticMapField {
tag := &UintLiteralNode{
terminalNode: terminalNode{
posRange: PosRange{Start: *ident.Start(), End: *ident.End()},
},
Val: tagNum,
}
return &SyntheticMapField{Ident: ident, Tag: tag}
}
func (n *SyntheticMapField) Start() *SourcePos {
return n.Ident.Start()
}
func (n *SyntheticMapField) End() *SourcePos {
return n.Ident.End()
}
func (n *SyntheticMapField) LeadingComments() []Comment {
return nil
}
func (n *SyntheticMapField) TrailingComments() []Comment {
return nil
}
func (n *SyntheticMapField) FieldLabel() Node {
return n.Ident
}
func (n *SyntheticMapField) FieldName() Node {
return n.Ident
}
func (n *SyntheticMapField) FieldType() Node {
return n.Ident
}
func (n *SyntheticMapField) FieldTag() Node {
return n.Tag
}
func (n *SyntheticMapField) FieldExtendee() Node {
return nil
}
func (n *SyntheticMapField) GetGroupKeyword() Node {
return nil
}
func (n *SyntheticMapField) GetOptions() *CompactOptionsNode {
return nil
}