github.com/phpstudyer/protoreflect@v1.7.2/desc/protoparse/ast/field.go (about) 1 package ast 2 3 import "fmt" 4 5 // FieldDeclNode is a node in the AST that defines a field. This includes 6 // normal message fields as well as extensions. There are multiple types 7 // of AST nodes that declare fields: 8 // - *FieldNode 9 // - *GroupNode 10 // - *MapFieldNode 11 // This also allows NoSourceNode to be used in place of one of the above 12 // for some usages. 13 type FieldDeclNode interface { 14 Node 15 FieldLabel() Node 16 FieldName() Node 17 FieldType() Node 18 FieldTag() Node 19 FieldExtendee() Node 20 GetGroupKeyword() Node 21 GetOptions() *CompactOptionsNode 22 } 23 24 var _ FieldDeclNode = (*FieldNode)(nil) 25 var _ FieldDeclNode = (*GroupNode)(nil) 26 var _ FieldDeclNode = (*MapFieldNode)(nil) 27 var _ FieldDeclNode = (*SyntheticMapField)(nil) 28 var _ FieldDeclNode = NoSourceNode{} 29 30 // FieldNode represents a normal field declaration (not groups or maps). It 31 // can represent extension fields as well as non-extension fields (both inside 32 // of messages and inside of one-ofs). Example: 33 // 34 // optional string foo = 1; 35 type FieldNode struct { 36 compositeNode 37 Label FieldLabel 38 FldType IdentValueNode 39 Name *IdentNode 40 Equals *RuneNode 41 Tag *UintLiteralNode 42 Options *CompactOptionsNode 43 Semicolon *RuneNode 44 45 // This is an up-link to the containing *ExtendNode for fields 46 // that are defined inside of "extend" blocks. 47 Extendee *ExtendNode 48 } 49 50 func (*FieldNode) msgElement() {} 51 func (*FieldNode) oneOfElement() {} 52 func (*FieldNode) extendElement() {} 53 54 // NewFieldNode creates a new *FieldNode. The label and options arguments may be 55 // nil but the others must be non-nil. 56 // - label: The token corresponding to the label keyword if present ("optional", 57 // "required", or "repeated"). 58 // - fieldType: The token corresponding to the field's type. 59 // - name: The token corresponding to the field's name. 60 // - equals: The token corresponding to the '=' rune after the name. 61 // - tag: The token corresponding to the field's tag number. 62 // - opts: Optional set of field options. 63 // - semicolon: The token corresponding to the ";" rune that ends the declaration. 64 func NewFieldNode(label *KeywordNode, fieldType IdentValueNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, semicolon *RuneNode) *FieldNode { 65 if fieldType == nil { 66 panic("fieldType is nil") 67 } 68 if name == nil { 69 panic("name is nil") 70 } 71 if equals == nil { 72 panic("equals is nil") 73 } 74 if tag == nil { 75 panic("tag is nil") 76 } 77 if semicolon == nil { 78 panic("semicolon is nil") 79 } 80 numChildren := 5 81 if label != nil { 82 numChildren++ 83 } 84 if opts != nil { 85 numChildren++ 86 } 87 children := make([]Node, 0, numChildren) 88 if label != nil { 89 children = append(children, label) 90 } 91 children = append(children, fieldType, name, equals, tag) 92 if opts != nil { 93 children = append(children, opts) 94 } 95 children = append(children, semicolon) 96 97 return &FieldNode{ 98 compositeNode: compositeNode{ 99 children: children, 100 }, 101 Label: newFieldLabel(label), 102 FldType: fieldType, 103 Name: name, 104 Equals: equals, 105 Tag: tag, 106 Options: opts, 107 Semicolon: semicolon, 108 } 109 } 110 111 func (n *FieldNode) FieldLabel() Node { 112 // proto3 fields and fields inside one-ofs will not have a label and we need 113 // this check in order to return a nil node -- otherwise we'd return a 114 // non-nil node that has a nil pointer value in it :/ 115 if n.Label.KeywordNode == nil { 116 return nil 117 } 118 return n.Label.KeywordNode 119 } 120 121 func (n *FieldNode) FieldName() Node { 122 return n.Name 123 } 124 125 func (n *FieldNode) FieldType() Node { 126 return n.FldType 127 } 128 129 func (n *FieldNode) FieldTag() Node { 130 return n.Tag 131 } 132 133 func (n *FieldNode) FieldExtendee() Node { 134 if n.Extendee != nil { 135 return n.Extendee.Extendee 136 } 137 return nil 138 } 139 140 func (n *FieldNode) GetGroupKeyword() Node { 141 return nil 142 } 143 144 func (n *FieldNode) GetOptions() *CompactOptionsNode { 145 return n.Options 146 } 147 148 // FieldLabel represents the label of a field, which indicates its cardinality 149 // (i.e. whether it is optional, required, or repeated). 150 type FieldLabel struct { 151 *KeywordNode 152 Repeated bool 153 Required bool 154 } 155 156 func newFieldLabel(lbl *KeywordNode) FieldLabel { 157 repeated, required := false, false 158 if lbl != nil { 159 repeated = lbl.Val == "repeated" 160 required = lbl.Val == "required" 161 } 162 return FieldLabel{ 163 KeywordNode: lbl, 164 Repeated: repeated, 165 Required: required, 166 } 167 } 168 169 // IsPresent returns true if a label keyword was present in the declaration 170 // and false if it was absent. 171 func (f *FieldLabel) IsPresent() bool { 172 return f.KeywordNode != nil 173 } 174 175 // GroupNode represents a group declaration, which doubles as a field and inline 176 // message declaration. It can represent extension fields as well as 177 // non-extension fields (both inside of messages and inside of one-ofs). 178 // Example: 179 // 180 // optional group Key = 4 { 181 // optional uint64 id = 1; 182 // optional string name = 2; 183 // } 184 type GroupNode struct { 185 compositeNode 186 Label FieldLabel 187 Keyword *KeywordNode 188 Name *IdentNode 189 Equals *RuneNode 190 Tag *UintLiteralNode 191 Options *CompactOptionsNode 192 MessageBody 193 194 // This is an up-link to the containing *ExtendNode for groups 195 // that are defined inside of "extend" blocks. 196 Extendee *ExtendNode 197 } 198 199 func (*GroupNode) msgElement() {} 200 func (*GroupNode) oneOfElement() {} 201 func (*GroupNode) extendElement() {} 202 203 // NewGroupNode creates a new *GroupNode. The label and options arguments may be 204 // nil but the others must be non-nil. 205 // - label: The token corresponding to the label keyword if present ("optional", 206 // "required", or "repeated"). 207 // - keyword: The token corresponding to the "group" keyword. 208 // - name: The token corresponding to the field's name. 209 // - equals: The token corresponding to the '=' rune after the name. 210 // - tag: The token corresponding to the field's tag number. 211 // - opts: Optional set of field options. 212 // - openBrace: The token corresponding to the "{" rune that starts the body. 213 // - decls: All declarations inside the group body. 214 // - closeBrace: The token corresponding to the "}" rune that ends the body. 215 func NewGroupNode(label *KeywordNode, keyword *KeywordNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, openBrace *RuneNode, decls []MessageElement, closeBrace *RuneNode) *GroupNode { 216 if keyword == nil { 217 panic("fieldType is nil") 218 } 219 if name == nil { 220 panic("name is nil") 221 } 222 if equals == nil { 223 panic("equals is nil") 224 } 225 if tag == nil { 226 panic("tag is nil") 227 } 228 if openBrace == nil { 229 panic("openBrace is nil") 230 } 231 if closeBrace == nil { 232 panic("closeBrace is nil") 233 } 234 numChildren := 6 + len(decls) 235 if label != nil { 236 numChildren++ 237 } 238 if opts != nil { 239 numChildren++ 240 } 241 children := make([]Node, 0, numChildren) 242 if label != nil { 243 children = append(children, label) 244 } 245 children = append(children, keyword, name, equals, tag) 246 if opts != nil { 247 children = append(children, opts) 248 } 249 children = append(children, openBrace) 250 for _, decl := range decls { 251 children = append(children, decl) 252 } 253 children = append(children, closeBrace) 254 255 ret := &GroupNode{ 256 compositeNode: compositeNode{ 257 children: children, 258 }, 259 Label: newFieldLabel(label), 260 Keyword: keyword, 261 Name: name, 262 Equals: equals, 263 Tag: tag, 264 Options: opts, 265 } 266 populateMessageBody(&ret.MessageBody, openBrace, decls, closeBrace) 267 return ret 268 } 269 270 func (n *GroupNode) FieldLabel() Node { 271 if n.Label.KeywordNode == nil { 272 // return nil interface to indicate absence, not a typed nil 273 return nil 274 } 275 return n.Label.KeywordNode 276 } 277 278 func (n *GroupNode) FieldName() Node { 279 return n.Name 280 } 281 282 func (n *GroupNode) FieldType() Node { 283 return n.Keyword 284 } 285 286 func (n *GroupNode) FieldTag() Node { 287 return n.Tag 288 } 289 290 func (n *GroupNode) FieldExtendee() Node { 291 if n.Extendee != nil { 292 return n.Extendee.Extendee 293 } 294 return nil 295 } 296 297 func (n *GroupNode) GetGroupKeyword() Node { 298 return n.Keyword 299 } 300 301 func (n *GroupNode) GetOptions() *CompactOptionsNode { 302 return n.Options 303 } 304 305 func (n *GroupNode) MessageName() Node { 306 return n.Name 307 } 308 309 // OneOfNode represents a one-of declaration. Example: 310 // 311 // oneof query { 312 // string by_name = 2; 313 // Type by_type = 3; 314 // Address by_address = 4; 315 // Labels by_label = 5; 316 // } 317 type OneOfNode struct { 318 compositeNode 319 Keyword *KeywordNode 320 Name *IdentNode 321 OpenBrace *RuneNode 322 Decls []OneOfElement 323 CloseBrace *RuneNode 324 } 325 326 func (*OneOfNode) msgElement() {} 327 328 // NewOneOfNode creates a new *OneOfNode. All arguments must be non-nil. While 329 // it is technically allowed for decls to be nil or empty, the resulting node 330 // will not be a valid oneof, which must have at least one field. 331 // - keyword: The token corresponding to the "oneof" keyword. 332 // - name: The token corresponding to the oneof's name. 333 // - openBrace: The token corresponding to the "{" rune that starts the body. 334 // - decls: All declarations inside the oneof body. 335 // - closeBrace: The token corresponding to the "}" rune that ends the body. 336 func NewOneOfNode(keyword *KeywordNode, name *IdentNode, openBrace *RuneNode, decls []OneOfElement, closeBrace *RuneNode) *OneOfNode { 337 if keyword == nil { 338 panic("keyword is nil") 339 } 340 if name == nil { 341 panic("name is nil") 342 } 343 if openBrace == nil { 344 panic("openBrace is nil") 345 } 346 if closeBrace == nil { 347 panic("closeBrace is nil") 348 } 349 children := make([]Node, 0, 4+len(decls)) 350 children = append(children, keyword, name, openBrace) 351 for _, decl := range decls { 352 children = append(children, decl) 353 } 354 children = append(children, closeBrace) 355 356 for _, decl := range decls { 357 switch decl := decl.(type) { 358 case *OptionNode, *FieldNode, *GroupNode, *EmptyDeclNode: 359 default: 360 panic(fmt.Sprintf("invalid OneOfElement type: %T", decl)) 361 } 362 } 363 364 return &OneOfNode{ 365 compositeNode: compositeNode{ 366 children: children, 367 }, 368 Keyword: keyword, 369 Name: name, 370 OpenBrace: openBrace, 371 Decls: decls, 372 CloseBrace: closeBrace, 373 } 374 } 375 376 // OneOfElement is an interface implemented by all AST nodes that can 377 // appear in the body of a oneof declaration. 378 type OneOfElement interface { 379 Node 380 oneOfElement() 381 } 382 383 var _ OneOfElement = (*OptionNode)(nil) 384 var _ OneOfElement = (*FieldNode)(nil) 385 var _ OneOfElement = (*GroupNode)(nil) 386 var _ OneOfElement = (*EmptyDeclNode)(nil) 387 388 // MapTypeNode represents the type declaration for a map field. It defines 389 // both the key and value types for the map. Example: 390 // 391 // map<string, Values> 392 type MapTypeNode struct { 393 compositeNode 394 Keyword *KeywordNode 395 OpenAngle *RuneNode 396 KeyType *IdentNode 397 Comma *RuneNode 398 ValueType IdentValueNode 399 CloseAngle *RuneNode 400 } 401 402 // NewMapTypeNode creates a new *MapTypeNode. All arguments must be non-nil. 403 // - keyword: The token corresponding to the "map" keyword. 404 // - openAngle: The token corresponding to the "<" rune after the keyword. 405 // - keyType: The token corresponding to the key type for the map. 406 // - comma: The token corresponding to the "," rune between key and value types. 407 // - valType: The token corresponding to the value type for the map. 408 // - closeAngle: The token corresponding to the ">" rune that ends the declaration. 409 func NewMapTypeNode(keyword *KeywordNode, openAngle *RuneNode, keyType *IdentNode, comma *RuneNode, valType IdentValueNode, closeAngle *RuneNode) *MapTypeNode { 410 if keyword == nil { 411 panic("keyword is nil") 412 } 413 if openAngle == nil { 414 panic("openAngle is nil") 415 } 416 if keyType == nil { 417 panic("keyType is nil") 418 } 419 if comma == nil { 420 panic("comma is nil") 421 } 422 if valType == nil { 423 panic("valType is nil") 424 } 425 if closeAngle == nil { 426 panic("closeAngle is nil") 427 } 428 children := []Node{keyword, openAngle, keyType, comma, valType, closeAngle} 429 return &MapTypeNode{ 430 compositeNode: compositeNode{ 431 children: children, 432 }, 433 Keyword: keyword, 434 OpenAngle: openAngle, 435 KeyType: keyType, 436 Comma: comma, 437 ValueType: valType, 438 CloseAngle: closeAngle, 439 } 440 } 441 442 // MapFieldNode represents a map field declaration. Example: 443 // 444 // map<string,string> replacements = 3 [deprecated = true]; 445 type MapFieldNode struct { 446 compositeNode 447 MapType *MapTypeNode 448 Name *IdentNode 449 Equals *RuneNode 450 Tag *UintLiteralNode 451 Options *CompactOptionsNode 452 Semicolon *RuneNode 453 } 454 455 func (*MapFieldNode) msgElement() {} 456 457 // NewMapFieldNode creates a new *MapFieldNode. All arguments must be non-nil 458 // except opts, which may be nil. 459 // - mapType: The token corresponding to the map type. 460 // - name: The token corresponding to the field's name. 461 // - equals: The token corresponding to the '=' rune after the name. 462 // - tag: The token corresponding to the field's tag number. 463 // - opts: Optional set of field options. 464 // - semicolon: The token corresponding to the ";" rune that ends the declaration. 465 func NewMapFieldNode(mapType *MapTypeNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, semicolon *RuneNode) *MapFieldNode { 466 if mapType == nil { 467 panic("mapType is nil") 468 } 469 if name == nil { 470 panic("name is nil") 471 } 472 if equals == nil { 473 panic("equals is nil") 474 } 475 if tag == nil { 476 panic("tag is nil") 477 } 478 if semicolon == nil { 479 panic("semicolon is nil") 480 } 481 numChildren := 5 482 if opts != nil { 483 numChildren++ 484 } 485 children := make([]Node, 0, numChildren) 486 children = append(children, mapType, name, equals, tag) 487 if opts != nil { 488 children = append(children, opts) 489 } 490 children = append(children, semicolon) 491 492 return &MapFieldNode{ 493 compositeNode: compositeNode{ 494 children: children, 495 }, 496 MapType: mapType, 497 Name: name, 498 Equals: equals, 499 Tag: tag, 500 Options: opts, 501 Semicolon: semicolon, 502 } 503 } 504 505 func (n *MapFieldNode) FieldLabel() Node { 506 return nil 507 } 508 509 func (n *MapFieldNode) FieldName() Node { 510 return n.Name 511 } 512 513 func (n *MapFieldNode) FieldType() Node { 514 return n.MapType 515 } 516 517 func (n *MapFieldNode) FieldTag() Node { 518 return n.Tag 519 } 520 521 func (n *MapFieldNode) FieldExtendee() Node { 522 return nil 523 } 524 525 func (n *MapFieldNode) GetGroupKeyword() Node { 526 return nil 527 } 528 529 func (n *MapFieldNode) GetOptions() *CompactOptionsNode { 530 return n.Options 531 } 532 533 func (n *MapFieldNode) MessageName() Node { 534 return n.Name 535 } 536 537 func (n *MapFieldNode) KeyField() *SyntheticMapField { 538 return NewSyntheticMapField(n.MapType.KeyType, 1) 539 } 540 541 func (n *MapFieldNode) ValueField() *SyntheticMapField { 542 return NewSyntheticMapField(n.MapType.ValueType, 2) 543 } 544 545 // SyntheticMapField is not an actual node in the AST but a synthetic node 546 // that implements FieldDeclNode. These are used to represent the implicit 547 // field declarations of the "key" and "value" fields in a map entry. 548 type SyntheticMapField struct { 549 Ident IdentValueNode 550 Tag *UintLiteralNode 551 } 552 553 // NewSyntheticMapField creates a new *SyntheticMapField for the given 554 // identifier (either a key or value type in a map declaration) and tag 555 // number (1 for key, 2 for value). 556 func NewSyntheticMapField(ident IdentValueNode, tagNum uint64) *SyntheticMapField { 557 tag := &UintLiteralNode{ 558 terminalNode: terminalNode{ 559 posRange: PosRange{Start: *ident.Start(), End: *ident.End()}, 560 }, 561 Val: tagNum, 562 } 563 return &SyntheticMapField{Ident: ident, Tag: tag} 564 } 565 566 func (n *SyntheticMapField) Start() *SourcePos { 567 return n.Ident.Start() 568 } 569 570 func (n *SyntheticMapField) End() *SourcePos { 571 return n.Ident.End() 572 } 573 574 func (n *SyntheticMapField) LeadingComments() []Comment { 575 return nil 576 } 577 578 func (n *SyntheticMapField) TrailingComments() []Comment { 579 return nil 580 } 581 582 func (n *SyntheticMapField) FieldLabel() Node { 583 return n.Ident 584 } 585 586 func (n *SyntheticMapField) FieldName() Node { 587 return n.Ident 588 } 589 590 func (n *SyntheticMapField) FieldType() Node { 591 return n.Ident 592 } 593 594 func (n *SyntheticMapField) FieldTag() Node { 595 return n.Tag 596 } 597 598 func (n *SyntheticMapField) FieldExtendee() Node { 599 return nil 600 } 601 602 func (n *SyntheticMapField) GetGroupKeyword() Node { 603 return nil 604 } 605 606 func (n *SyntheticMapField) GetOptions() *CompactOptionsNode { 607 return nil 608 }