github.com/syumai/protoreflect@v1.7.1-0.20200810020253-2ac7e3b3a321/desc/protoparse/options.go (about) 1 package protoparse 2 3 import ( 4 "bytes" 5 "fmt" 6 "math" 7 8 "github.com/golang/protobuf/proto" 9 dpb "github.com/golang/protobuf/protoc-gen-go/descriptor" 10 11 "github.com/syumai/protoreflect/desc" 12 "github.com/syumai/protoreflect/desc/internal" 13 "github.com/syumai/protoreflect/dynamic" 14 ) 15 16 // NB: To process options, we need descriptors, but we may not have rich 17 // descriptors when trying to interpret options for unlinked parsed files. 18 // So we define minimal interfaces that can be backed by both rich descriptors 19 // as well as their poorer cousins, plain ol' descriptor protos. 20 21 type descriptorish interface { 22 GetFile() fileDescriptorish 23 GetFullyQualifiedName() string 24 AsProto() proto.Message 25 } 26 27 type fileDescriptorish interface { 28 descriptorish 29 GetFileOptions() *dpb.FileOptions 30 GetPackage() string 31 FindSymbol(name string) desc.Descriptor 32 GetPublicDependencies() []fileDescriptorish 33 GetDependencies() []fileDescriptorish 34 GetMessageTypes() []msgDescriptorish 35 GetExtensions() []fldDescriptorish 36 GetEnumTypes() []enumDescriptorish 37 GetServices() []svcDescriptorish 38 } 39 40 type msgDescriptorish interface { 41 descriptorish 42 GetMessageOptions() *dpb.MessageOptions 43 GetFields() []fldDescriptorish 44 GetOneOfs() []oneofDescriptorish 45 GetExtensionRanges() []extRangeDescriptorish 46 GetNestedMessageTypes() []msgDescriptorish 47 GetNestedExtensions() []fldDescriptorish 48 GetNestedEnumTypes() []enumDescriptorish 49 } 50 51 type fldDescriptorish interface { 52 descriptorish 53 GetFieldOptions() *dpb.FieldOptions 54 GetMessageType() *desc.MessageDescriptor 55 GetEnumType() *desc.EnumDescriptor 56 AsFieldDescriptorProto() *dpb.FieldDescriptorProto 57 } 58 59 type oneofDescriptorish interface { 60 descriptorish 61 GetOneOfOptions() *dpb.OneofOptions 62 } 63 64 type enumDescriptorish interface { 65 descriptorish 66 GetEnumOptions() *dpb.EnumOptions 67 GetValues() []enumValDescriptorish 68 } 69 70 type enumValDescriptorish interface { 71 descriptorish 72 GetEnumValueOptions() *dpb.EnumValueOptions 73 } 74 75 type svcDescriptorish interface { 76 descriptorish 77 GetServiceOptions() *dpb.ServiceOptions 78 GetMethods() []methodDescriptorish 79 } 80 81 type methodDescriptorish interface { 82 descriptorish 83 GetMethodOptions() *dpb.MethodOptions 84 } 85 86 // The hierarchy of descriptorish implementations backed by 87 // rich descriptors: 88 89 type richFileDescriptorish struct { 90 *desc.FileDescriptor 91 } 92 93 func (d richFileDescriptorish) GetFile() fileDescriptorish { 94 return d 95 } 96 97 func (d richFileDescriptorish) GetPublicDependencies() []fileDescriptorish { 98 deps := d.FileDescriptor.GetPublicDependencies() 99 ret := make([]fileDescriptorish, len(deps)) 100 for i, d := range deps { 101 ret[i] = richFileDescriptorish{FileDescriptor: d} 102 } 103 return ret 104 } 105 106 func (d richFileDescriptorish) GetDependencies() []fileDescriptorish { 107 deps := d.FileDescriptor.GetDependencies() 108 ret := make([]fileDescriptorish, len(deps)) 109 for i, d := range deps { 110 ret[i] = richFileDescriptorish{FileDescriptor: d} 111 } 112 return ret 113 } 114 115 func (d richFileDescriptorish) GetMessageTypes() []msgDescriptorish { 116 msgs := d.FileDescriptor.GetMessageTypes() 117 ret := make([]msgDescriptorish, len(msgs)) 118 for i, m := range msgs { 119 ret[i] = richMsgDescriptorish{MessageDescriptor: m} 120 } 121 return ret 122 } 123 124 func (d richFileDescriptorish) GetExtensions() []fldDescriptorish { 125 flds := d.FileDescriptor.GetExtensions() 126 ret := make([]fldDescriptorish, len(flds)) 127 for i, f := range flds { 128 ret[i] = richFldDescriptorish{FieldDescriptor: f} 129 } 130 return ret 131 } 132 133 func (d richFileDescriptorish) GetEnumTypes() []enumDescriptorish { 134 ens := d.FileDescriptor.GetEnumTypes() 135 ret := make([]enumDescriptorish, len(ens)) 136 for i, en := range ens { 137 ret[i] = richEnumDescriptorish{EnumDescriptor: en} 138 } 139 return ret 140 } 141 142 func (d richFileDescriptorish) GetServices() []svcDescriptorish { 143 svcs := d.FileDescriptor.GetServices() 144 ret := make([]svcDescriptorish, len(svcs)) 145 for i, s := range svcs { 146 ret[i] = richSvcDescriptorish{ServiceDescriptor: s} 147 } 148 return ret 149 } 150 151 type richMsgDescriptorish struct { 152 *desc.MessageDescriptor 153 } 154 155 func (d richMsgDescriptorish) GetFile() fileDescriptorish { 156 return richFileDescriptorish{FileDescriptor: d.MessageDescriptor.GetFile()} 157 } 158 159 func (d richMsgDescriptorish) GetFields() []fldDescriptorish { 160 flds := d.MessageDescriptor.GetFields() 161 ret := make([]fldDescriptorish, len(flds)) 162 for i, f := range flds { 163 ret[i] = richFldDescriptorish{FieldDescriptor: f} 164 } 165 return ret 166 } 167 168 func (d richMsgDescriptorish) GetOneOfs() []oneofDescriptorish { 169 oos := d.MessageDescriptor.GetOneOfs() 170 ret := make([]oneofDescriptorish, len(oos)) 171 for i, oo := range oos { 172 ret[i] = richOneOfDescriptorish{OneOfDescriptor: oo} 173 } 174 return ret 175 } 176 177 func (d richMsgDescriptorish) GetExtensionRanges() []extRangeDescriptorish { 178 md := d.MessageDescriptor 179 mdFqn := md.GetFullyQualifiedName() 180 extrs := md.AsDescriptorProto().GetExtensionRange() 181 ret := make([]extRangeDescriptorish, len(extrs)) 182 for i, extr := range extrs { 183 ret[i] = extRangeDescriptorish{ 184 er: extr, 185 qual: mdFqn, 186 file: richFileDescriptorish{FileDescriptor: md.GetFile()}, 187 } 188 } 189 return ret 190 } 191 192 func (d richMsgDescriptorish) GetNestedMessageTypes() []msgDescriptorish { 193 msgs := d.MessageDescriptor.GetNestedMessageTypes() 194 ret := make([]msgDescriptorish, len(msgs)) 195 for i, m := range msgs { 196 ret[i] = richMsgDescriptorish{MessageDescriptor: m} 197 } 198 return ret 199 } 200 201 func (d richMsgDescriptorish) GetNestedExtensions() []fldDescriptorish { 202 flds := d.MessageDescriptor.GetNestedExtensions() 203 ret := make([]fldDescriptorish, len(flds)) 204 for i, f := range flds { 205 ret[i] = richFldDescriptorish{FieldDescriptor: f} 206 } 207 return ret 208 } 209 210 func (d richMsgDescriptorish) GetNestedEnumTypes() []enumDescriptorish { 211 ens := d.MessageDescriptor.GetNestedEnumTypes() 212 ret := make([]enumDescriptorish, len(ens)) 213 for i, en := range ens { 214 ret[i] = richEnumDescriptorish{EnumDescriptor: en} 215 } 216 return ret 217 } 218 219 type richFldDescriptorish struct { 220 *desc.FieldDescriptor 221 } 222 223 func (d richFldDescriptorish) GetFile() fileDescriptorish { 224 return richFileDescriptorish{FileDescriptor: d.FieldDescriptor.GetFile()} 225 } 226 227 func (d richFldDescriptorish) AsFieldDescriptorProto() *dpb.FieldDescriptorProto { 228 return d.FieldDescriptor.AsFieldDescriptorProto() 229 } 230 231 type richOneOfDescriptorish struct { 232 *desc.OneOfDescriptor 233 } 234 235 func (d richOneOfDescriptorish) GetFile() fileDescriptorish { 236 return richFileDescriptorish{FileDescriptor: d.OneOfDescriptor.GetFile()} 237 } 238 239 type richEnumDescriptorish struct { 240 *desc.EnumDescriptor 241 } 242 243 func (d richEnumDescriptorish) GetFile() fileDescriptorish { 244 return richFileDescriptorish{FileDescriptor: d.EnumDescriptor.GetFile()} 245 } 246 247 func (d richEnumDescriptorish) GetValues() []enumValDescriptorish { 248 vals := d.EnumDescriptor.GetValues() 249 ret := make([]enumValDescriptorish, len(vals)) 250 for i, val := range vals { 251 ret[i] = richEnumValDescriptorish{EnumValueDescriptor: val} 252 } 253 return ret 254 } 255 256 type richEnumValDescriptorish struct { 257 *desc.EnumValueDescriptor 258 } 259 260 func (d richEnumValDescriptorish) GetFile() fileDescriptorish { 261 return richFileDescriptorish{FileDescriptor: d.EnumValueDescriptor.GetFile()} 262 } 263 264 type richSvcDescriptorish struct { 265 *desc.ServiceDescriptor 266 } 267 268 func (d richSvcDescriptorish) GetFile() fileDescriptorish { 269 return richFileDescriptorish{FileDescriptor: d.ServiceDescriptor.GetFile()} 270 } 271 272 func (d richSvcDescriptorish) GetMethods() []methodDescriptorish { 273 mtds := d.ServiceDescriptor.GetMethods() 274 ret := make([]methodDescriptorish, len(mtds)) 275 for i, mtd := range mtds { 276 ret[i] = richMethodDescriptorish{MethodDescriptor: mtd} 277 } 278 return ret 279 } 280 281 type richMethodDescriptorish struct { 282 *desc.MethodDescriptor 283 } 284 285 func (d richMethodDescriptorish) GetFile() fileDescriptorish { 286 return richFileDescriptorish{FileDescriptor: d.MethodDescriptor.GetFile()} 287 } 288 289 // The hierarchy of descriptorish implementations backed by 290 // plain descriptor protos: 291 292 type poorFileDescriptorish struct { 293 *dpb.FileDescriptorProto 294 } 295 296 func (d poorFileDescriptorish) GetFile() fileDescriptorish { 297 return d 298 } 299 300 func (d poorFileDescriptorish) GetFullyQualifiedName() string { 301 return d.FileDescriptorProto.GetName() 302 } 303 304 func (d poorFileDescriptorish) AsProto() proto.Message { 305 return d.FileDescriptorProto 306 } 307 308 func (d poorFileDescriptorish) GetFileOptions() *dpb.FileOptions { 309 return d.FileDescriptorProto.GetOptions() 310 } 311 312 func (d poorFileDescriptorish) FindSymbol(name string) desc.Descriptor { 313 return nil 314 } 315 316 func (d poorFileDescriptorish) GetPublicDependencies() []fileDescriptorish { 317 return nil 318 } 319 320 func (d poorFileDescriptorish) GetDependencies() []fileDescriptorish { 321 return nil 322 } 323 324 func (d poorFileDescriptorish) GetMessageTypes() []msgDescriptorish { 325 msgs := d.FileDescriptorProto.GetMessageType() 326 pkg := d.FileDescriptorProto.GetPackage() 327 ret := make([]msgDescriptorish, len(msgs)) 328 for i, m := range msgs { 329 ret[i] = poorMsgDescriptorish{ 330 DescriptorProto: m, 331 qual: pkg, 332 file: d, 333 } 334 } 335 return ret 336 } 337 338 func (d poorFileDescriptorish) GetExtensions() []fldDescriptorish { 339 exts := d.FileDescriptorProto.GetExtension() 340 pkg := d.FileDescriptorProto.GetPackage() 341 ret := make([]fldDescriptorish, len(exts)) 342 for i, e := range exts { 343 ret[i] = poorFldDescriptorish{ 344 FieldDescriptorProto: e, 345 qual: pkg, 346 file: d, 347 } 348 } 349 return ret 350 } 351 352 func (d poorFileDescriptorish) GetEnumTypes() []enumDescriptorish { 353 ens := d.FileDescriptorProto.GetEnumType() 354 pkg := d.FileDescriptorProto.GetPackage() 355 ret := make([]enumDescriptorish, len(ens)) 356 for i, e := range ens { 357 ret[i] = poorEnumDescriptorish{ 358 EnumDescriptorProto: e, 359 qual: pkg, 360 file: d, 361 } 362 } 363 return ret 364 } 365 366 func (d poorFileDescriptorish) GetServices() []svcDescriptorish { 367 svcs := d.FileDescriptorProto.GetService() 368 pkg := d.FileDescriptorProto.GetPackage() 369 ret := make([]svcDescriptorish, len(svcs)) 370 for i, s := range svcs { 371 ret[i] = poorSvcDescriptorish{ 372 ServiceDescriptorProto: s, 373 qual: pkg, 374 file: d, 375 } 376 } 377 return ret 378 } 379 380 type poorMsgDescriptorish struct { 381 *dpb.DescriptorProto 382 qual string 383 file fileDescriptorish 384 } 385 386 func (d poorMsgDescriptorish) GetFile() fileDescriptorish { 387 return d.file 388 } 389 390 func (d poorMsgDescriptorish) GetFullyQualifiedName() string { 391 return qualify(d.qual, d.DescriptorProto.GetName()) 392 } 393 394 func qualify(qual, name string) string { 395 if qual == "" { 396 return name 397 } else { 398 return fmt.Sprintf("%s.%s", qual, name) 399 } 400 } 401 402 func (d poorMsgDescriptorish) AsProto() proto.Message { 403 return d.DescriptorProto 404 } 405 406 func (d poorMsgDescriptorish) GetMessageOptions() *dpb.MessageOptions { 407 return d.DescriptorProto.GetOptions() 408 } 409 410 func (d poorMsgDescriptorish) GetFields() []fldDescriptorish { 411 flds := d.DescriptorProto.GetField() 412 ret := make([]fldDescriptorish, len(flds)) 413 for i, f := range flds { 414 ret[i] = poorFldDescriptorish{ 415 FieldDescriptorProto: f, 416 qual: d.GetFullyQualifiedName(), 417 file: d.file, 418 } 419 } 420 return ret 421 } 422 423 func (d poorMsgDescriptorish) GetOneOfs() []oneofDescriptorish { 424 oos := d.DescriptorProto.GetOneofDecl() 425 ret := make([]oneofDescriptorish, len(oos)) 426 for i, oo := range oos { 427 ret[i] = poorOneOfDescriptorish{ 428 OneofDescriptorProto: oo, 429 qual: d.GetFullyQualifiedName(), 430 file: d.file, 431 } 432 } 433 return ret 434 } 435 436 func (d poorMsgDescriptorish) GetExtensionRanges() []extRangeDescriptorish { 437 mdFqn := d.GetFullyQualifiedName() 438 extrs := d.DescriptorProto.GetExtensionRange() 439 ret := make([]extRangeDescriptorish, len(extrs)) 440 for i, extr := range extrs { 441 ret[i] = extRangeDescriptorish{ 442 er: extr, 443 qual: mdFqn, 444 file: d.file, 445 } 446 } 447 return ret 448 } 449 450 func (d poorMsgDescriptorish) GetNestedMessageTypes() []msgDescriptorish { 451 msgs := d.DescriptorProto.GetNestedType() 452 ret := make([]msgDescriptorish, len(msgs)) 453 for i, m := range msgs { 454 ret[i] = poorMsgDescriptorish{ 455 DescriptorProto: m, 456 qual: d.GetFullyQualifiedName(), 457 file: d.file, 458 } 459 } 460 return ret 461 } 462 463 func (d poorMsgDescriptorish) GetNestedExtensions() []fldDescriptorish { 464 flds := d.DescriptorProto.GetExtension() 465 ret := make([]fldDescriptorish, len(flds)) 466 for i, f := range flds { 467 ret[i] = poorFldDescriptorish{ 468 FieldDescriptorProto: f, 469 qual: d.GetFullyQualifiedName(), 470 file: d.file, 471 } 472 } 473 return ret 474 } 475 476 func (d poorMsgDescriptorish) GetNestedEnumTypes() []enumDescriptorish { 477 ens := d.DescriptorProto.GetEnumType() 478 ret := make([]enumDescriptorish, len(ens)) 479 for i, en := range ens { 480 ret[i] = poorEnumDescriptorish{ 481 EnumDescriptorProto: en, 482 qual: d.GetFullyQualifiedName(), 483 file: d.file, 484 } 485 } 486 return ret 487 } 488 489 type poorFldDescriptorish struct { 490 *dpb.FieldDescriptorProto 491 qual string 492 file fileDescriptorish 493 } 494 495 func (d poorFldDescriptorish) GetFile() fileDescriptorish { 496 return d.file 497 } 498 499 func (d poorFldDescriptorish) GetFullyQualifiedName() string { 500 return qualify(d.qual, d.FieldDescriptorProto.GetName()) 501 } 502 503 func (d poorFldDescriptorish) AsProto() proto.Message { 504 return d.FieldDescriptorProto 505 } 506 507 func (d poorFldDescriptorish) GetFieldOptions() *dpb.FieldOptions { 508 return d.FieldDescriptorProto.GetOptions() 509 } 510 511 func (d poorFldDescriptorish) GetMessageType() *desc.MessageDescriptor { 512 return nil 513 } 514 515 func (d poorFldDescriptorish) GetEnumType() *desc.EnumDescriptor { 516 return nil 517 } 518 519 type poorOneOfDescriptorish struct { 520 *dpb.OneofDescriptorProto 521 qual string 522 file fileDescriptorish 523 } 524 525 func (d poorOneOfDescriptorish) GetFile() fileDescriptorish { 526 return d.file 527 } 528 529 func (d poorOneOfDescriptorish) GetFullyQualifiedName() string { 530 return qualify(d.qual, d.OneofDescriptorProto.GetName()) 531 } 532 533 func (d poorOneOfDescriptorish) AsProto() proto.Message { 534 return d.OneofDescriptorProto 535 } 536 537 func (d poorOneOfDescriptorish) GetOneOfOptions() *dpb.OneofOptions { 538 return d.OneofDescriptorProto.GetOptions() 539 } 540 541 func (d poorFldDescriptorish) AsFieldDescriptorProto() *dpb.FieldDescriptorProto { 542 return d.FieldDescriptorProto 543 } 544 545 type poorEnumDescriptorish struct { 546 *dpb.EnumDescriptorProto 547 qual string 548 file fileDescriptorish 549 } 550 551 func (d poorEnumDescriptorish) GetFile() fileDescriptorish { 552 return d.file 553 } 554 555 func (d poorEnumDescriptorish) GetFullyQualifiedName() string { 556 return qualify(d.qual, d.EnumDescriptorProto.GetName()) 557 } 558 559 func (d poorEnumDescriptorish) AsProto() proto.Message { 560 return d.EnumDescriptorProto 561 } 562 563 func (d poorEnumDescriptorish) GetEnumOptions() *dpb.EnumOptions { 564 return d.EnumDescriptorProto.GetOptions() 565 } 566 567 func (d poorEnumDescriptorish) GetValues() []enumValDescriptorish { 568 vals := d.EnumDescriptorProto.GetValue() 569 ret := make([]enumValDescriptorish, len(vals)) 570 for i, v := range vals { 571 ret[i] = poorEnumValDescriptorish{ 572 EnumValueDescriptorProto: v, 573 qual: d.GetFullyQualifiedName(), 574 file: d.file, 575 } 576 } 577 return ret 578 } 579 580 type poorEnumValDescriptorish struct { 581 *dpb.EnumValueDescriptorProto 582 qual string 583 file fileDescriptorish 584 } 585 586 func (d poorEnumValDescriptorish) GetFile() fileDescriptorish { 587 return d.file 588 } 589 590 func (d poorEnumValDescriptorish) GetFullyQualifiedName() string { 591 return qualify(d.qual, d.EnumValueDescriptorProto.GetName()) 592 } 593 594 func (d poorEnumValDescriptorish) AsProto() proto.Message { 595 return d.EnumValueDescriptorProto 596 } 597 598 func (d poorEnumValDescriptorish) GetEnumValueOptions() *dpb.EnumValueOptions { 599 return d.EnumValueDescriptorProto.GetOptions() 600 } 601 602 type poorSvcDescriptorish struct { 603 *dpb.ServiceDescriptorProto 604 qual string 605 file fileDescriptorish 606 } 607 608 func (d poorSvcDescriptorish) GetFile() fileDescriptorish { 609 return d.file 610 } 611 612 func (d poorSvcDescriptorish) GetFullyQualifiedName() string { 613 return qualify(d.qual, d.ServiceDescriptorProto.GetName()) 614 } 615 616 func (d poorSvcDescriptorish) AsProto() proto.Message { 617 return d.ServiceDescriptorProto 618 } 619 620 func (d poorSvcDescriptorish) GetServiceOptions() *dpb.ServiceOptions { 621 return d.ServiceDescriptorProto.GetOptions() 622 } 623 624 func (d poorSvcDescriptorish) GetMethods() []methodDescriptorish { 625 mtds := d.ServiceDescriptorProto.GetMethod() 626 ret := make([]methodDescriptorish, len(mtds)) 627 for i, m := range mtds { 628 ret[i] = poorMethodDescriptorish{ 629 MethodDescriptorProto: m, 630 qual: d.GetFullyQualifiedName(), 631 file: d.file, 632 } 633 } 634 return ret 635 } 636 637 type poorMethodDescriptorish struct { 638 *dpb.MethodDescriptorProto 639 qual string 640 file fileDescriptorish 641 } 642 643 func (d poorMethodDescriptorish) GetFile() fileDescriptorish { 644 return d.file 645 } 646 647 func (d poorMethodDescriptorish) GetFullyQualifiedName() string { 648 return qualify(d.qual, d.MethodDescriptorProto.GetName()) 649 } 650 651 func (d poorMethodDescriptorish) AsProto() proto.Message { 652 return d.MethodDescriptorProto 653 } 654 655 func (d poorMethodDescriptorish) GetMethodOptions() *dpb.MethodOptions { 656 return d.MethodDescriptorProto.GetOptions() 657 } 658 659 type extRangeDescriptorish struct { 660 er *dpb.DescriptorProto_ExtensionRange 661 qual string 662 file fileDescriptorish 663 } 664 665 func (er extRangeDescriptorish) GetFile() fileDescriptorish { 666 return er.file 667 } 668 669 func (er extRangeDescriptorish) GetFullyQualifiedName() string { 670 return qualify(er.qual, fmt.Sprintf("%d-%d", er.er.GetStart(), er.er.GetEnd()-1)) 671 } 672 673 func (er extRangeDescriptorish) AsProto() proto.Message { 674 return er.er 675 } 676 677 func (er extRangeDescriptorish) GetExtensionRangeOptions() *dpb.ExtensionRangeOptions { 678 return er.er.GetOptions() 679 } 680 681 func interpretFileOptions(r *parseResult, fd fileDescriptorish) error { 682 opts := fd.GetFileOptions() 683 if opts != nil { 684 if len(opts.UninterpretedOption) > 0 { 685 if remain, err := interpretOptions(r, fd, opts, opts.UninterpretedOption); err != nil { 686 return err 687 } else { 688 opts.UninterpretedOption = remain 689 } 690 } 691 } 692 for _, md := range fd.GetMessageTypes() { 693 if err := interpretMessageOptions(r, md); err != nil { 694 return err 695 } 696 } 697 for _, fld := range fd.GetExtensions() { 698 if err := interpretFieldOptions(r, fld); err != nil { 699 return err 700 } 701 } 702 for _, ed := range fd.GetEnumTypes() { 703 if err := interpretEnumOptions(r, ed); err != nil { 704 return err 705 } 706 } 707 for _, sd := range fd.GetServices() { 708 opts := sd.GetServiceOptions() 709 if len(opts.GetUninterpretedOption()) > 0 { 710 if remain, err := interpretOptions(r, sd, opts, opts.UninterpretedOption); err != nil { 711 return err 712 } else { 713 opts.UninterpretedOption = remain 714 } 715 } 716 for _, mtd := range sd.GetMethods() { 717 opts := mtd.GetMethodOptions() 718 if len(opts.GetUninterpretedOption()) > 0 { 719 if remain, err := interpretOptions(r, mtd, opts, opts.UninterpretedOption); err != nil { 720 return err 721 } else { 722 opts.UninterpretedOption = remain 723 } 724 } 725 } 726 } 727 return nil 728 } 729 730 func interpretMessageOptions(r *parseResult, md msgDescriptorish) error { 731 opts := md.GetMessageOptions() 732 if opts != nil { 733 if len(opts.UninterpretedOption) > 0 { 734 if remain, err := interpretOptions(r, md, opts, opts.UninterpretedOption); err != nil { 735 return err 736 } else { 737 opts.UninterpretedOption = remain 738 } 739 } 740 } 741 for _, fld := range md.GetFields() { 742 if err := interpretFieldOptions(r, fld); err != nil { 743 return err 744 } 745 } 746 for _, ood := range md.GetOneOfs() { 747 opts := ood.GetOneOfOptions() 748 if len(opts.GetUninterpretedOption()) > 0 { 749 if remain, err := interpretOptions(r, ood, opts, opts.UninterpretedOption); err != nil { 750 return err 751 } else { 752 opts.UninterpretedOption = remain 753 } 754 } 755 } 756 for _, fld := range md.GetNestedExtensions() { 757 if err := interpretFieldOptions(r, fld); err != nil { 758 return err 759 } 760 } 761 for _, er := range md.GetExtensionRanges() { 762 opts := er.GetExtensionRangeOptions() 763 if len(opts.GetUninterpretedOption()) > 0 { 764 if remain, err := interpretOptions(r, er, opts, opts.UninterpretedOption); err != nil { 765 return err 766 } else { 767 opts.UninterpretedOption = remain 768 } 769 } 770 } 771 for _, nmd := range md.GetNestedMessageTypes() { 772 if err := interpretMessageOptions(r, nmd); err != nil { 773 return err 774 } 775 } 776 for _, ed := range md.GetNestedEnumTypes() { 777 if err := interpretEnumOptions(r, ed); err != nil { 778 return err 779 } 780 } 781 return nil 782 } 783 784 func interpretFieldOptions(r *parseResult, fld fldDescriptorish) error { 785 opts := fld.GetFieldOptions() 786 if len(opts.GetUninterpretedOption()) > 0 { 787 uo := opts.UninterpretedOption 788 scope := fmt.Sprintf("field %s", fld.GetFullyQualifiedName()) 789 790 // process json_name pseudo-option 791 if index, err := findOption(r, scope, uo, "json_name"); err != nil && !r.lenient { 792 return err 793 } else if index >= 0 { 794 opt := uo[index] 795 optNode := r.getOptionNode(opt) 796 797 // attribute source code info 798 if on, ok := optNode.(*optionNode); ok { 799 r.interpretedOptions[on] = []int32{-1, internal.Field_jsonNameTag} 800 } 801 uo = removeOption(uo, index) 802 if opt.StringValue == nil { 803 if err := r.errs.handleErrorWithPos(optNode.getValue().start(), "%s: expecting string value for json_name option", scope); err != nil { 804 return err 805 } 806 } else { 807 fld.AsFieldDescriptorProto().JsonName = proto.String(string(opt.StringValue)) 808 } 809 } 810 811 // and process default pseudo-option 812 if index, err := processDefaultOption(r, scope, fld, uo); err != nil && !r.lenient { 813 return err 814 } else if index >= 0 { 815 // attribute source code info 816 optNode := r.getOptionNode(uo[index]) 817 if on, ok := optNode.(*optionNode); ok { 818 r.interpretedOptions[on] = []int32{-1, internal.Field_defaultTag} 819 } 820 uo = removeOption(uo, index) 821 } 822 823 if len(uo) == 0 { 824 // no real options, only pseudo-options above? clear out options 825 fld.AsFieldDescriptorProto().Options = nil 826 } else if remain, err := interpretOptions(r, fld, opts, uo); err != nil { 827 return err 828 } else { 829 opts.UninterpretedOption = remain 830 } 831 } 832 return nil 833 } 834 835 func processDefaultOption(res *parseResult, scope string, fld fldDescriptorish, uos []*dpb.UninterpretedOption) (defaultIndex int, err error) { 836 found, err := findOption(res, scope, uos, "default") 837 if err != nil || found == -1 { 838 return -1, err 839 } 840 opt := uos[found] 841 optNode := res.getOptionNode(opt) 842 fdp := fld.AsFieldDescriptorProto() 843 if fdp.GetLabel() == dpb.FieldDescriptorProto_LABEL_REPEATED { 844 return -1, res.errs.handleErrorWithPos(optNode.getName().start(), "%s: default value cannot be set because field is repeated", scope) 845 } 846 if fdp.GetType() == dpb.FieldDescriptorProto_TYPE_GROUP || fdp.GetType() == dpb.FieldDescriptorProto_TYPE_MESSAGE { 847 return -1, res.errs.handleErrorWithPos(optNode.getName().start(), "%s: default value cannot be set because field is a message", scope) 848 } 849 val := optNode.getValue() 850 if _, ok := val.(*aggregateLiteralNode); ok { 851 return -1, res.errs.handleErrorWithPos(val.start(), "%s: default value cannot be an aggregate", scope) 852 } 853 mc := &messageContext{ 854 res: res, 855 file: fld.GetFile(), 856 elementName: fld.GetFullyQualifiedName(), 857 elementType: descriptorType(fld.AsProto()), 858 option: opt, 859 } 860 v, err := fieldValue(res, mc, fld, val, true) 861 if err != nil { 862 return -1, res.errs.handleError(err) 863 } 864 if str, ok := v.(string); ok { 865 fld.AsFieldDescriptorProto().DefaultValue = proto.String(str) 866 } else if b, ok := v.([]byte); ok { 867 fld.AsFieldDescriptorProto().DefaultValue = proto.String(encodeDefaultBytes(b)) 868 } else { 869 var flt float64 870 var ok bool 871 if flt, ok = v.(float64); !ok { 872 var flt32 float32 873 if flt32, ok = v.(float32); ok { 874 flt = float64(flt32) 875 } 876 } 877 if ok { 878 if math.IsInf(flt, 1) { 879 fld.AsFieldDescriptorProto().DefaultValue = proto.String("inf") 880 } else if ok && math.IsInf(flt, -1) { 881 fld.AsFieldDescriptorProto().DefaultValue = proto.String("-inf") 882 } else if ok && math.IsNaN(flt) { 883 fld.AsFieldDescriptorProto().DefaultValue = proto.String("nan") 884 } else { 885 fld.AsFieldDescriptorProto().DefaultValue = proto.String(fmt.Sprintf("%v", v)) 886 } 887 } else { 888 fld.AsFieldDescriptorProto().DefaultValue = proto.String(fmt.Sprintf("%v", v)) 889 } 890 } 891 return found, nil 892 } 893 894 func encodeDefaultBytes(b []byte) string { 895 var buf bytes.Buffer 896 writeEscapedBytes(&buf, b) 897 return buf.String() 898 } 899 900 func interpretEnumOptions(r *parseResult, ed enumDescriptorish) error { 901 opts := ed.GetEnumOptions() 902 if opts != nil { 903 if len(opts.UninterpretedOption) > 0 { 904 if remain, err := interpretOptions(r, ed, opts, opts.UninterpretedOption); err != nil { 905 return err 906 } else { 907 opts.UninterpretedOption = remain 908 } 909 } 910 } 911 for _, evd := range ed.GetValues() { 912 opts := evd.GetEnumValueOptions() 913 if len(opts.GetUninterpretedOption()) > 0 { 914 if remain, err := interpretOptions(r, evd, opts, opts.UninterpretedOption); err != nil { 915 return err 916 } else { 917 opts.UninterpretedOption = remain 918 } 919 } 920 } 921 return nil 922 } 923 924 func interpretOptions(res *parseResult, element descriptorish, opts proto.Message, uninterpreted []*dpb.UninterpretedOption) ([]*dpb.UninterpretedOption, error) { 925 optsd, err := desc.LoadMessageDescriptorForMessage(opts) 926 if err != nil { 927 if res.lenient { 928 return uninterpreted, nil 929 } 930 return nil, res.errs.handleError(err) 931 } 932 dm := dynamic.NewMessage(optsd) 933 err = dm.ConvertFrom(opts) 934 if err != nil { 935 if res.lenient { 936 return uninterpreted, nil 937 } 938 node := res.nodes[element.AsProto()] 939 return nil, res.errs.handleError(ErrorWithSourcePos{Pos: node.start(), Underlying: err}) 940 } 941 942 mc := &messageContext{res: res, file: element.GetFile(), elementName: element.GetFullyQualifiedName(), elementType: descriptorType(element.AsProto())} 943 var remain []*dpb.UninterpretedOption 944 for _, uo := range uninterpreted { 945 node := res.getOptionNode(uo) 946 if !uo.Name[0].GetIsExtension() && uo.Name[0].GetNamePart() == "uninterpreted_option" { 947 if res.lenient { 948 remain = append(remain, uo) 949 continue 950 } 951 // uninterpreted_option might be found reflectively, but is not actually valid for use 952 if err := res.errs.handleErrorWithPos(node.getName().start(), "%vinvalid option 'uninterpreted_option'", mc); err != nil { 953 return nil, err 954 } 955 } 956 mc.option = uo 957 path, err := interpretField(res, mc, element, dm, uo, 0, nil) 958 if err != nil { 959 if res.lenient { 960 remain = append(remain, uo) 961 continue 962 } 963 return nil, err 964 } 965 if optn, ok := node.(*optionNode); ok { 966 res.interpretedOptions[optn] = path 967 } 968 } 969 970 if res.lenient { 971 // If we're lenient, then we don't want to clobber the passed in message 972 // and leave it partially populated. So we convert into a copy first 973 optsClone := proto.Clone(opts) 974 if err := dm.ConvertToDeterministic(optsClone); err != nil { 975 // TODO: do this in a more granular way, so we can convert individual 976 // fields and leave bad ones uninterpreted instead of skipping all of 977 // the work we've done so far. 978 return uninterpreted, nil 979 } 980 // conversion from dynamic message above worked, so now 981 // it is safe to overwrite the passed in message 982 opts.Reset() 983 proto.Merge(opts, optsClone) 984 985 return remain, nil 986 } 987 988 if err := dm.ValidateRecursive(); err != nil { 989 node := res.nodes[element.AsProto()] 990 if err := res.errs.handleErrorWithPos(node.start(), "error in %s options: %v", descriptorType(element.AsProto()), err); err != nil { 991 return nil, err 992 } 993 } 994 995 // nw try to convert into the passed in message and fail if not successful 996 if err := dm.ConvertToDeterministic(opts); err != nil { 997 node := res.nodes[element.AsProto()] 998 return nil, res.errs.handleError(ErrorWithSourcePos{Pos: node.start(), Underlying: err}) 999 } 1000 1001 return nil, nil 1002 } 1003 1004 func interpretField(res *parseResult, mc *messageContext, element descriptorish, dm *dynamic.Message, opt *dpb.UninterpretedOption, nameIndex int, pathPrefix []int32) (path []int32, err error) { 1005 var fld *desc.FieldDescriptor 1006 nm := opt.GetName()[nameIndex] 1007 node := res.getOptionNamePartNode(nm) 1008 if nm.GetIsExtension() { 1009 extName := nm.GetNamePart() 1010 if extName[0] == '.' { 1011 extName = extName[1:] /* skip leading dot */ 1012 } 1013 fld = findExtension(element.GetFile(), extName, false, map[fileDescriptorish]struct{}{}) 1014 if fld == nil { 1015 return nil, res.errs.handleErrorWithPos(node.start(), 1016 "%vunrecognized extension %s of %s", 1017 mc, extName, dm.GetMessageDescriptor().GetFullyQualifiedName()) 1018 } 1019 if fld.GetOwner().GetFullyQualifiedName() != dm.GetMessageDescriptor().GetFullyQualifiedName() { 1020 return nil, res.errs.handleErrorWithPos(node.start(), 1021 "%vextension %s should extend %s but instead extends %s", 1022 mc, extName, dm.GetMessageDescriptor().GetFullyQualifiedName(), fld.GetOwner().GetFullyQualifiedName()) 1023 } 1024 } else { 1025 fld = dm.GetMessageDescriptor().FindFieldByName(nm.GetNamePart()) 1026 if fld == nil { 1027 return nil, res.errs.handleErrorWithPos(node.start(), 1028 "%vfield %s of %s does not exist", 1029 mc, nm.GetNamePart(), dm.GetMessageDescriptor().GetFullyQualifiedName()) 1030 } 1031 } 1032 1033 path = append(pathPrefix, fld.GetNumber()) 1034 1035 if len(opt.GetName()) > nameIndex+1 { 1036 nextnm := opt.GetName()[nameIndex+1] 1037 nextnode := res.getOptionNamePartNode(nextnm) 1038 if fld.GetType() != dpb.FieldDescriptorProto_TYPE_MESSAGE { 1039 return nil, res.errs.handleErrorWithPos(nextnode.start(), 1040 "%vcannot set field %s because %s is not a message", 1041 mc, nextnm.GetNamePart(), nm.GetNamePart()) 1042 } 1043 if fld.IsRepeated() { 1044 return nil, res.errs.handleErrorWithPos(nextnode.start(), 1045 "%vcannot set field %s because %s is repeated (must use an aggregate)", 1046 mc, nextnm.GetNamePart(), nm.GetNamePart()) 1047 } 1048 var fdm *dynamic.Message 1049 var err error 1050 if dm.HasField(fld) { 1051 var v interface{} 1052 v, err = dm.TryGetField(fld) 1053 fdm, _ = v.(*dynamic.Message) 1054 } else { 1055 fdm = dynamic.NewMessage(fld.GetMessageType()) 1056 err = dm.TrySetField(fld, fdm) 1057 } 1058 if err != nil { 1059 return nil, res.errs.handleError(ErrorWithSourcePos{Pos: node.start(), Underlying: err}) 1060 } 1061 // recurse to set next part of name 1062 return interpretField(res, mc, element, fdm, opt, nameIndex+1, path) 1063 } 1064 1065 optNode := res.getOptionNode(opt) 1066 if err := setOptionField(res, mc, dm, fld, node, optNode.getValue()); err != nil { 1067 return nil, res.errs.handleError(err) 1068 } 1069 if fld.IsRepeated() { 1070 path = append(path, int32(dm.FieldLength(fld))-1) 1071 } 1072 return path, nil 1073 } 1074 1075 func findExtension(fd fileDescriptorish, name string, public bool, checked map[fileDescriptorish]struct{}) *desc.FieldDescriptor { 1076 if _, ok := checked[fd]; ok { 1077 return nil 1078 } 1079 checked[fd] = struct{}{} 1080 d := fd.FindSymbol(name) 1081 if d != nil { 1082 if fld, ok := d.(*desc.FieldDescriptor); ok { 1083 return fld 1084 } 1085 return nil 1086 } 1087 1088 // When public = false, we are searching only directly imported symbols. But we 1089 // also need to search transitive public imports due to semantics of public imports. 1090 if public { 1091 for _, dep := range fd.GetPublicDependencies() { 1092 d := findExtension(dep, name, true, checked) 1093 if d != nil { 1094 return d 1095 } 1096 } 1097 } else { 1098 for _, dep := range fd.GetDependencies() { 1099 d := findExtension(dep, name, true, checked) 1100 if d != nil { 1101 return d 1102 } 1103 } 1104 } 1105 return nil 1106 } 1107 1108 func setOptionField(res *parseResult, mc *messageContext, dm *dynamic.Message, fld *desc.FieldDescriptor, name node, val valueNode) error { 1109 v := val.value() 1110 if sl, ok := v.([]valueNode); ok { 1111 // handle slices a little differently than the others 1112 if !fld.IsRepeated() { 1113 return errorWithPos(val.start(), "%vvalue is an array but field is not repeated", mc) 1114 } 1115 origPath := mc.optAggPath 1116 defer func() { 1117 mc.optAggPath = origPath 1118 }() 1119 for index, item := range sl { 1120 mc.optAggPath = fmt.Sprintf("%s[%d]", origPath, index) 1121 if v, err := fieldValue(res, mc, richFldDescriptorish{FieldDescriptor: fld}, item, false); err != nil { 1122 return err 1123 } else if err = dm.TryAddRepeatedField(fld, v); err != nil { 1124 return errorWithPos(val.start(), "%verror setting value: %s", mc, err) 1125 } 1126 } 1127 return nil 1128 } 1129 1130 v, err := fieldValue(res, mc, richFldDescriptorish{FieldDescriptor: fld}, val, false) 1131 if err != nil { 1132 return err 1133 } 1134 if fld.IsRepeated() { 1135 err = dm.TryAddRepeatedField(fld, v) 1136 } else { 1137 if dm.HasField(fld) { 1138 return errorWithPos(name.start(), "%vnon-repeated option field %s already set", mc, fieldName(fld)) 1139 } 1140 err = dm.TrySetField(fld, v) 1141 } 1142 if err != nil { 1143 return errorWithPos(val.start(), "%verror setting value: %s", mc, err) 1144 } 1145 1146 return nil 1147 } 1148 1149 func findOption(res *parseResult, scope string, opts []*dpb.UninterpretedOption, name string) (int, error) { 1150 found := -1 1151 for i, opt := range opts { 1152 if len(opt.Name) != 1 { 1153 continue 1154 } 1155 if opt.Name[0].GetIsExtension() || opt.Name[0].GetNamePart() != name { 1156 continue 1157 } 1158 if found >= 0 { 1159 optNode := res.getOptionNode(opt) 1160 return -1, res.errs.handleErrorWithPos(optNode.getName().start(), "%s: option %s cannot be defined more than once", scope, name) 1161 } 1162 found = i 1163 } 1164 return found, nil 1165 } 1166 1167 func removeOption(uo []*dpb.UninterpretedOption, indexToRemove int) []*dpb.UninterpretedOption { 1168 if indexToRemove == 0 { 1169 return uo[1:] 1170 } else if int(indexToRemove) == len(uo)-1 { 1171 return uo[:len(uo)-1] 1172 } else { 1173 return append(uo[:indexToRemove], uo[indexToRemove+1:]...) 1174 } 1175 } 1176 1177 type messageContext struct { 1178 res *parseResult 1179 file fileDescriptorish 1180 elementType string 1181 elementName string 1182 option *dpb.UninterpretedOption 1183 optAggPath string 1184 } 1185 1186 func (c *messageContext) String() string { 1187 var ctx bytes.Buffer 1188 if c.elementType != "file" { 1189 _, _ = fmt.Fprintf(&ctx, "%s %s: ", c.elementType, c.elementName) 1190 } 1191 if c.option != nil && c.option.Name != nil { 1192 ctx.WriteString("option ") 1193 writeOptionName(&ctx, c.option.Name) 1194 if c.res.nodes == nil { 1195 // if we have no source position info, try to provide as much context 1196 // as possible (if nodes != nil, we don't need this because any errors 1197 // will actually have file and line numbers) 1198 if c.optAggPath != "" { 1199 _, _ = fmt.Fprintf(&ctx, " at %s", c.optAggPath) 1200 } 1201 } 1202 ctx.WriteString(": ") 1203 } 1204 return ctx.String() 1205 } 1206 1207 func writeOptionName(buf *bytes.Buffer, parts []*dpb.UninterpretedOption_NamePart) { 1208 first := true 1209 for _, p := range parts { 1210 if first { 1211 first = false 1212 } else { 1213 buf.WriteByte('.') 1214 } 1215 nm := p.GetNamePart() 1216 if nm[0] == '.' { 1217 // skip leading dot 1218 nm = nm[1:] 1219 } 1220 if p.GetIsExtension() { 1221 buf.WriteByte('(') 1222 buf.WriteString(nm) 1223 buf.WriteByte(')') 1224 } else { 1225 buf.WriteString(nm) 1226 } 1227 } 1228 } 1229 1230 func fieldName(fld *desc.FieldDescriptor) string { 1231 if fld.IsExtension() { 1232 return fld.GetFullyQualifiedName() 1233 } else { 1234 return fld.GetName() 1235 } 1236 } 1237 1238 func valueKind(val interface{}) string { 1239 switch val := val.(type) { 1240 case identifier: 1241 return "identifier" 1242 case bool: 1243 return "bool" 1244 case int64: 1245 if val < 0 { 1246 return "negative integer" 1247 } 1248 return "integer" 1249 case uint64: 1250 return "integer" 1251 case float64: 1252 return "double" 1253 case string, []byte: 1254 return "string" 1255 case []*aggregateEntryNode: 1256 return "message" 1257 default: 1258 return fmt.Sprintf("%T", val) 1259 } 1260 } 1261 1262 func fieldValue(res *parseResult, mc *messageContext, fld fldDescriptorish, val valueNode, enumAsString bool) (interface{}, error) { 1263 v := val.value() 1264 t := fld.AsFieldDescriptorProto().GetType() 1265 switch t { 1266 case dpb.FieldDescriptorProto_TYPE_ENUM: 1267 if id, ok := v.(identifier); ok { 1268 ev := fld.GetEnumType().FindValueByName(string(id)) 1269 if ev == nil { 1270 return nil, errorWithPos(val.start(), "%venum %s has no value named %s", mc, fld.GetEnumType().GetFullyQualifiedName(), id) 1271 } 1272 if enumAsString { 1273 return ev.GetName(), nil 1274 } else { 1275 return ev.GetNumber(), nil 1276 } 1277 } 1278 return nil, errorWithPos(val.start(), "%vexpecting enum, got %s", mc, valueKind(v)) 1279 case dpb.FieldDescriptorProto_TYPE_MESSAGE, dpb.FieldDescriptorProto_TYPE_GROUP: 1280 if aggs, ok := v.([]*aggregateEntryNode); ok { 1281 fmd := fld.GetMessageType() 1282 fdm := dynamic.NewMessage(fmd) 1283 origPath := mc.optAggPath 1284 defer func() { 1285 mc.optAggPath = origPath 1286 }() 1287 for _, a := range aggs { 1288 if origPath == "" { 1289 mc.optAggPath = a.name.value() 1290 } else { 1291 mc.optAggPath = origPath + "." + a.name.value() 1292 } 1293 var ffld *desc.FieldDescriptor 1294 if a.name.isExtension { 1295 n := a.name.name.val 1296 ffld = findExtension(mc.file, n, false, map[fileDescriptorish]struct{}{}) 1297 if ffld == nil { 1298 // may need to qualify with package name 1299 pkg := mc.file.GetPackage() 1300 if pkg != "" { 1301 ffld = findExtension(mc.file, pkg+"."+n, false, map[fileDescriptorish]struct{}{}) 1302 } 1303 } 1304 } else { 1305 ffld = fmd.FindFieldByName(a.name.value()) 1306 } 1307 if ffld == nil { 1308 return nil, errorWithPos(val.start(), "%vfield %s not found", mc, a.name.name.val) 1309 } 1310 if err := setOptionField(res, mc, fdm, ffld, a.name, a.val); err != nil { 1311 return nil, err 1312 } 1313 } 1314 return fdm, nil 1315 } 1316 return nil, errorWithPos(val.start(), "%vexpecting message, got %s", mc, valueKind(v)) 1317 case dpb.FieldDescriptorProto_TYPE_BOOL: 1318 if b, ok := v.(bool); ok { 1319 return b, nil 1320 } 1321 return nil, errorWithPos(val.start(), "%vexpecting bool, got %s", mc, valueKind(v)) 1322 case dpb.FieldDescriptorProto_TYPE_BYTES: 1323 if str, ok := v.(string); ok { 1324 return []byte(str), nil 1325 } 1326 return nil, errorWithPos(val.start(), "%vexpecting bytes, got %s", mc, valueKind(v)) 1327 case dpb.FieldDescriptorProto_TYPE_STRING: 1328 if str, ok := v.(string); ok { 1329 return str, nil 1330 } 1331 return nil, errorWithPos(val.start(), "%vexpecting string, got %s", mc, valueKind(v)) 1332 case dpb.FieldDescriptorProto_TYPE_INT32, dpb.FieldDescriptorProto_TYPE_SINT32, dpb.FieldDescriptorProto_TYPE_SFIXED32: 1333 if i, ok := v.(int64); ok { 1334 if i > math.MaxInt32 || i < math.MinInt32 { 1335 return nil, errorWithPos(val.start(), "%vvalue %d is out of range for int32", mc, i) 1336 } 1337 return int32(i), nil 1338 } 1339 if ui, ok := v.(uint64); ok { 1340 if ui > math.MaxInt32 { 1341 return nil, errorWithPos(val.start(), "%vvalue %d is out of range for int32", mc, ui) 1342 } 1343 return int32(ui), nil 1344 } 1345 return nil, errorWithPos(val.start(), "%vexpecting int32, got %s", mc, valueKind(v)) 1346 case dpb.FieldDescriptorProto_TYPE_UINT32, dpb.FieldDescriptorProto_TYPE_FIXED32: 1347 if i, ok := v.(int64); ok { 1348 if i > math.MaxUint32 || i < 0 { 1349 return nil, errorWithPos(val.start(), "%vvalue %d is out of range for uint32", mc, i) 1350 } 1351 return uint32(i), nil 1352 } 1353 if ui, ok := v.(uint64); ok { 1354 if ui > math.MaxUint32 { 1355 return nil, errorWithPos(val.start(), "%vvalue %d is out of range for uint32", mc, ui) 1356 } 1357 return uint32(ui), nil 1358 } 1359 return nil, errorWithPos(val.start(), "%vexpecting uint32, got %s", mc, valueKind(v)) 1360 case dpb.FieldDescriptorProto_TYPE_INT64, dpb.FieldDescriptorProto_TYPE_SINT64, dpb.FieldDescriptorProto_TYPE_SFIXED64: 1361 if i, ok := v.(int64); ok { 1362 return i, nil 1363 } 1364 if ui, ok := v.(uint64); ok { 1365 if ui > math.MaxInt64 { 1366 return nil, errorWithPos(val.start(), "%vvalue %d is out of range for int64", mc, ui) 1367 } 1368 return int64(ui), nil 1369 } 1370 return nil, errorWithPos(val.start(), "%vexpecting int64, got %s", mc, valueKind(v)) 1371 case dpb.FieldDescriptorProto_TYPE_UINT64, dpb.FieldDescriptorProto_TYPE_FIXED64: 1372 if i, ok := v.(int64); ok { 1373 if i < 0 { 1374 return nil, errorWithPos(val.start(), "%vvalue %d is out of range for uint64", mc, i) 1375 } 1376 return uint64(i), nil 1377 } 1378 if ui, ok := v.(uint64); ok { 1379 return ui, nil 1380 } 1381 return nil, errorWithPos(val.start(), "%vexpecting uint64, got %s", mc, valueKind(v)) 1382 case dpb.FieldDescriptorProto_TYPE_DOUBLE: 1383 if d, ok := v.(float64); ok { 1384 return d, nil 1385 } 1386 if i, ok := v.(int64); ok { 1387 return float64(i), nil 1388 } 1389 if u, ok := v.(uint64); ok { 1390 return float64(u), nil 1391 } 1392 return nil, errorWithPos(val.start(), "%vexpecting double, got %s", mc, valueKind(v)) 1393 case dpb.FieldDescriptorProto_TYPE_FLOAT: 1394 if d, ok := v.(float64); ok { 1395 if (d > math.MaxFloat32 || d < -math.MaxFloat32) && !math.IsInf(d, 1) && !math.IsInf(d, -1) && !math.IsNaN(d) { 1396 return nil, errorWithPos(val.start(), "%vvalue %f is out of range for float", mc, d) 1397 } 1398 return float32(d), nil 1399 } 1400 if i, ok := v.(int64); ok { 1401 return float32(i), nil 1402 } 1403 if u, ok := v.(uint64); ok { 1404 return float32(u), nil 1405 } 1406 return nil, errorWithPos(val.start(), "%vexpecting float, got %s", mc, valueKind(v)) 1407 default: 1408 return nil, errorWithPos(val.start(), "%vunrecognized field type: %s", mc, t) 1409 } 1410 }