github.com/junaozun/protoc-gen-go-clone@v0.0.0-20231229025521-89199567f720/generator/generator.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package generator is deprecated. 6 // 7 // This package is excluded from the Go protocol buffer compatibility guarantee 8 // and may be deleted at some point in the future. 9 // 10 // Deprecated: Use the "google.golang.org/protobuf/compiler/protogen" package 11 // instead to write protoc plugins in Go. 12 package generator 13 14 import ( 15 "bufio" 16 "bytes" 17 "compress/gzip" 18 "crypto/sha256" 19 "encoding/hex" 20 "fmt" 21 "go/ast" 22 "go/build" 23 "go/parser" 24 "go/printer" 25 "go/token" 26 "log" 27 "os" 28 "path" 29 "sort" 30 "strconv" 31 "strings" 32 "unicode" 33 "unicode/utf8" 34 35 "github.com/golang/protobuf/proto" 36 "github.com/golang/protobuf/protoc-gen-go/descriptor" 37 plugin "github.com/golang/protobuf/protoc-gen-go/plugin" 38 ) 39 40 func init() { 41 /* 42 fmt.Fprint(os.Stderr, 43 "WARNING: Package \"github.com/golang/protobuf/protoc-gen-go/generator\" is deprecated.\n"+ 44 "\tA future release of golang/protobuf will delete this package,\n"+ 45 "\twhich has long been excluded from the compatibility promise.\n\n") 46 47 */ 48 } 49 50 // generatedCodeVersion indicates a version of the generated code. 51 // It is incremented whenever an incompatibility between the generated code and 52 // proto package is introduced; the generated code references 53 // a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion). 54 const generatedCodeVersion = 3 55 56 // A Plugin provides functionality to add to the output during Go code generation, 57 // such as to produce RPC stubs. 58 type Plugin interface { 59 // Name identifies the plugin. 60 Name() string 61 // Init is called once after data structures are built but before 62 // code generation begins. 63 Init(g *Generator) 64 // Generate produces the code generated by the plugin for this file, 65 // except for the imports, by calling the generator's methods P, In, and Out. 66 Generate(file *FileDescriptor) 67 // GenerateImports produces the import declarations for this file. 68 // It is called after Generate. 69 GenerateImports(file *FileDescriptor) 70 } 71 72 var plugins []Plugin 73 74 // RegisterPlugin installs a (second-order) plugin to be run when the Go output is generated. 75 // It is typically called during initialization. 76 func RegisterPlugin(p Plugin) { 77 plugins = append(plugins, p) 78 } 79 80 // A GoImportPath is the import path of a Go package. e.g., "google.golang.org/genproto/protobuf". 81 type GoImportPath string 82 83 func (p GoImportPath) String() string { return strconv.Quote(string(p)) } 84 85 // A GoPackageName is the name of a Go package. e.g., "protobuf". 86 type GoPackageName string 87 88 // Each type we import as a protocol buffer (other than FileDescriptorProto) needs 89 // a pointer to the FileDescriptorProto that represents it. These types achieve that 90 // wrapping by placing each Proto inside a struct with the pointer to its File. The 91 // structs have the same names as their contents, with "Proto" removed. 92 // FileDescriptor is used to store the things that it points to. 93 94 // The file and package name method are common to messages and enums. 95 type common struct { 96 file *FileDescriptor // File this object comes from. 97 } 98 99 // GoImportPath is the import path of the Go package containing the type. 100 func (c *common) GoImportPath() GoImportPath { 101 return c.file.importPath 102 } 103 104 func (c *common) File() *FileDescriptor { return c.file } 105 106 func fileIsProto3(file *descriptor.FileDescriptorProto) bool { 107 return file.GetSyntax() == "proto3" 108 } 109 110 func (c *common) proto3() bool { return fileIsProto3(c.file.FileDescriptorProto) } 111 112 // Descriptor represents a protocol buffer message. 113 type Descriptor struct { 114 common 115 *descriptor.DescriptorProto 116 parent *Descriptor // The containing message, if any. 117 nested []*Descriptor // Inner messages, if any. 118 enums []*EnumDescriptor // Inner enums, if any. 119 ext []*ExtensionDescriptor // Extensions, if any. 120 typename []string // Cached typename vector. 121 index int // The index into the container, whether the file or another message. 122 path string // The SourceCodeInfo path as comma-separated integers. 123 group bool 124 } 125 126 // TypeName returns the elements of the dotted type name. 127 // The package name is not part of this name. 128 func (d *Descriptor) TypeName() []string { 129 if d.typename != nil { 130 return d.typename 131 } 132 n := 0 133 for parent := d; parent != nil; parent = parent.parent { 134 n++ 135 } 136 s := make([]string, n) 137 for parent := d; parent != nil; parent = parent.parent { 138 n-- 139 s[n] = parent.GetName() 140 } 141 d.typename = s 142 return s 143 } 144 145 // EnumDescriptor describes an enum. If it's at top level, its parent will be nil. 146 // Otherwise it will be the descriptor of the message in which it is defined. 147 type EnumDescriptor struct { 148 common 149 *descriptor.EnumDescriptorProto 150 parent *Descriptor // The containing message, if any. 151 typename []string // Cached typename vector. 152 index int // The index into the container, whether the file or a message. 153 path string // The SourceCodeInfo path as comma-separated integers. 154 } 155 156 // TypeName returns the elements of the dotted type name. 157 // The package name is not part of this name. 158 func (e *EnumDescriptor) TypeName() (s []string) { 159 if e.typename != nil { 160 return e.typename 161 } 162 name := e.GetName() 163 if e.parent == nil { 164 s = make([]string, 1) 165 } else { 166 pname := e.parent.TypeName() 167 s = make([]string, len(pname)+1) 168 copy(s, pname) 169 } 170 s[len(s)-1] = name 171 e.typename = s 172 return s 173 } 174 175 // Everything but the last element of the full type name, CamelCased. 176 // The values of type Foo.Bar are call Foo_value1... not Foo_Bar_value1... . 177 func (e *EnumDescriptor) prefix() string { 178 if e.parent == nil { 179 // If the enum is not part of a message, the prefix is just the type name. 180 return CamelCase(*e.Name) + "_" 181 } 182 typeName := e.TypeName() 183 return CamelCaseSlice(typeName[0:len(typeName)-1]) + "_" 184 } 185 186 // The integer value of the named constant in this enumerated type. 187 func (e *EnumDescriptor) integerValueAsString(name string) string { 188 for _, c := range e.Value { 189 if c.GetName() == name { 190 return fmt.Sprint(c.GetNumber()) 191 } 192 } 193 log.Fatal("cannot find value for enum constant") 194 return "" 195 } 196 197 // ExtensionDescriptor describes an extension. If it's at top level, its parent will be nil. 198 // Otherwise it will be the descriptor of the message in which it is defined. 199 type ExtensionDescriptor struct { 200 common 201 *descriptor.FieldDescriptorProto 202 parent *Descriptor // The containing message, if any. 203 } 204 205 // TypeName returns the elements of the dotted type name. 206 // The package name is not part of this name. 207 func (e *ExtensionDescriptor) TypeName() (s []string) { 208 name := e.GetName() 209 if e.parent == nil { 210 // top-level extension 211 s = make([]string, 1) 212 } else { 213 pname := e.parent.TypeName() 214 s = make([]string, len(pname)+1) 215 copy(s, pname) 216 } 217 s[len(s)-1] = name 218 return s 219 } 220 221 // DescName returns the variable name used for the generated descriptor. 222 func (e *ExtensionDescriptor) DescName() string { 223 // The full type name. 224 typeName := e.TypeName() 225 // Each scope of the extension is individually CamelCased, and all are joined with "_" with an "E_" prefix. 226 for i, s := range typeName { 227 typeName[i] = CamelCase(s) 228 } 229 return "E_" + strings.Join(typeName, "_") 230 } 231 232 // ImportedDescriptor describes a type that has been publicly imported from another file. 233 type ImportedDescriptor struct { 234 common 235 o Object 236 } 237 238 func (id *ImportedDescriptor) TypeName() []string { return id.o.TypeName() } 239 240 // FileDescriptor describes an protocol buffer descriptor file (.proto). 241 // It includes slices of all the messages and enums defined within it. 242 // Those slices are constructed by WrapTypes. 243 type FileDescriptor struct { 244 *descriptor.FileDescriptorProto 245 desc []*Descriptor // All the messages defined in this file. 246 enum []*EnumDescriptor // All the enums defined in this file. 247 ext []*ExtensionDescriptor // All the top-level extensions defined in this file. 248 imp []*ImportedDescriptor // All types defined in files publicly imported by this file. 249 250 // Comments, stored as a map of path (comma-separated integers) to the comment. 251 comments map[string]*descriptor.SourceCodeInfo_Location 252 253 // The full list of symbols that are exported, 254 // as a map from the exported object to its symbols. 255 // This is used for supporting public imports. 256 exported map[Object][]symbol 257 258 importPath GoImportPath // Import path of this file's package. 259 packageName GoPackageName // Name of this file's Go package. 260 261 proto3 bool // whether to generate proto3 code for this file 262 } 263 264 // VarName is the variable name we'll use in the generated code to refer 265 // to the compressed bytes of this descriptor. It is not exported, so 266 // it is only valid inside the generated package. 267 func (d *FileDescriptor) VarName() string { 268 h := sha256.Sum256([]byte(d.GetName())) 269 return fmt.Sprintf("fileDescriptor_%s", hex.EncodeToString(h[:8])) 270 } 271 272 // goPackageOption interprets the file's go_package option. 273 // If there is no go_package, it returns ("", "", false). 274 // If there's a simple name, it returns ("", pkg, true). 275 // If the option implies an import path, it returns (impPath, pkg, true). 276 func (d *FileDescriptor) goPackageOption() (impPath GoImportPath, pkg GoPackageName, ok bool) { 277 opt := d.GetOptions().GetGoPackage() 278 if opt == "" { 279 return "", "", false 280 } 281 // A semicolon-delimited suffix delimits the import path and package name. 282 sc := strings.Index(opt, ";") 283 if sc >= 0 { 284 return GoImportPath(opt[:sc]), cleanPackageName(opt[sc+1:]), true 285 } 286 // The presence of a slash implies there's an import path. 287 slash := strings.LastIndex(opt, "/") 288 if slash >= 0 { 289 return GoImportPath(opt), cleanPackageName(opt[slash+1:]), true 290 } 291 return "", cleanPackageName(opt), true 292 } 293 294 // goFileName returns the output name for the generated Go file. 295 func (d *FileDescriptor) goFileName(pathType pathType) string { 296 name := *d.Name 297 if ext := path.Ext(name); ext == ".proto" || ext == ".protodevel" { 298 name = name[:len(name)-len(ext)] 299 } 300 name += fmt.Sprintf(".pb.%s.go", toolName) 301 302 if pathType == pathTypeSourceRelative { 303 return name 304 } 305 306 // Does the file have a "go_package" option? 307 // If it does, it may override the filename. 308 if impPath, _, ok := d.goPackageOption(); ok && impPath != "" { 309 // Replace the existing dirname with the declared import path. 310 _, name = path.Split(name) 311 name = path.Join(string(impPath), name) 312 return name 313 } 314 315 return name 316 } 317 318 func (d *FileDescriptor) addExport(obj Object, sym symbol) { 319 d.exported[obj] = append(d.exported[obj], sym) 320 } 321 322 // symbol is an interface representing an exported Go symbol. 323 type symbol interface { 324 // GenerateAlias should generate an appropriate alias 325 // for the symbol from the named package. 326 GenerateAlias(g *Generator, filename string, pkg GoPackageName) 327 } 328 329 type messageSymbol struct { 330 sym string 331 hasExtensions, isMessageSet bool 332 oneofTypes []string 333 } 334 335 type getterSymbol struct { 336 name string 337 typ string 338 typeName string // canonical name in proto world; empty for proto.Message and similar 339 genType bool // whether typ contains a generated type (message/group/enum) 340 } 341 342 func (ms *messageSymbol) GenerateAlias(g *Generator, filename string, pkg GoPackageName) { 343 g.P("// ", ms.sym, " from public import ", filename) 344 g.P("type ", ms.sym, " = ", pkg, ".", ms.sym) 345 for _, name := range ms.oneofTypes { 346 g.P("type ", name, " = ", pkg, ".", name) 347 } 348 } 349 350 type enumSymbol struct { 351 name string 352 proto3 bool // Whether this came from a proto3 file. 353 } 354 355 func (es enumSymbol) GenerateAlias(g *Generator, filename string, pkg GoPackageName) { 356 s := es.name 357 g.P("// ", s, " from public import ", filename) 358 g.P("type ", s, " = ", pkg, ".", s) 359 g.P("var ", s, "_name = ", pkg, ".", s, "_name") 360 g.P("var ", s, "_value = ", pkg, ".", s, "_value") 361 } 362 363 type constOrVarSymbol struct { 364 sym string 365 typ string // either "const" or "var" 366 cast string // if non-empty, a type cast is required (used for enums) 367 } 368 369 func (cs constOrVarSymbol) GenerateAlias(g *Generator, filename string, pkg GoPackageName) { 370 v := string(pkg) + "." + cs.sym 371 if cs.cast != "" { 372 v = cs.cast + "(" + v + ")" 373 } 374 g.P(cs.typ, " ", cs.sym, " = ", v) 375 } 376 377 // Object is an interface abstracting the abilities shared by enums, messages, extensions and imported objects. 378 type Object interface { 379 GoImportPath() GoImportPath 380 TypeName() []string 381 File() *FileDescriptor 382 } 383 384 // Generator is the type whose methods generate the output, stored in the associated response structure. 385 type Generator struct { 386 *bytes.Buffer 387 388 Request *plugin.CodeGeneratorRequest // The input. 389 Response *plugin.CodeGeneratorResponse // The output. 390 391 Param map[string]string // Command-line parameters. 392 PackageImportPath string // Go import path of the package we're generating code for 393 ImportPrefix string // String to prefix to imported package file names. 394 ImportMap map[string]string // Mapping from .proto file name to import path 395 396 Pkg map[string]string // The names under which we import support packages 397 398 outputImportPath GoImportPath // Package we're generating code for. 399 allFiles []*FileDescriptor // All files in the tree 400 allFilesByName map[string]*FileDescriptor // All files by filename. 401 genFiles []*FileDescriptor // Those files we will generate output for. 402 file *FileDescriptor // The file we are compiling now. 403 packageNames map[GoImportPath]GoPackageName // Imported package names in the current file. 404 usedPackages map[GoImportPath]bool // Packages used in current file. 405 usedPackageNames map[GoPackageName]bool // Package names used in the current file. 406 addedImports map[GoImportPath]bool // Additional imports to emit. 407 typeNameToObject map[string]Object // Key is a fully-qualified name in input syntax. 408 init []string // Lines to emit in the init function. 409 indent string 410 pathType pathType // How to generate output filenames. 411 writeOutput bool 412 annotateCode bool // whether to store annotations 413 annotations []*descriptor.GeneratedCodeInfo_Annotation // annotations to store 414 } 415 416 type pathType int 417 418 const ( 419 pathTypeImport pathType = iota 420 pathTypeSourceRelative 421 ) 422 423 // New creates a new generator and allocates the request and response protobufs. 424 func New() *Generator { 425 g := new(Generator) 426 g.Buffer = new(bytes.Buffer) 427 g.Request = new(plugin.CodeGeneratorRequest) 428 g.Response = new(plugin.CodeGeneratorResponse) 429 return g 430 } 431 432 // Error reports a problem, including an error, and exits the program. 433 func (g *Generator) Error(err error, msgs ...string) { 434 s := strings.Join(msgs, " ") + ":" + err.Error() 435 log.Print(g.toolName(), ": error:", s) 436 os.Exit(1) 437 } 438 439 // Fail reports a problem and exits the program. 440 func (g *Generator) Fail(msgs ...string) { 441 s := strings.Join(msgs, " ") 442 log.Print(g.toolName(), ": error:", s) 443 os.Exit(1) 444 } 445 446 // CommandLineParameters breaks the comma-separated list of key=value pairs 447 // in the parameter (a member of the request protobuf) into a key/value map. 448 // It then sets file name mappings defined by those entries. 449 func (g *Generator) CommandLineParameters(parameter string) { 450 allPlugins := plugins 451 g.Param = make(map[string]string) 452 for _, p := range strings.Split(parameter, ",") { 453 if i := strings.Index(p, "="); i < 0 { 454 g.Param[p] = "" 455 } else { 456 g.Param[p[0:i]] = p[i+1:] 457 } 458 } 459 460 g.ImportMap = make(map[string]string) 461 pluginList := "none" // Default list of plugin names to enable (empty means all). 462 for k, v := range g.Param { 463 switch k { 464 case "import_prefix": 465 g.ImportPrefix = v 466 case "import_path": 467 g.PackageImportPath = v 468 case "paths": 469 switch v { 470 case "import": 471 g.pathType = pathTypeImport 472 case "source_relative": 473 g.pathType = pathTypeSourceRelative 474 default: 475 g.Fail(fmt.Sprintf(`Unknown path type %q: want "import" or "source_relative".`, v)) 476 } 477 case "plugins": 478 pluginList = v 479 case "annotate_code": 480 if v == "true" { 481 g.annotateCode = true 482 } 483 default: 484 if len(k) > 0 && k[0] == 'M' { 485 g.ImportMap[k[1:]] = v 486 } 487 } 488 } 489 if pluginList != "" { 490 // Amend the set of plugins. 491 enabled := make(map[string]bool) 492 for _, name := range strings.Split(pluginList, "+") { 493 enabled[name] = true 494 } 495 var nplugins []Plugin 496 for _, p := range plugins { 497 if enabled[p.Name()] { 498 nplugins = append(nplugins, p) 499 } 500 } 501 plugins = nplugins 502 } 503 if len(plugins) == 0 { 504 plugins = allPlugins 505 } 506 } 507 508 // DefaultPackageName returns the package name printed for the object. 509 // If its file is in a different package, it returns the package name we're using for this file, plus ".". 510 // Otherwise it returns the empty string. 511 func (g *Generator) DefaultPackageName(obj Object) string { 512 importPath := obj.GoImportPath() 513 if importPath == g.outputImportPath { 514 return "" 515 } 516 return string(g.GoPackageName(importPath)) + "." 517 } 518 519 // GoPackageName returns the name used for a package. 520 func (g *Generator) GoPackageName(importPath GoImportPath) GoPackageName { 521 if name, ok := g.packageNames[importPath]; ok { 522 return name 523 } 524 name := cleanPackageName(baseName(string(importPath))) 525 for i, orig := 1, name; g.usedPackageNames[name] || isGoPredeclaredIdentifier[string(name)]; i++ { 526 name = orig + GoPackageName(strconv.Itoa(i)) 527 } 528 g.packageNames[importPath] = name 529 g.usedPackageNames[name] = true 530 return name 531 } 532 533 // AddImport adds a package to the generated file's import section. 534 // It returns the name used for the package. 535 func (g *Generator) AddImport(importPath GoImportPath) GoPackageName { 536 g.addedImports[importPath] = true 537 return g.GoPackageName(importPath) 538 } 539 540 // GetImport returns the package with the given import path. 541 func (g *Generator) GetImport() map[GoImportPath]bool { 542 return g.addedImports 543 } 544 545 var globalPackageNames = map[GoPackageName]bool{ 546 "fmt": true, 547 "math": true, 548 "proto": true, 549 } 550 551 // Create and remember a guaranteed unique package name. Pkg is the candidate name. 552 // The FileDescriptor parameter is unused. 553 func RegisterUniquePackageName(pkg string, f *FileDescriptor) string { 554 name := cleanPackageName(pkg) 555 for i, orig := 1, name; globalPackageNames[name]; i++ { 556 name = orig + GoPackageName(strconv.Itoa(i)) 557 } 558 globalPackageNames[name] = true 559 return string(name) 560 } 561 562 var isGoKeyword = map[string]bool{ 563 "break": true, 564 "case": true, 565 "chan": true, 566 "const": true, 567 "continue": true, 568 "default": true, 569 "else": true, 570 "defer": true, 571 "fallthrough": true, 572 "for": true, 573 "func": true, 574 "go": true, 575 "goto": true, 576 "if": true, 577 "import": true, 578 "interface": true, 579 "map": true, 580 "package": true, 581 "range": true, 582 "return": true, 583 "select": true, 584 "struct": true, 585 "switch": true, 586 "type": true, 587 "var": true, 588 } 589 590 var isGoPredeclaredIdentifier = map[string]bool{ 591 "append": true, 592 "bool": true, 593 "byte": true, 594 "cap": true, 595 "close": true, 596 "complex": true, 597 "complex128": true, 598 "complex64": true, 599 "copy": true, 600 "delete": true, 601 "error": true, 602 "false": true, 603 "float32": true, 604 "float64": true, 605 "imag": true, 606 "int": true, 607 "int16": true, 608 "int32": true, 609 "int64": true, 610 "int8": true, 611 "iota": true, 612 "len": true, 613 "make": true, 614 "new": true, 615 "nil": true, 616 "panic": true, 617 "print": true, 618 "println": true, 619 "real": true, 620 "recover": true, 621 "rune": true, 622 "string": true, 623 "true": true, 624 "uint": true, 625 "uint16": true, 626 "uint32": true, 627 "uint64": true, 628 "uint8": true, 629 "uintptr": true, 630 } 631 632 func cleanPackageName(name string) GoPackageName { 633 name = strings.Map(badToUnderscore, name) 634 // Identifier must not be keyword or predeclared identifier: insert _. 635 if isGoKeyword[name] { 636 name = "_" + name 637 } 638 // Identifier must not begin with digit: insert _. 639 if r, _ := utf8.DecodeRuneInString(name); unicode.IsDigit(r) { 640 name = "_" + name 641 } 642 return GoPackageName(name) 643 } 644 645 // defaultGoPackage returns the package name to use, 646 // derived from the import path of the package we're building code for. 647 func (g *Generator) defaultGoPackage() GoPackageName { 648 p := g.PackageImportPath 649 if i := strings.LastIndex(p, "/"); i >= 0 { 650 p = p[i+1:] 651 } 652 return cleanPackageName(p) 653 } 654 655 // SetPackageNames sets the package name for this run. 656 // The package name must agree across all files being generated. 657 // It also defines unique package names for all imported files. 658 func (g *Generator) SetPackageNames() { 659 g.outputImportPath = g.genFiles[0].importPath 660 661 defaultPackageNames := make(map[GoImportPath]GoPackageName) 662 for _, f := range g.genFiles { 663 if _, p, ok := f.goPackageOption(); ok { 664 defaultPackageNames[f.importPath] = p 665 } 666 } 667 for _, f := range g.genFiles { 668 if _, p, ok := f.goPackageOption(); ok { 669 // Source file: option go_package = "quux/bar"; 670 f.packageName = p 671 } else if p, ok := defaultPackageNames[f.importPath]; ok { 672 // A go_package option in another file in the same package. 673 // 674 // This is a poor choice in general, since every source file should 675 // contain a go_package option. Supported mainly for historical 676 // compatibility. 677 f.packageName = p 678 } else if p := g.defaultGoPackage(); p != "" { 679 // Command-line: import_path=quux/bar. 680 // 681 // The import_path flag sets a package name for files which don't 682 // contain a go_package option. 683 f.packageName = p 684 } else if p := f.GetPackage(); p != "" { 685 // Source file: package quux.bar; 686 f.packageName = cleanPackageName(p) 687 } else { 688 // Source filename. 689 f.packageName = cleanPackageName(baseName(f.GetName())) 690 } 691 } 692 693 // Check that all files have a consistent package name and import path. 694 for _, f := range g.genFiles[1:] { 695 if a, b := g.genFiles[0].importPath, f.importPath; a != b { 696 g.Fail(fmt.Sprintf("inconsistent package import paths: %v, %v", a, b)) 697 } 698 if a, b := g.genFiles[0].packageName, f.packageName; a != b { 699 g.Fail(fmt.Sprintf("inconsistent package names: %v, %v", a, b)) 700 } 701 } 702 703 // Names of support packages. These never vary (if there are conflicts, 704 // we rename the conflicting package), so this could be removed someday. 705 g.Pkg = map[string]string{ 706 "fmt": "fmt", 707 "math": "math", 708 "proto": "proto", 709 } 710 } 711 712 // WrapTypes walks the incoming data, wrapping DescriptorProtos, EnumDescriptorProtos 713 // and FileDescriptorProtos into file-referenced objects within the Generator. 714 // It also creates the list of files to generate and so should be called before GenerateAllFiles. 715 func (g *Generator) WrapTypes() { 716 g.allFiles = make([]*FileDescriptor, 0, len(g.Request.ProtoFile)) 717 g.allFilesByName = make(map[string]*FileDescriptor, len(g.allFiles)) 718 genFileNames := make(map[string]bool) 719 for _, n := range g.Request.FileToGenerate { 720 genFileNames[n] = true 721 } 722 for _, f := range g.Request.ProtoFile { 723 fd := &FileDescriptor{ 724 FileDescriptorProto: f, 725 exported: make(map[Object][]symbol), 726 proto3: fileIsProto3(f), 727 } 728 // The import path may be set in a number of ways. 729 if substitution, ok := g.ImportMap[f.GetName()]; ok { 730 // Command-line: M=foo.proto=quux/bar. 731 // 732 // Explicit mapping of source file to import path. 733 fd.importPath = GoImportPath(substitution) 734 } else if genFileNames[f.GetName()] && g.PackageImportPath != "" { 735 // Command-line: import_path=quux/bar. 736 // 737 // The import_path flag sets the import path for every file that 738 // we generate code for. 739 fd.importPath = GoImportPath(g.PackageImportPath) 740 } else if p, _, _ := fd.goPackageOption(); p != "" { 741 // Source file: option go_package = "quux/bar"; 742 // 743 // The go_package option sets the import path. Most users should use this. 744 fd.importPath = p 745 } else { 746 // Source filename. 747 // 748 // Last resort when nothing else is available. 749 fd.importPath = GoImportPath(path.Dir(f.GetName())) 750 } 751 // We must wrap the descriptors before we wrap the enums 752 fd.desc = wrapDescriptors(fd) 753 g.buildNestedDescriptors(fd.desc) 754 fd.enum = wrapEnumDescriptors(fd, fd.desc) 755 g.buildNestedEnums(fd.desc, fd.enum) 756 fd.ext = wrapExtensions(fd) 757 extractComments(fd) 758 g.allFiles = append(g.allFiles, fd) 759 g.allFilesByName[f.GetName()] = fd 760 } 761 for _, fd := range g.allFiles { 762 fd.imp = wrapImported(fd, g) 763 } 764 765 g.genFiles = make([]*FileDescriptor, 0, len(g.Request.FileToGenerate)) 766 for _, fileName := range g.Request.FileToGenerate { 767 fd := g.allFilesByName[fileName] 768 if fd == nil { 769 g.Fail("could not find file named", fileName) 770 } 771 g.genFiles = append(g.genFiles, fd) 772 } 773 } 774 775 // Scan the descriptors in this file. For each one, build the slice of nested descriptors 776 func (g *Generator) buildNestedDescriptors(descs []*Descriptor) { 777 for _, desc := range descs { 778 if len(desc.NestedType) != 0 { 779 for _, nest := range descs { 780 if nest.parent == desc { 781 desc.nested = append(desc.nested, nest) 782 } 783 } 784 if len(desc.nested) != len(desc.NestedType) { 785 g.Fail("internal error: nesting failure for", desc.GetName()) 786 } 787 } 788 } 789 } 790 791 func (g *Generator) buildNestedEnums(descs []*Descriptor, enums []*EnumDescriptor) { 792 for _, desc := range descs { 793 if len(desc.EnumType) != 0 { 794 for _, enum := range enums { 795 if enum.parent == desc { 796 desc.enums = append(desc.enums, enum) 797 } 798 } 799 if len(desc.enums) != len(desc.EnumType) { 800 g.Fail("internal error: enum nesting failure for", desc.GetName()) 801 } 802 } 803 } 804 } 805 806 // Construct the Descriptor 807 func newDescriptor(desc *descriptor.DescriptorProto, parent *Descriptor, file *FileDescriptor, index int) *Descriptor { 808 d := &Descriptor{ 809 common: common{file}, 810 DescriptorProto: desc, 811 parent: parent, 812 index: index, 813 } 814 if parent == nil { 815 d.path = fmt.Sprintf("%d,%d", messagePath, index) 816 } else { 817 d.path = fmt.Sprintf("%s,%d,%d", parent.path, messageMessagePath, index) 818 } 819 820 // The only way to distinguish a group from a message is whether 821 // the containing message has a TYPE_GROUP field that matches. 822 if parent != nil { 823 parts := d.TypeName() 824 if file.Package != nil { 825 parts = append([]string{*file.Package}, parts...) 826 } 827 exp := "." + strings.Join(parts, ".") 828 for _, field := range parent.Field { 829 if field.GetType() == descriptor.FieldDescriptorProto_TYPE_GROUP && field.GetTypeName() == exp { 830 d.group = true 831 break 832 } 833 } 834 } 835 836 for _, field := range desc.Extension { 837 d.ext = append(d.ext, &ExtensionDescriptor{common{file}, field, d}) 838 } 839 840 return d 841 } 842 843 // Return a slice of all the Descriptors defined within this file 844 func wrapDescriptors(file *FileDescriptor) []*Descriptor { 845 sl := make([]*Descriptor, 0, len(file.MessageType)+10) 846 for i, desc := range file.MessageType { 847 sl = wrapThisDescriptor(sl, desc, nil, file, i) 848 } 849 return sl 850 } 851 852 // Wrap this Descriptor, recursively 853 func wrapThisDescriptor(sl []*Descriptor, desc *descriptor.DescriptorProto, parent *Descriptor, file *FileDescriptor, index int) []*Descriptor { 854 sl = append(sl, newDescriptor(desc, parent, file, index)) 855 me := sl[len(sl)-1] 856 for i, nested := range desc.NestedType { 857 sl = wrapThisDescriptor(sl, nested, me, file, i) 858 } 859 return sl 860 } 861 862 // Construct the EnumDescriptor 863 func newEnumDescriptor(desc *descriptor.EnumDescriptorProto, parent *Descriptor, file *FileDescriptor, index int) *EnumDescriptor { 864 ed := &EnumDescriptor{ 865 common: common{file}, 866 EnumDescriptorProto: desc, 867 parent: parent, 868 index: index, 869 } 870 if parent == nil { 871 ed.path = fmt.Sprintf("%d,%d", enumPath, index) 872 } else { 873 ed.path = fmt.Sprintf("%s,%d,%d", parent.path, messageEnumPath, index) 874 } 875 return ed 876 } 877 878 // Return a slice of all the EnumDescriptors defined within this file 879 func wrapEnumDescriptors(file *FileDescriptor, descs []*Descriptor) []*EnumDescriptor { 880 sl := make([]*EnumDescriptor, 0, len(file.EnumType)+10) 881 // Top-level enums. 882 for i, enum := range file.EnumType { 883 sl = append(sl, newEnumDescriptor(enum, nil, file, i)) 884 } 885 // Enums within messages. Enums within embedded messages appear in the outer-most message. 886 for _, nested := range descs { 887 for i, enum := range nested.EnumType { 888 sl = append(sl, newEnumDescriptor(enum, nested, file, i)) 889 } 890 } 891 return sl 892 } 893 894 // Return a slice of all the top-level ExtensionDescriptors defined within this file. 895 func wrapExtensions(file *FileDescriptor) []*ExtensionDescriptor { 896 var sl []*ExtensionDescriptor 897 for _, field := range file.Extension { 898 sl = append(sl, &ExtensionDescriptor{common{file}, field, nil}) 899 } 900 return sl 901 } 902 903 // Return a slice of all the types that are publicly imported into this file. 904 func wrapImported(file *FileDescriptor, g *Generator) (sl []*ImportedDescriptor) { 905 for _, index := range file.PublicDependency { 906 df := g.fileByName(file.Dependency[index]) 907 for _, d := range df.desc { 908 if d.GetOptions().GetMapEntry() { 909 continue 910 } 911 sl = append(sl, &ImportedDescriptor{common{file}, d}) 912 } 913 for _, e := range df.enum { 914 sl = append(sl, &ImportedDescriptor{common{file}, e}) 915 } 916 for _, ext := range df.ext { 917 sl = append(sl, &ImportedDescriptor{common{file}, ext}) 918 } 919 } 920 return 921 } 922 923 func extractComments(file *FileDescriptor) { 924 file.comments = make(map[string]*descriptor.SourceCodeInfo_Location) 925 for _, loc := range file.GetSourceCodeInfo().GetLocation() { 926 if loc.LeadingComments == nil { 927 continue 928 } 929 var p []string 930 for _, n := range loc.Path { 931 p = append(p, strconv.Itoa(int(n))) 932 } 933 file.comments[strings.Join(p, ",")] = loc 934 } 935 } 936 937 // BuildTypeNameMap builds the map from fully qualified type names to objects. 938 // The key names for the map come from the input data, which puts a period at the beginning. 939 // It should be called after SetPackageNames and before GenerateAllFiles. 940 func (g *Generator) BuildTypeNameMap() { 941 g.typeNameToObject = make(map[string]Object) 942 for _, f := range g.allFiles { 943 // The names in this loop are defined by the proto world, not us, so the 944 // package name may be empty. If so, the dotted package name of X will 945 // be ".X"; otherwise it will be ".pkg.X". 946 dottedPkg := "." + f.GetPackage() 947 if dottedPkg != "." { 948 dottedPkg += "." 949 } 950 for _, enum := range f.enum { 951 name := dottedPkg + dottedSlice(enum.TypeName()) 952 g.typeNameToObject[name] = enum 953 } 954 for _, desc := range f.desc { 955 name := dottedPkg + dottedSlice(desc.TypeName()) 956 g.typeNameToObject[name] = desc 957 } 958 } 959 } 960 961 // ObjectNamed, given a fully-qualified input type name as it appears in the input data, 962 // returns the descriptor for the message or enum with that name. 963 func (g *Generator) ObjectNamed(typeName string) Object { 964 o, ok := g.typeNameToObject[typeName] 965 if !ok { 966 g.Fail("can't find object with type", typeName) 967 } 968 return o 969 } 970 971 // AnnotatedAtoms is a list of atoms (as consumed by P) that records the file name and proto AST path from which they originated. 972 type AnnotatedAtoms struct { 973 source string 974 path string 975 atoms []interface{} 976 } 977 978 // Annotate records the file name and proto AST path of a list of atoms 979 // so that a later call to P can emit a link from each atom to its origin. 980 func Annotate(file *FileDescriptor, path string, atoms ...interface{}) *AnnotatedAtoms { 981 return &AnnotatedAtoms{source: *file.Name, path: path, atoms: atoms} 982 } 983 984 // printAtom prints the (atomic, non-annotation) argument to the generated output. 985 func (g *Generator) printAtom(v interface{}) { 986 switch v := v.(type) { 987 case string: 988 g.WriteString(v) 989 case *string: 990 g.WriteString(*v) 991 case bool: 992 fmt.Fprint(g, v) 993 case *bool: 994 fmt.Fprint(g, *v) 995 case int: 996 fmt.Fprint(g, v) 997 case *int32: 998 fmt.Fprint(g, *v) 999 case *int64: 1000 fmt.Fprint(g, *v) 1001 case float64: 1002 fmt.Fprint(g, v) 1003 case *float64: 1004 fmt.Fprint(g, *v) 1005 case GoPackageName: 1006 g.WriteString(string(v)) 1007 case GoImportPath: 1008 g.WriteString(strconv.Quote(string(v))) 1009 default: 1010 g.Fail(fmt.Sprintf("unknown type in printer: %T", v)) 1011 } 1012 } 1013 1014 // P prints the arguments to the generated output. It handles strings and int32s, plus 1015 // handling indirections because they may be *string, etc. Any inputs of type AnnotatedAtoms may emit 1016 // annotations in a .meta file in addition to outputting the atoms themselves (if g.annotateCode 1017 // is true). 1018 func (g *Generator) P(str ...interface{}) { 1019 if !g.writeOutput { 1020 return 1021 } 1022 g.WriteString(g.indent) 1023 for _, v := range str { 1024 switch v := v.(type) { 1025 case *AnnotatedAtoms: 1026 begin := int32(g.Len()) 1027 for _, v := range v.atoms { 1028 g.printAtom(v) 1029 } 1030 if g.annotateCode { 1031 end := int32(g.Len()) 1032 var path []int32 1033 for _, token := range strings.Split(v.path, ",") { 1034 val, err := strconv.ParseInt(token, 10, 32) 1035 if err != nil { 1036 g.Fail("could not parse proto AST path: ", err.Error()) 1037 } 1038 path = append(path, int32(val)) 1039 } 1040 g.annotations = append(g.annotations, &descriptor.GeneratedCodeInfo_Annotation{ 1041 Path: path, 1042 SourceFile: &v.source, 1043 Begin: &begin, 1044 End: &end, 1045 }) 1046 } 1047 default: 1048 g.printAtom(v) 1049 } 1050 } 1051 g.WriteByte('\n') 1052 } 1053 1054 // addInitf stores the given statement to be printed inside the file's init function. 1055 // The statement is given as a format specifier and arguments. 1056 func (g *Generator) addInitf(stmt string, a ...interface{}) { 1057 g.init = append(g.init, fmt.Sprintf(stmt, a...)) 1058 } 1059 1060 // In Indents the output one tab stop. 1061 func (g *Generator) In() { g.indent += "\t" } 1062 1063 // Out unindents the output one tab stop. 1064 func (g *Generator) Out() { 1065 if len(g.indent) > 0 { 1066 g.indent = g.indent[1:] 1067 } 1068 } 1069 1070 // GenerateAllFiles generates the output for all the files we're outputting. 1071 func (g *Generator) GenerateAllFiles() { 1072 // Initialize the plugins 1073 for _, p := range plugins { 1074 p.Init(g) 1075 } 1076 // Generate the output. The generator runs for every file, even the files 1077 // that we don't generate output for, so that we can collate the full list 1078 // of exported symbols to support public imports. 1079 genFileMap := make(map[*FileDescriptor]bool, len(g.genFiles)) 1080 for _, file := range g.genFiles { 1081 genFileMap[file] = true 1082 } 1083 for _, file := range g.allFiles { 1084 g.Reset() 1085 g.annotations = nil 1086 g.writeOutput = genFileMap[file] 1087 g.generate(file) 1088 if !g.writeOutput { 1089 continue 1090 } 1091 fname := file.goFileName(g.pathType) 1092 g.Response.File = append(g.Response.File, &plugin.CodeGeneratorResponse_File{ 1093 Name: proto.String(fname), 1094 Content: proto.String(g.String()), 1095 }) 1096 if g.annotateCode { 1097 // Store the generated code annotations in text, as the protoc plugin protocol requires that 1098 // strings contain valid UTF-8. 1099 g.Response.File = append(g.Response.File, &plugin.CodeGeneratorResponse_File{ 1100 Name: proto.String(file.goFileName(g.pathType) + ".meta"), 1101 Content: proto.String(proto.CompactTextString(&descriptor.GeneratedCodeInfo{Annotation: g.annotations})), 1102 }) 1103 } 1104 } 1105 } 1106 1107 // Run all the plugins associated with the file. 1108 func (g *Generator) runPlugins(file *FileDescriptor) { 1109 for _, p := range plugins { 1110 p.Generate(file) 1111 } 1112 } 1113 1114 // Fill the response protocol buffer with the generated output for all the files we're 1115 // supposed to generate. 1116 func (g *Generator) generate(file *FileDescriptor) { 1117 g.file = file 1118 g.usedPackages = make(map[GoImportPath]bool) 1119 g.packageNames = make(map[GoImportPath]GoPackageName) 1120 g.usedPackageNames = make(map[GoPackageName]bool) 1121 g.addedImports = make(map[GoImportPath]bool) 1122 for name := range globalPackageNames { 1123 g.usedPackageNames[name] = true 1124 } 1125 1126 g.P("// This is a compile-time assertion to ensure that this generated file") 1127 g.P("// is compatible with the proto package it is being compiled against.") 1128 g.P("// A compilation error at this line likely means your copy of the") 1129 g.P("// proto package needs to be updated.") 1130 g.P("const _ = ", g.Pkg["proto"], ".ProtoPackageIsVersion", generatedCodeVersion, " // please upgrade the proto package") 1131 g.P() 1132 1133 for _, td := range g.file.imp { 1134 g.generateImported(td) 1135 } 1136 // 不生成message,还有protoc-gen-go来生成 1137 /* 1138 for _, enum := range g.file.enum { 1139 g.generateEnum(enum) 1140 } 1141 for _, desc := range g.file.desc { 1142 // Don't generate virtual messages for maps. 1143 if desc.GetOptions().GetMapEntry() { 1144 continue 1145 } 1146 g.generateMessage(desc) 1147 } 1148 for _, ext := range g.file.ext { 1149 g.generateExtension(ext) 1150 } 1151 g.generateInitFunction() 1152 g.generateFileDescriptor(file) 1153 */ 1154 // Run the plugins before the imports so we know which imports are necessary. 1155 g.runPlugins(file) 1156 1157 // Generate header and imports last, though they appear first in the output. 1158 rem := g.Buffer 1159 remAnno := g.annotations 1160 g.Buffer = new(bytes.Buffer) 1161 g.annotations = nil 1162 g.generateHeader() 1163 g.generateImports() 1164 if !g.writeOutput { 1165 return 1166 } 1167 // Adjust the offsets for annotations displaced by the header and imports. 1168 for _, anno := range remAnno { 1169 *anno.Begin += int32(g.Len()) 1170 *anno.End += int32(g.Len()) 1171 g.annotations = append(g.annotations, anno) 1172 } 1173 g.Write(rem.Bytes()) 1174 1175 // Reformat generated code and patch annotation locations. 1176 fset := token.NewFileSet() 1177 original := g.Bytes() 1178 if g.annotateCode { 1179 // make a copy independent of g; we'll need it after Reset. 1180 original = append([]byte(nil), original...) 1181 } 1182 fileAST, err := parser.ParseFile(fset, "", original, parser.ParseComments) 1183 if err != nil { 1184 // Print out the bad code with line numbers. 1185 // This should never happen in practice, but it can while changing generated code, 1186 // so consider this a debugging aid. 1187 var src bytes.Buffer 1188 s := bufio.NewScanner(bytes.NewReader(original)) 1189 for line := 1; s.Scan(); line++ { 1190 fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes()) 1191 } 1192 g.Fail("bad Go source code was generated:", err.Error(), "\n"+src.String()) 1193 } 1194 ast.SortImports(fset, fileAST) 1195 g.Reset() 1196 err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(g, fset, fileAST) 1197 if err != nil { 1198 g.Fail("generated Go source code could not be reformatted:", err.Error()) 1199 } 1200 /* 1201 if g.annotateCode { 1202 m, err := remap.Compute(original, g.Bytes()) 1203 if err != nil { 1204 g.Fail("formatted generated Go source code could not be mapped back to the original code:", err.Error()) 1205 } 1206 for _, anno := range g.annotations { 1207 new, ok := m.Find(int(*anno.Begin), int(*anno.End)) 1208 if !ok { 1209 g.Fail("span in formatted generated Go source code could not be mapped back to the original code") 1210 } 1211 *anno.Begin = int32(new.Pos) 1212 *anno.End = int32(new.End) 1213 } 1214 } 1215 1216 */ 1217 } 1218 1219 // Generate the header, including package definition 1220 func (g *Generator) generateHeader() { 1221 g.P("// Code generated by ", g.toolName(), ". DO NOT EDIT.") 1222 if g.file.GetOptions().GetDeprecated() { 1223 g.P("// ", g.file.Name, " is a deprecated file.") 1224 } else { 1225 g.P("// source: ", g.file.Name) 1226 } 1227 g.P() 1228 g.PrintComments(strconv.Itoa(packagePath)) 1229 g.P() 1230 g.P("package ", g.file.packageName) 1231 g.P() 1232 } 1233 1234 // deprecationComment is the standard comment added to deprecated 1235 // messages, fields, enums, and enum values. 1236 var deprecationComment = "// Deprecated: Do not use." 1237 1238 // PrintComments prints any comments from the source .proto file. 1239 // The path is a comma-separated list of integers. 1240 // It returns an indication of whether any comments were printed. 1241 // See descriptor.proto for its format. 1242 func (g *Generator) PrintComments(path string) bool { 1243 if !g.writeOutput { 1244 return false 1245 } 1246 if c, ok := g.makeComments(path); ok { 1247 g.P(c) 1248 return true 1249 } 1250 return false 1251 } 1252 1253 // makeComments generates the comment string for the field, no "\n" at the end 1254 func (g *Generator) makeComments(path string) (string, bool) { 1255 loc, ok := g.file.comments[path] 1256 if !ok { 1257 return "", false 1258 } 1259 w := new(bytes.Buffer) 1260 nl := "" 1261 for _, line := range strings.Split(strings.TrimSuffix(loc.GetLeadingComments(), "\n"), "\n") { 1262 fmt.Fprintf(w, "%s//%s", nl, line) 1263 nl = "\n" 1264 } 1265 return w.String(), true 1266 } 1267 1268 func (g *Generator) fileByName(filename string) *FileDescriptor { 1269 return g.allFilesByName[filename] 1270 } 1271 1272 // weak returns whether the ith import of the current file is a weak import. 1273 func (g *Generator) weak(i int32) bool { 1274 for _, j := range g.file.WeakDependency { 1275 if j == i { 1276 return true 1277 } 1278 } 1279 return false 1280 } 1281 1282 // Generate the imports 1283 func (g *Generator) generateImports() { 1284 imports := make(map[GoImportPath]GoPackageName) 1285 for i, s := range g.file.Dependency { 1286 fd := g.fileByName(s) 1287 importPath := fd.importPath 1288 // Do not import our own package. 1289 if importPath == g.file.importPath { 1290 continue 1291 } 1292 // Do not import weak imports. 1293 if g.weak(int32(i)) { 1294 continue 1295 } 1296 // Do not import a package twice. 1297 if _, ok := imports[importPath]; ok { 1298 continue 1299 } 1300 // We need to import all the dependencies, even if we don't reference them, 1301 // because other code and tools depend on having the full transitive closure 1302 // of protocol buffer types in the binary. 1303 packageName := g.GoPackageName(importPath) 1304 if _, ok := g.usedPackages[importPath]; !ok { 1305 packageName = "_" 1306 } 1307 imports[importPath] = packageName 1308 } 1309 for importPath := range g.addedImports { 1310 imports[importPath] = g.GoPackageName(importPath) 1311 } 1312 // We almost always need a proto import. Rather than computing when we 1313 // do, which is tricky when there's a plugin, just import it and 1314 // reference it later. The same argument applies to the fmt and math packages. 1315 g.P("import (") 1316 g.P(g.Pkg["fmt"] + ` "fmt"`) 1317 g.P(g.Pkg["math"] + ` "math"`) 1318 g.P(g.Pkg["proto"]+" ", GoImportPath(g.ImportPrefix)+"github.com/golang/protobuf/proto") 1319 for importPath, packageName := range imports { 1320 g.P(packageName, " ", GoImportPath(g.ImportPrefix)+importPath) 1321 } 1322 g.P(")") 1323 g.P() 1324 // TODO: may need to worry about uniqueness across plugins 1325 for _, p := range plugins { 1326 p.GenerateImports(g.file) 1327 g.P() 1328 } 1329 g.P("// Reference imports to suppress errors if they are not otherwise used.") 1330 g.P("var _ = ", g.Pkg["proto"], ".Marshal") 1331 g.P("var _ = ", g.Pkg["fmt"], ".Errorf") 1332 g.P("var _ = ", g.Pkg["math"], ".Inf") 1333 g.P() 1334 } 1335 1336 func (g *Generator) generateImported(id *ImportedDescriptor) { 1337 df := id.o.File() 1338 filename := *df.Name 1339 if df.importPath == g.file.importPath { 1340 // Don't generate type aliases for files in the same Go package as this one. 1341 return 1342 } 1343 if !supportTypeAliases { 1344 g.Fail(fmt.Sprintf("%s: public imports require at least go1.9", filename)) 1345 } 1346 g.usedPackages[df.importPath] = true 1347 1348 for _, sym := range df.exported[id.o] { 1349 sym.GenerateAlias(g, filename, g.GoPackageName(df.importPath)) 1350 } 1351 1352 g.P() 1353 } 1354 1355 // Generate the enum definitions for this EnumDescriptor. 1356 func (g *Generator) generateEnum(enum *EnumDescriptor) { 1357 // The full type name 1358 typeName := enum.TypeName() 1359 // The full type name, CamelCased. 1360 ccTypeName := CamelCaseSlice(typeName) 1361 ccPrefix := enum.prefix() 1362 1363 deprecatedEnum := "" 1364 if enum.GetOptions().GetDeprecated() { 1365 deprecatedEnum = deprecationComment 1366 } 1367 g.PrintComments(enum.path) 1368 g.P("type ", Annotate(enum.file, enum.path, ccTypeName), " int32", deprecatedEnum) 1369 g.file.addExport(enum, enumSymbol{ccTypeName, enum.proto3()}) 1370 g.P("const (") 1371 for i, e := range enum.Value { 1372 etorPath := fmt.Sprintf("%s,%d,%d", enum.path, enumValuePath, i) 1373 g.PrintComments(etorPath) 1374 1375 deprecatedValue := "" 1376 if e.GetOptions().GetDeprecated() { 1377 deprecatedValue = deprecationComment 1378 } 1379 1380 name := ccPrefix + *e.Name 1381 g.P(Annotate(enum.file, etorPath, name), " ", ccTypeName, " = ", e.Number, " ", deprecatedValue) 1382 g.file.addExport(enum, constOrVarSymbol{name, "const", ccTypeName}) 1383 } 1384 g.P(")") 1385 g.P() 1386 g.P("var ", ccTypeName, "_name = map[int32]string{") 1387 generated := make(map[int32]bool) // avoid duplicate values 1388 for _, e := range enum.Value { 1389 duplicate := "" 1390 if _, present := generated[*e.Number]; present { 1391 duplicate = "// Duplicate value: " 1392 } 1393 g.P(duplicate, e.Number, ": ", strconv.Quote(*e.Name), ",") 1394 generated[*e.Number] = true 1395 } 1396 g.P("}") 1397 g.P() 1398 g.P("var ", ccTypeName, "_value = map[string]int32{") 1399 for _, e := range enum.Value { 1400 g.P(strconv.Quote(*e.Name), ": ", e.Number, ",") 1401 } 1402 g.P("}") 1403 g.P() 1404 1405 if !enum.proto3() { 1406 g.P("func (x ", ccTypeName, ") Enum() *", ccTypeName, " {") 1407 g.P("p := new(", ccTypeName, ")") 1408 g.P("*p = x") 1409 g.P("return p") 1410 g.P("}") 1411 g.P() 1412 } 1413 1414 g.P("func (x ", ccTypeName, ") String() string {") 1415 g.P("return ", g.Pkg["proto"], ".EnumName(", ccTypeName, "_name, int32(x))") 1416 g.P("}") 1417 g.P() 1418 1419 if !enum.proto3() { 1420 g.P("func (x *", ccTypeName, ") UnmarshalJSON(data []byte) error {") 1421 g.P("value, err := ", g.Pkg["proto"], ".UnmarshalJSONEnum(", ccTypeName, `_value, data, "`, ccTypeName, `")`) 1422 g.P("if err != nil {") 1423 g.P("return err") 1424 g.P("}") 1425 g.P("*x = ", ccTypeName, "(value)") 1426 g.P("return nil") 1427 g.P("}") 1428 g.P() 1429 } 1430 1431 var indexes []string 1432 for m := enum.parent; m != nil; m = m.parent { 1433 // XXX: skip groups? 1434 indexes = append([]string{strconv.Itoa(m.index)}, indexes...) 1435 } 1436 indexes = append(indexes, strconv.Itoa(enum.index)) 1437 g.P("func (", ccTypeName, ") EnumDescriptor() ([]byte, []int) {") 1438 g.P("return ", g.file.VarName(), ", []int{", strings.Join(indexes, ", "), "}") 1439 g.P("}") 1440 g.P() 1441 if enum.file.GetPackage() == "google.protobuf" && enum.GetName() == "NullValue" { 1442 g.P("func (", ccTypeName, `) XXX_WellKnownType() string { return "`, enum.GetName(), `" }`) 1443 g.P() 1444 } 1445 1446 g.generateEnumRegistration(enum) 1447 } 1448 1449 // The tag is a string like "varint,2,opt,name=fieldname,def=7" that 1450 // identifies details of the field for the protocol buffer marshaling and unmarshaling 1451 // code. The fields are: 1452 // wire encoding 1453 // protocol tag number 1454 // opt,req,rep for optional, required, or repeated 1455 // packed whether the encoding is "packed" (optional; repeated primitives only) 1456 // name= the original declared name 1457 // enum= the name of the enum type if it is an enum-typed field. 1458 // proto3 if this field is in a proto3 message 1459 // def= string representation of the default value, if any. 1460 // The default value must be in a representation that can be used at run-time 1461 // to generate the default value. Thus bools become 0 and 1, for instance. 1462 func (g *Generator) goTag(message *Descriptor, field *descriptor.FieldDescriptorProto, wiretype string) string { 1463 optrepreq := "" 1464 switch { 1465 case isOptional(field): 1466 optrepreq = "opt" 1467 case isRequired(field): 1468 optrepreq = "req" 1469 case isRepeated(field): 1470 optrepreq = "rep" 1471 } 1472 var defaultValue string 1473 if dv := field.DefaultValue; dv != nil { // set means an explicit default 1474 defaultValue = *dv 1475 // Some types need tweaking. 1476 switch *field.Type { 1477 case descriptor.FieldDescriptorProto_TYPE_BOOL: 1478 if defaultValue == "true" { 1479 defaultValue = "1" 1480 } else { 1481 defaultValue = "0" 1482 } 1483 case descriptor.FieldDescriptorProto_TYPE_STRING, 1484 descriptor.FieldDescriptorProto_TYPE_BYTES: 1485 // Nothing to do. Quoting is done for the whole tag. 1486 case descriptor.FieldDescriptorProto_TYPE_ENUM: 1487 // For enums we need to provide the integer constant. 1488 obj := g.ObjectNamed(field.GetTypeName()) 1489 if id, ok := obj.(*ImportedDescriptor); ok { 1490 // It is an enum that was publicly imported. 1491 // We need the underlying type. 1492 obj = id.o 1493 } 1494 enum, ok := obj.(*EnumDescriptor) 1495 if !ok { 1496 log.Printf("obj is a %T", obj) 1497 if id, ok := obj.(*ImportedDescriptor); ok { 1498 log.Printf("id.o is a %T", id.o) 1499 } 1500 g.Fail("unknown enum type", CamelCaseSlice(obj.TypeName())) 1501 } 1502 defaultValue = enum.integerValueAsString(defaultValue) 1503 case descriptor.FieldDescriptorProto_TYPE_FLOAT: 1504 if def := defaultValue; def != "inf" && def != "-inf" && def != "nan" { 1505 if f, err := strconv.ParseFloat(defaultValue, 32); err == nil { 1506 defaultValue = fmt.Sprint(float32(f)) 1507 } 1508 } 1509 case descriptor.FieldDescriptorProto_TYPE_DOUBLE: 1510 if def := defaultValue; def != "inf" && def != "-inf" && def != "nan" { 1511 if f, err := strconv.ParseFloat(defaultValue, 64); err == nil { 1512 defaultValue = fmt.Sprint(f) 1513 } 1514 } 1515 } 1516 defaultValue = ",def=" + defaultValue 1517 } 1518 enum := "" 1519 if *field.Type == descriptor.FieldDescriptorProto_TYPE_ENUM { 1520 // We avoid using obj.GoPackageName(), because we want to use the 1521 // original (proto-world) package name. 1522 obj := g.ObjectNamed(field.GetTypeName()) 1523 if id, ok := obj.(*ImportedDescriptor); ok { 1524 obj = id.o 1525 } 1526 enum = ",enum=" 1527 if pkg := obj.File().GetPackage(); pkg != "" { 1528 enum += pkg + "." 1529 } 1530 enum += CamelCaseSlice(obj.TypeName()) 1531 } 1532 packed := "" 1533 if (field.Options != nil && field.Options.GetPacked()) || 1534 // Per https://developers.google.com/protocol-buffers/docs/proto3#simple: 1535 // "In proto3, repeated fields of scalar numeric types use packed encoding by default." 1536 (message.proto3() && (field.Options == nil || field.Options.Packed == nil) && 1537 isRepeated(field) && isScalar(field)) { 1538 packed = ",packed" 1539 } 1540 fieldName := field.GetName() 1541 name := fieldName 1542 if *field.Type == descriptor.FieldDescriptorProto_TYPE_GROUP { 1543 // We must use the type name for groups instead of 1544 // the field name to preserve capitalization. 1545 // type_name in FieldDescriptorProto is fully-qualified, 1546 // but we only want the local part. 1547 name = *field.TypeName 1548 if i := strings.LastIndex(name, "."); i >= 0 { 1549 name = name[i+1:] 1550 } 1551 } 1552 if json := field.GetJsonName(); field.Extendee == nil && json != "" && json != name { 1553 // TODO: escaping might be needed, in which case 1554 // perhaps this should be in its own "json" tag. 1555 name += ",json=" + json 1556 } 1557 name = ",name=" + name 1558 if message.proto3() { 1559 name += ",proto3" 1560 } 1561 oneof := "" 1562 if field.OneofIndex != nil { 1563 oneof = ",oneof" 1564 } 1565 return strconv.Quote(fmt.Sprintf("%s,%d,%s%s%s%s%s%s", 1566 wiretype, 1567 field.GetNumber(), 1568 optrepreq, 1569 packed, 1570 name, 1571 enum, 1572 oneof, 1573 defaultValue)) 1574 } 1575 1576 func needsStar(typ descriptor.FieldDescriptorProto_Type) bool { 1577 switch typ { 1578 case descriptor.FieldDescriptorProto_TYPE_GROUP: 1579 return false 1580 case descriptor.FieldDescriptorProto_TYPE_MESSAGE: 1581 return false 1582 case descriptor.FieldDescriptorProto_TYPE_BYTES: 1583 return false 1584 } 1585 return true 1586 } 1587 1588 // TypeName is the printed name appropriate for an item. If the object is in the current file, 1589 // TypeName drops the package name and underscores the rest. 1590 // Otherwise the object is from another package; and the result is the underscored 1591 // package name followed by the item name. 1592 // The result always has an initial capital. 1593 func (g *Generator) TypeName(obj Object) string { 1594 return g.DefaultPackageName(obj) + CamelCaseSlice(obj.TypeName()) 1595 } 1596 1597 // GoType returns a string representing the type name, and the wire type 1598 func (g *Generator) GoType(message *Descriptor, field *descriptor.FieldDescriptorProto) (typ string, wire string) { 1599 // TODO: Options. 1600 switch *field.Type { 1601 case descriptor.FieldDescriptorProto_TYPE_DOUBLE: 1602 typ, wire = "float64", "fixed64" 1603 case descriptor.FieldDescriptorProto_TYPE_FLOAT: 1604 typ, wire = "float32", "fixed32" 1605 case descriptor.FieldDescriptorProto_TYPE_INT64: 1606 typ, wire = "int64", "varint" 1607 case descriptor.FieldDescriptorProto_TYPE_UINT64: 1608 typ, wire = "uint64", "varint" 1609 case descriptor.FieldDescriptorProto_TYPE_INT32: 1610 typ, wire = "int32", "varint" 1611 case descriptor.FieldDescriptorProto_TYPE_UINT32: 1612 typ, wire = "uint32", "varint" 1613 case descriptor.FieldDescriptorProto_TYPE_FIXED64: 1614 typ, wire = "uint64", "fixed64" 1615 case descriptor.FieldDescriptorProto_TYPE_FIXED32: 1616 typ, wire = "uint32", "fixed32" 1617 case descriptor.FieldDescriptorProto_TYPE_BOOL: 1618 typ, wire = "bool", "varint" 1619 case descriptor.FieldDescriptorProto_TYPE_STRING: 1620 typ, wire = "string", "bytes" 1621 case descriptor.FieldDescriptorProto_TYPE_GROUP: 1622 desc := g.ObjectNamed(field.GetTypeName()) 1623 typ, wire = "*"+g.TypeName(desc), "group" 1624 case descriptor.FieldDescriptorProto_TYPE_MESSAGE: 1625 desc := g.ObjectNamed(field.GetTypeName()) 1626 typ, wire = "*"+g.TypeName(desc), "bytes" 1627 case descriptor.FieldDescriptorProto_TYPE_BYTES: 1628 typ, wire = "[]byte", "bytes" 1629 case descriptor.FieldDescriptorProto_TYPE_ENUM: 1630 desc := g.ObjectNamed(field.GetTypeName()) 1631 typ, wire = g.TypeName(desc), "varint" 1632 case descriptor.FieldDescriptorProto_TYPE_SFIXED32: 1633 typ, wire = "int32", "fixed32" 1634 case descriptor.FieldDescriptorProto_TYPE_SFIXED64: 1635 typ, wire = "int64", "fixed64" 1636 case descriptor.FieldDescriptorProto_TYPE_SINT32: 1637 typ, wire = "int32", "zigzag32" 1638 case descriptor.FieldDescriptorProto_TYPE_SINT64: 1639 typ, wire = "int64", "zigzag64" 1640 default: 1641 g.Fail("unknown type for", field.GetName()) 1642 } 1643 if isRepeated(field) { 1644 typ = "[]" + typ 1645 } else if message != nil && message.proto3() { 1646 return 1647 } else if field.OneofIndex != nil && message != nil { 1648 return 1649 } else if needsStar(*field.Type) { 1650 typ = "*" + typ 1651 } 1652 return 1653 } 1654 1655 func (g *Generator) RecordTypeUse(t string) { 1656 if _, ok := g.typeNameToObject[t]; !ok { 1657 return 1658 } 1659 importPath := g.ObjectNamed(t).GoImportPath() 1660 if importPath == g.outputImportPath { 1661 // Don't record use of objects in our package. 1662 return 1663 } 1664 g.AddImport(importPath) 1665 g.usedPackages[importPath] = true 1666 } 1667 1668 // Method names that may be generated. Fields with these names get an 1669 // underscore appended. Any change to this set is a potential incompatible 1670 // API change because it changes generated field names. 1671 var methodNames = [...]string{ 1672 "Reset", 1673 "String", 1674 "ProtoMessage", 1675 "Marshal", 1676 "Unmarshal", 1677 "ExtensionRangeArray", 1678 "ExtensionMap", 1679 "Descriptor", 1680 } 1681 1682 // Names of messages in the `google.protobuf` package for which 1683 // we will generate XXX_WellKnownType methods. 1684 var wellKnownTypes = map[string]bool{ 1685 "Any": true, 1686 "Duration": true, 1687 "Empty": true, 1688 "Struct": true, 1689 "Timestamp": true, 1690 1691 "Value": true, 1692 "ListValue": true, 1693 "DoubleValue": true, 1694 "FloatValue": true, 1695 "Int64Value": true, 1696 "UInt64Value": true, 1697 "Int32Value": true, 1698 "UInt32Value": true, 1699 "BoolValue": true, 1700 "StringValue": true, 1701 "BytesValue": true, 1702 } 1703 1704 // getterDefault finds the default value for the field to return from a getter, 1705 // regardless of if it's a built in default or explicit from the source. Returns e.g. "nil", `""`, "Default_MessageType_FieldName" 1706 func (g *Generator) getterDefault(field *descriptor.FieldDescriptorProto, goMessageType string) string { 1707 if isRepeated(field) { 1708 return "nil" 1709 } 1710 if def := field.GetDefaultValue(); def != "" { 1711 defaultConstant := g.defaultConstantName(goMessageType, field.GetName()) 1712 if *field.Type != descriptor.FieldDescriptorProto_TYPE_BYTES { 1713 return defaultConstant 1714 } 1715 return "append([]byte(nil), " + defaultConstant + "...)" 1716 } 1717 switch *field.Type { 1718 case descriptor.FieldDescriptorProto_TYPE_BOOL: 1719 return "false" 1720 case descriptor.FieldDescriptorProto_TYPE_STRING: 1721 return `""` 1722 case descriptor.FieldDescriptorProto_TYPE_GROUP, descriptor.FieldDescriptorProto_TYPE_MESSAGE, descriptor.FieldDescriptorProto_TYPE_BYTES: 1723 return "nil" 1724 case descriptor.FieldDescriptorProto_TYPE_ENUM: 1725 obj := g.ObjectNamed(field.GetTypeName()) 1726 var enum *EnumDescriptor 1727 if id, ok := obj.(*ImportedDescriptor); ok { 1728 // The enum type has been publicly imported. 1729 enum, _ = id.o.(*EnumDescriptor) 1730 } else { 1731 enum, _ = obj.(*EnumDescriptor) 1732 } 1733 if enum == nil { 1734 log.Printf("don't know how to generate getter for %s", field.GetName()) 1735 return "nil" 1736 } 1737 if len(enum.Value) == 0 { 1738 return "0 // empty enum" 1739 } 1740 first := enum.Value[0].GetName() 1741 return g.DefaultPackageName(obj) + enum.prefix() + first 1742 default: 1743 return "0" 1744 } 1745 } 1746 1747 // defaultConstantName builds the name of the default constant from the message 1748 // type name and the untouched field name, e.g. "Default_MessageType_FieldName" 1749 func (g *Generator) defaultConstantName(goMessageType, protoFieldName string) string { 1750 return "Default_" + goMessageType + "_" + CamelCase(protoFieldName) 1751 } 1752 1753 // The different types of fields in a message and how to actually print them 1754 // Most of the logic for generateMessage is in the methods of these types. 1755 // 1756 // Note that the content of the field is irrelevant, a simpleField can contain 1757 // anything from a scalar to a group (which is just a message). 1758 // 1759 // Extension fields (and message sets) are however handled separately. 1760 // 1761 // simpleField - a field that is neiter weak nor oneof, possibly repeated 1762 // oneofField - field containing list of subfields: 1763 // - oneofSubField - a field within the oneof 1764 1765 // msgCtx contains the context for the generator functions. 1766 type msgCtx struct { 1767 goName string // Go struct name of the message, e.g. MessageName 1768 message *Descriptor // The descriptor for the message 1769 } 1770 1771 // fieldCommon contains data common to all types of fields. 1772 type fieldCommon struct { 1773 goName string // Go name of field, e.g. "FieldName" or "Descriptor_" 1774 protoName string // Name of field in proto language, e.g. "field_name" or "descriptor" 1775 getterName string // Name of the getter, e.g. "GetFieldName" or "GetDescriptor_" 1776 goType string // The Go type as a string, e.g. "*int32" or "*OtherMessage" 1777 tags string // The tag string/annotation for the type, e.g. `protobuf:"varint,8,opt,name=region_id,json=regionId"` 1778 fullPath string // The full path of the field as used by Annotate etc, e.g. "4,0,2,0" 1779 } 1780 1781 // getProtoName gets the proto name of a field, e.g. "field_name" or "descriptor". 1782 func (f *fieldCommon) getProtoName() string { 1783 return f.protoName 1784 } 1785 1786 // getGoType returns the go type of the field as a string, e.g. "*int32". 1787 func (f *fieldCommon) getGoType() string { 1788 return f.goType 1789 } 1790 1791 // simpleField is not weak, not a oneof, not an extension. Can be required, optional or repeated. 1792 type simpleField struct { 1793 fieldCommon 1794 protoTypeName string // Proto type name, empty if primitive, e.g. ".google.protobuf.Duration" 1795 protoType descriptor.FieldDescriptorProto_Type // Actual type enum value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64 1796 deprecated string // Deprecation comment, if any, e.g. "// Deprecated: Do not use." 1797 getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName" 1798 protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5" 1799 comment string // The full comment for the field, e.g. "// Useful information" 1800 } 1801 1802 // decl prints the declaration of the field in the struct (if any). 1803 func (f *simpleField) decl(g *Generator, mc *msgCtx) { 1804 g.P(f.comment, Annotate(mc.message.file, f.fullPath, f.goName), "\t", f.goType, "\t`", f.tags, "`", f.deprecated) 1805 } 1806 1807 // getter prints the getter for the field. 1808 func (f *simpleField) getter(g *Generator, mc *msgCtx) { 1809 star := "" 1810 tname := f.goType 1811 if needsStar(f.protoType) && tname[0] == '*' { 1812 tname = tname[1:] 1813 star = "*" 1814 } 1815 if f.deprecated != "" { 1816 g.P(f.deprecated) 1817 } 1818 g.P("func (m *", mc.goName, ") ", Annotate(mc.message.file, f.fullPath, f.getterName), "() "+tname+" {") 1819 if f.getterDef == "nil" { // Simpler getter 1820 g.P("if m != nil {") 1821 g.P("return m." + f.goName) 1822 g.P("}") 1823 g.P("return nil") 1824 g.P("}") 1825 g.P() 1826 return 1827 } 1828 if mc.message.proto3() { 1829 g.P("if m != nil {") 1830 } else { 1831 g.P("if m != nil && m." + f.goName + " != nil {") 1832 } 1833 g.P("return " + star + "m." + f.goName) 1834 g.P("}") 1835 g.P("return ", f.getterDef) 1836 g.P("}") 1837 g.P() 1838 } 1839 1840 // setter prints the setter method of the field. 1841 func (f *simpleField) setter(g *Generator, mc *msgCtx) { 1842 // No setter for regular fields yet 1843 } 1844 1845 // getProtoDef returns the default value explicitly stated in the proto file, e.g "yoshi" or "5". 1846 func (f *simpleField) getProtoDef() string { 1847 return f.protoDef 1848 } 1849 1850 // getProtoTypeName returns the protobuf type name for the field as returned by field.GetTypeName(), e.g. ".google.protobuf.Duration". 1851 func (f *simpleField) getProtoTypeName() string { 1852 return f.protoTypeName 1853 } 1854 1855 // getProtoType returns the *field.Type value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64. 1856 func (f *simpleField) getProtoType() descriptor.FieldDescriptorProto_Type { 1857 return f.protoType 1858 } 1859 1860 // oneofSubFields are kept slize held by each oneofField. They do not appear in the top level slize of fields for the message. 1861 type oneofSubField struct { 1862 fieldCommon 1863 protoTypeName string // Proto type name, empty if primitive, e.g. ".google.protobuf.Duration" 1864 protoType descriptor.FieldDescriptorProto_Type // Actual type enum value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64 1865 oneofTypeName string // Type name of the enclosing struct, e.g. "MessageName_FieldName" 1866 fieldNumber int // Actual field number, as defined in proto, e.g. 12 1867 getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName" 1868 protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5" 1869 deprecated string // Deprecation comment, if any. 1870 } 1871 1872 // typedNil prints a nil casted to the pointer to this field. 1873 // - for XXX_OneofWrappers 1874 func (f *oneofSubField) typedNil(g *Generator) { 1875 g.P("(*", f.oneofTypeName, ")(nil),") 1876 } 1877 1878 // getProtoDef returns the default value explicitly stated in the proto file, e.g "yoshi" or "5". 1879 func (f *oneofSubField) getProtoDef() string { 1880 return f.protoDef 1881 } 1882 1883 // getProtoTypeName returns the protobuf type name for the field as returned by field.GetTypeName(), e.g. ".google.protobuf.Duration". 1884 func (f *oneofSubField) getProtoTypeName() string { 1885 return f.protoTypeName 1886 } 1887 1888 // getProtoType returns the *field.Type value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64. 1889 func (f *oneofSubField) getProtoType() descriptor.FieldDescriptorProto_Type { 1890 return f.protoType 1891 } 1892 1893 // oneofField represents the oneof on top level. 1894 // The alternative fields within the oneof are represented by oneofSubField. 1895 type oneofField struct { 1896 fieldCommon 1897 subFields []*oneofSubField // All the possible oneof fields 1898 comment string // The full comment for the field, e.g. "// Types that are valid to be assigned to MyOneof:\n\\" 1899 } 1900 1901 // decl prints the declaration of the field in the struct (if any). 1902 func (f *oneofField) decl(g *Generator, mc *msgCtx) { 1903 comment := f.comment 1904 for _, sf := range f.subFields { 1905 comment += "//\t*" + sf.oneofTypeName + "\n" 1906 } 1907 g.P(comment, Annotate(mc.message.file, f.fullPath, f.goName), " ", f.goType, " `", f.tags, "`") 1908 } 1909 1910 // getter for a oneof field will print additional discriminators and interfaces for the oneof, 1911 // also it prints all the getters for the sub fields. 1912 func (f *oneofField) getter(g *Generator, mc *msgCtx) { 1913 // The discriminator type 1914 g.P("type ", f.goType, " interface {") 1915 g.P(f.goType, "()") 1916 g.P("}") 1917 g.P() 1918 // The subField types, fulfilling the discriminator type contract 1919 for _, sf := range f.subFields { 1920 g.P("type ", Annotate(mc.message.file, sf.fullPath, sf.oneofTypeName), " struct {") 1921 g.P(Annotate(mc.message.file, sf.fullPath, sf.goName), " ", sf.goType, " `", sf.tags, "`") 1922 g.P("}") 1923 g.P() 1924 } 1925 for _, sf := range f.subFields { 1926 g.P("func (*", sf.oneofTypeName, ") ", f.goType, "() {}") 1927 g.P() 1928 } 1929 // Getter for the oneof field 1930 g.P("func (m *", mc.goName, ") ", Annotate(mc.message.file, f.fullPath, f.getterName), "() ", f.goType, " {") 1931 g.P("if m != nil { return m.", f.goName, " }") 1932 g.P("return nil") 1933 g.P("}") 1934 g.P() 1935 // Getters for each oneof 1936 for _, sf := range f.subFields { 1937 if sf.deprecated != "" { 1938 g.P(sf.deprecated) 1939 } 1940 g.P("func (m *", mc.goName, ") ", Annotate(mc.message.file, sf.fullPath, sf.getterName), "() "+sf.goType+" {") 1941 g.P("if x, ok := m.", f.getterName, "().(*", sf.oneofTypeName, "); ok {") 1942 g.P("return x.", sf.goName) 1943 g.P("}") 1944 g.P("return ", sf.getterDef) 1945 g.P("}") 1946 g.P() 1947 } 1948 } 1949 1950 // setter prints the setter method of the field. 1951 func (f *oneofField) setter(g *Generator, mc *msgCtx) { 1952 // No setters for oneof yet 1953 } 1954 1955 // topLevelField interface implemented by all types of fields on the top level (not oneofSubField). 1956 type topLevelField interface { 1957 decl(g *Generator, mc *msgCtx) // print declaration within the struct 1958 getter(g *Generator, mc *msgCtx) // print getter 1959 setter(g *Generator, mc *msgCtx) // print setter if applicable 1960 } 1961 1962 // defField interface implemented by all types of fields that can have defaults (not oneofField, but instead oneofSubField). 1963 type defField interface { 1964 getProtoDef() string // default value explicitly stated in the proto file, e.g "yoshi" or "5" 1965 getProtoName() string // proto name of a field, e.g. "field_name" or "descriptor" 1966 getGoType() string // go type of the field as a string, e.g. "*int32" 1967 getProtoTypeName() string // protobuf type name for the field, e.g. ".google.protobuf.Duration" 1968 getProtoType() descriptor.FieldDescriptorProto_Type // *field.Type value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64 1969 } 1970 1971 // generateDefaultConstants adds constants for default values if needed, which is only if the default value is. 1972 // explicit in the proto. 1973 func (g *Generator) generateDefaultConstants(mc *msgCtx, topLevelFields []topLevelField) { 1974 // Collect fields that can have defaults 1975 dFields := []defField{} 1976 for _, pf := range topLevelFields { 1977 if f, ok := pf.(*oneofField); ok { 1978 for _, osf := range f.subFields { 1979 dFields = append(dFields, osf) 1980 } 1981 continue 1982 } 1983 dFields = append(dFields, pf.(defField)) 1984 } 1985 for _, df := range dFields { 1986 def := df.getProtoDef() 1987 if def == "" { 1988 continue 1989 } 1990 fieldname := g.defaultConstantName(mc.goName, df.getProtoName()) 1991 typename := df.getGoType() 1992 if typename[0] == '*' { 1993 typename = typename[1:] 1994 } 1995 kind := "const " 1996 switch { 1997 case typename == "bool": 1998 case typename == "string": 1999 def = strconv.Quote(def) 2000 case typename == "[]byte": 2001 def = "[]byte(" + strconv.Quote(unescape(def)) + ")" 2002 kind = "var " 2003 case def == "inf", def == "-inf", def == "nan": 2004 // These names are known to, and defined by, the protocol language. 2005 switch def { 2006 case "inf": 2007 def = "math.Inf(1)" 2008 case "-inf": 2009 def = "math.Inf(-1)" 2010 case "nan": 2011 def = "math.NaN()" 2012 } 2013 if df.getProtoType() == descriptor.FieldDescriptorProto_TYPE_FLOAT { 2014 def = "float32(" + def + ")" 2015 } 2016 kind = "var " 2017 case df.getProtoType() == descriptor.FieldDescriptorProto_TYPE_FLOAT: 2018 if f, err := strconv.ParseFloat(def, 32); err == nil { 2019 def = fmt.Sprint(float32(f)) 2020 } 2021 case df.getProtoType() == descriptor.FieldDescriptorProto_TYPE_DOUBLE: 2022 if f, err := strconv.ParseFloat(def, 64); err == nil { 2023 def = fmt.Sprint(f) 2024 } 2025 case df.getProtoType() == descriptor.FieldDescriptorProto_TYPE_ENUM: 2026 // Must be an enum. Need to construct the prefixed name. 2027 obj := g.ObjectNamed(df.getProtoTypeName()) 2028 var enum *EnumDescriptor 2029 if id, ok := obj.(*ImportedDescriptor); ok { 2030 // The enum type has been publicly imported. 2031 enum, _ = id.o.(*EnumDescriptor) 2032 } else { 2033 enum, _ = obj.(*EnumDescriptor) 2034 } 2035 if enum == nil { 2036 log.Printf("don't know how to generate constant for %s", fieldname) 2037 continue 2038 } 2039 def = g.DefaultPackageName(obj) + enum.prefix() + def 2040 } 2041 g.P(kind, fieldname, " ", typename, " = ", def) 2042 g.file.addExport(mc.message, constOrVarSymbol{fieldname, kind, ""}) 2043 } 2044 g.P() 2045 } 2046 2047 // generateInternalStructFields just adds the XXX_<something> fields to the message struct. 2048 func (g *Generator) generateInternalStructFields(mc *msgCtx, topLevelFields []topLevelField) { 2049 g.P("XXX_NoUnkeyedLiteral\tstruct{} `json:\"-\"`") // prevent unkeyed struct literals 2050 if len(mc.message.ExtensionRange) > 0 { 2051 messageset := "" 2052 if opts := mc.message.Options; opts != nil && opts.GetMessageSetWireFormat() { 2053 messageset = "protobuf_messageset:\"1\" " 2054 } 2055 g.P(g.Pkg["proto"], ".XXX_InternalExtensions `", messageset, "json:\"-\"`") 2056 } 2057 g.P("XXX_unrecognized\t[]byte `json:\"-\"`") 2058 g.P("XXX_sizecache\tint32 `json:\"-\"`") 2059 2060 } 2061 2062 // generateOneofFuncs adds all the utility functions for oneof, including marshalling, unmarshalling and sizer. 2063 func (g *Generator) generateOneofFuncs(mc *msgCtx, topLevelFields []topLevelField) { 2064 ofields := []*oneofField{} 2065 for _, f := range topLevelFields { 2066 if o, ok := f.(*oneofField); ok { 2067 ofields = append(ofields, o) 2068 } 2069 } 2070 if len(ofields) == 0 { 2071 return 2072 } 2073 2074 // OneofFuncs 2075 g.P("// XXX_OneofWrappers is for the internal use of the proto package.") 2076 g.P("func (*", mc.goName, ") XXX_OneofWrappers() []interface{} {") 2077 g.P("return []interface{}{") 2078 for _, of := range ofields { 2079 for _, sf := range of.subFields { 2080 sf.typedNil(g) 2081 } 2082 } 2083 g.P("}") 2084 g.P("}") 2085 g.P() 2086 } 2087 2088 // generateMessageStruct adds the actual struct with it's members (but not methods) to the output. 2089 func (g *Generator) generateMessageStruct(mc *msgCtx, topLevelFields []topLevelField) { 2090 comments := g.PrintComments(mc.message.path) 2091 2092 // Guarantee deprecation comments appear after user-provided comments. 2093 if mc.message.GetOptions().GetDeprecated() { 2094 if comments { 2095 // Convention: Separate deprecation comments from original 2096 // comments with an empty line. 2097 g.P("//") 2098 } 2099 g.P(deprecationComment) 2100 } 2101 2102 g.P("type ", Annotate(mc.message.file, mc.message.path, mc.goName), " struct {") 2103 for _, pf := range topLevelFields { 2104 pf.decl(g, mc) 2105 } 2106 g.generateInternalStructFields(mc, topLevelFields) 2107 g.P("}") 2108 } 2109 2110 // generateGetters adds getters for all fields, including oneofs and weak fields when applicable. 2111 func (g *Generator) generateGetters(mc *msgCtx, topLevelFields []topLevelField) { 2112 for _, pf := range topLevelFields { 2113 pf.getter(g, mc) 2114 } 2115 } 2116 2117 // generateSetters add setters for all fields, including oneofs and weak fields when applicable. 2118 func (g *Generator) generateSetters(mc *msgCtx, topLevelFields []topLevelField) { 2119 for _, pf := range topLevelFields { 2120 pf.setter(g, mc) 2121 } 2122 } 2123 2124 // generateCommonMethods adds methods to the message that are not on a per field basis. 2125 func (g *Generator) generateCommonMethods(mc *msgCtx) { 2126 // Reset, String and ProtoMessage methods. 2127 g.P("func (m *", mc.goName, ") Reset() { *m = ", mc.goName, "{} }") 2128 g.P("func (m *", mc.goName, ") String() string { return ", g.Pkg["proto"], ".CompactTextString(m) }") 2129 g.P("func (*", mc.goName, ") ProtoMessage() {}") 2130 var indexes []string 2131 for m := mc.message; m != nil; m = m.parent { 2132 indexes = append([]string{strconv.Itoa(m.index)}, indexes...) 2133 } 2134 g.P("func (*", mc.goName, ") Descriptor() ([]byte, []int) {") 2135 g.P("return ", g.file.VarName(), ", []int{", strings.Join(indexes, ", "), "}") 2136 g.P("}") 2137 g.P() 2138 // TODO: Revisit the decision to use a XXX_WellKnownType method 2139 // if we change proto.MessageName to work with multiple equivalents. 2140 if mc.message.file.GetPackage() == "google.protobuf" && wellKnownTypes[mc.message.GetName()] { 2141 g.P("func (*", mc.goName, `) XXX_WellKnownType() string { return "`, mc.message.GetName(), `" }`) 2142 g.P() 2143 } 2144 2145 // Extension support methods 2146 if len(mc.message.ExtensionRange) > 0 { 2147 g.P() 2148 g.P("var extRange_", mc.goName, " = []", g.Pkg["proto"], ".ExtensionRange{") 2149 for _, r := range mc.message.ExtensionRange { 2150 end := fmt.Sprint(*r.End - 1) // make range inclusive on both ends 2151 g.P("{Start: ", r.Start, ", End: ", end, "},") 2152 } 2153 g.P("}") 2154 g.P("func (*", mc.goName, ") ExtensionRangeArray() []", g.Pkg["proto"], ".ExtensionRange {") 2155 g.P("return extRange_", mc.goName) 2156 g.P("}") 2157 g.P() 2158 } 2159 2160 // TODO: It does not scale to keep adding another method for every 2161 // operation on protos that we want to switch over to using the 2162 // table-driven approach. Instead, we should only add a single method 2163 // that allows getting access to the *InternalMessageInfo struct and then 2164 // calling Unmarshal, Marshal, Merge, Size, and Discard directly on that. 2165 2166 // Wrapper for table-driven marshaling and unmarshaling. 2167 g.P("func (m *", mc.goName, ") XXX_Unmarshal(b []byte) error {") 2168 g.P("return xxx_messageInfo_", mc.goName, ".Unmarshal(m, b)") 2169 g.P("}") 2170 2171 g.P("func (m *", mc.goName, ") XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {") 2172 g.P("return xxx_messageInfo_", mc.goName, ".Marshal(b, m, deterministic)") 2173 g.P("}") 2174 2175 g.P("func (m *", mc.goName, ") XXX_Merge(src ", g.Pkg["proto"], ".Message) {") 2176 g.P("xxx_messageInfo_", mc.goName, ".Merge(m, src)") 2177 g.P("}") 2178 2179 g.P("func (m *", mc.goName, ") XXX_Size() int {") // avoid name clash with "Size" field in some message 2180 g.P("return xxx_messageInfo_", mc.goName, ".Size(m)") 2181 g.P("}") 2182 2183 g.P("func (m *", mc.goName, ") XXX_DiscardUnknown() {") 2184 g.P("xxx_messageInfo_", mc.goName, ".DiscardUnknown(m)") 2185 g.P("}") 2186 2187 g.P("var xxx_messageInfo_", mc.goName, " ", g.Pkg["proto"], ".InternalMessageInfo") 2188 g.P() 2189 } 2190 2191 // Generate the type, methods and default constant definitions for this Descriptor. 2192 func (g *Generator) generateMessage(message *Descriptor) { 2193 topLevelFields := []topLevelField{} 2194 oFields := make(map[int32]*oneofField) 2195 // The full type name 2196 typeName := message.TypeName() 2197 // The full type name, CamelCased. 2198 goTypeName := CamelCaseSlice(typeName) 2199 2200 usedNames := make(map[string]bool) 2201 for _, n := range methodNames { 2202 usedNames[n] = true 2203 } 2204 2205 // allocNames finds a conflict-free variation of the given strings, 2206 // consistently mutating their suffixes. 2207 // It returns the same number of strings. 2208 allocNames := func(ns ...string) []string { 2209 Loop: 2210 for { 2211 for _, n := range ns { 2212 if usedNames[n] { 2213 for i := range ns { 2214 ns[i] += "_" 2215 } 2216 continue Loop 2217 } 2218 } 2219 for _, n := range ns { 2220 usedNames[n] = true 2221 } 2222 return ns 2223 } 2224 } 2225 2226 mapFieldTypes := make(map[*descriptor.FieldDescriptorProto]string) // keep track of the map fields to be added later 2227 2228 // Build a structure more suitable for generating the text in one pass 2229 for i, field := range message.Field { 2230 // Allocate the getter and the field at the same time so name 2231 // collisions create field/method consistent names. 2232 // TODO: This allocation occurs based on the order of the fields 2233 // in the proto file, meaning that a change in the field 2234 // ordering can change generated Method/Field names. 2235 base := CamelCase(*field.Name) 2236 ns := allocNames(base, "Get"+base) 2237 fieldName, fieldGetterName := ns[0], ns[1] 2238 typename, wiretype := g.GoType(message, field) 2239 jsonName := *field.Name 2240 tag := fmt.Sprintf("protobuf:%s json:%q", g.goTag(message, field, wiretype), jsonName+",omitempty") 2241 2242 oneof := field.OneofIndex != nil 2243 if oneof && oFields[*field.OneofIndex] == nil { 2244 odp := message.OneofDecl[int(*field.OneofIndex)] 2245 base := CamelCase(odp.GetName()) 2246 names := allocNames(base, "Get"+base) 2247 fname, gname := names[0], names[1] 2248 2249 // This is the first field of a oneof we haven't seen before. 2250 // Generate the union field. 2251 oneofFullPath := fmt.Sprintf("%s,%d,%d", message.path, messageOneofPath, *field.OneofIndex) 2252 c, ok := g.makeComments(oneofFullPath) 2253 if ok { 2254 c += "\n//\n" 2255 } 2256 c += "// Types that are valid to be assigned to " + fname + ":\n" 2257 // Generate the rest of this comment later, 2258 // when we've computed any disambiguation. 2259 2260 dname := "is" + goTypeName + "_" + fname 2261 tag := `protobuf_oneof:"` + odp.GetName() + `"` 2262 of := oneofField{ 2263 fieldCommon: fieldCommon{ 2264 goName: fname, 2265 getterName: gname, 2266 goType: dname, 2267 tags: tag, 2268 protoName: odp.GetName(), 2269 fullPath: oneofFullPath, 2270 }, 2271 comment: c, 2272 } 2273 topLevelFields = append(topLevelFields, &of) 2274 oFields[*field.OneofIndex] = &of 2275 } 2276 2277 if *field.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE { 2278 desc := g.ObjectNamed(field.GetTypeName()) 2279 if d, ok := desc.(*Descriptor); ok && d.GetOptions().GetMapEntry() { 2280 // Figure out the Go types and tags for the key and value types. 2281 keyField, valField := d.Field[0], d.Field[1] 2282 keyType, keyWire := g.GoType(d, keyField) 2283 valType, valWire := g.GoType(d, valField) 2284 keyTag, valTag := g.goTag(d, keyField, keyWire), g.goTag(d, valField, valWire) 2285 2286 // We don't use stars, except for message-typed values. 2287 // Message and enum types are the only two possibly foreign types used in maps, 2288 // so record their use. They are not permitted as map keys. 2289 keyType = strings.TrimPrefix(keyType, "*") 2290 switch *valField.Type { 2291 case descriptor.FieldDescriptorProto_TYPE_ENUM: 2292 valType = strings.TrimPrefix(valType, "*") 2293 g.RecordTypeUse(valField.GetTypeName()) 2294 case descriptor.FieldDescriptorProto_TYPE_MESSAGE: 2295 g.RecordTypeUse(valField.GetTypeName()) 2296 default: 2297 valType = strings.TrimPrefix(valType, "*") 2298 } 2299 2300 typename = fmt.Sprintf("map[%s]%s", keyType, valType) 2301 mapFieldTypes[field] = typename // record for the getter generation 2302 2303 tag += fmt.Sprintf(" protobuf_key:%s protobuf_val:%s", keyTag, valTag) 2304 } 2305 } 2306 2307 fieldDeprecated := "" 2308 if field.GetOptions().GetDeprecated() { 2309 fieldDeprecated = deprecationComment 2310 } 2311 2312 dvalue := g.getterDefault(field, goTypeName) 2313 if oneof { 2314 tname := goTypeName + "_" + fieldName 2315 // It is possible for this to collide with a message or enum 2316 // nested in this message. Check for collisions. 2317 for { 2318 ok := true 2319 for _, desc := range message.nested { 2320 if CamelCaseSlice(desc.TypeName()) == tname { 2321 ok = false 2322 break 2323 } 2324 } 2325 for _, enum := range message.enums { 2326 if CamelCaseSlice(enum.TypeName()) == tname { 2327 ok = false 2328 break 2329 } 2330 } 2331 if !ok { 2332 tname += "_" 2333 continue 2334 } 2335 break 2336 } 2337 2338 oneofField := oFields[*field.OneofIndex] 2339 tag := "protobuf:" + g.goTag(message, field, wiretype) 2340 sf := oneofSubField{ 2341 fieldCommon: fieldCommon{ 2342 goName: fieldName, 2343 getterName: fieldGetterName, 2344 goType: typename, 2345 tags: tag, 2346 protoName: field.GetName(), 2347 fullPath: fmt.Sprintf("%s,%d,%d", message.path, messageFieldPath, i), 2348 }, 2349 protoTypeName: field.GetTypeName(), 2350 fieldNumber: int(*field.Number), 2351 protoType: *field.Type, 2352 getterDef: dvalue, 2353 protoDef: field.GetDefaultValue(), 2354 oneofTypeName: tname, 2355 deprecated: fieldDeprecated, 2356 } 2357 oneofField.subFields = append(oneofField.subFields, &sf) 2358 g.RecordTypeUse(field.GetTypeName()) 2359 continue 2360 } 2361 2362 fieldFullPath := fmt.Sprintf("%s,%d,%d", message.path, messageFieldPath, i) 2363 c, ok := g.makeComments(fieldFullPath) 2364 if ok { 2365 c += "\n" 2366 } 2367 rf := simpleField{ 2368 fieldCommon: fieldCommon{ 2369 goName: fieldName, 2370 getterName: fieldGetterName, 2371 goType: typename, 2372 tags: tag, 2373 protoName: field.GetName(), 2374 fullPath: fieldFullPath, 2375 }, 2376 protoTypeName: field.GetTypeName(), 2377 protoType: *field.Type, 2378 deprecated: fieldDeprecated, 2379 getterDef: dvalue, 2380 protoDef: field.GetDefaultValue(), 2381 comment: c, 2382 } 2383 var pf topLevelField = &rf 2384 2385 topLevelFields = append(topLevelFields, pf) 2386 g.RecordTypeUse(field.GetTypeName()) 2387 } 2388 2389 mc := &msgCtx{ 2390 goName: goTypeName, 2391 message: message, 2392 } 2393 2394 g.generateMessageStruct(mc, topLevelFields) 2395 g.P() 2396 g.generateCommonMethods(mc) 2397 g.P() 2398 g.generateDefaultConstants(mc, topLevelFields) 2399 g.P() 2400 g.generateGetters(mc, topLevelFields) 2401 g.P() 2402 g.generateSetters(mc, topLevelFields) 2403 g.P() 2404 g.generateOneofFuncs(mc, topLevelFields) 2405 g.P() 2406 2407 var oneofTypes []string 2408 for _, f := range topLevelFields { 2409 if of, ok := f.(*oneofField); ok { 2410 for _, osf := range of.subFields { 2411 oneofTypes = append(oneofTypes, osf.oneofTypeName) 2412 } 2413 } 2414 } 2415 2416 opts := message.Options 2417 ms := &messageSymbol{ 2418 sym: goTypeName, 2419 hasExtensions: len(message.ExtensionRange) > 0, 2420 isMessageSet: opts != nil && opts.GetMessageSetWireFormat(), 2421 oneofTypes: oneofTypes, 2422 } 2423 g.file.addExport(message, ms) 2424 2425 for _, ext := range message.ext { 2426 g.generateExtension(ext) 2427 } 2428 2429 fullName := strings.Join(message.TypeName(), ".") 2430 if g.file.Package != nil { 2431 fullName = *g.file.Package + "." + fullName 2432 } 2433 2434 g.addInitf("%s.RegisterType((*%s)(nil), %q)", g.Pkg["proto"], goTypeName, fullName) 2435 // Register types for native map types. 2436 for _, k := range mapFieldKeys(mapFieldTypes) { 2437 fullName := strings.TrimPrefix(*k.TypeName, ".") 2438 g.addInitf("%s.RegisterMapType((%s)(nil), %q)", g.Pkg["proto"], mapFieldTypes[k], fullName) 2439 } 2440 2441 } 2442 2443 type byTypeName []*descriptor.FieldDescriptorProto 2444 2445 func (a byTypeName) Len() int { return len(a) } 2446 func (a byTypeName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 2447 func (a byTypeName) Less(i, j int) bool { return *a[i].TypeName < *a[j].TypeName } 2448 2449 // mapFieldKeys returns the keys of m in a consistent order. 2450 func mapFieldKeys(m map[*descriptor.FieldDescriptorProto]string) []*descriptor.FieldDescriptorProto { 2451 keys := make([]*descriptor.FieldDescriptorProto, 0, len(m)) 2452 for k := range m { 2453 keys = append(keys, k) 2454 } 2455 sort.Sort(byTypeName(keys)) 2456 return keys 2457 } 2458 2459 var escapeChars = [256]byte{ 2460 'a': '\a', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t', 'v': '\v', '\\': '\\', '"': '"', '\'': '\'', '?': '?', 2461 } 2462 2463 // unescape reverses the "C" escaping that protoc does for default values of bytes fields. 2464 // It is best effort in that it effectively ignores malformed input. Seemingly invalid escape 2465 // sequences are conveyed, unmodified, into the decoded result. 2466 func unescape(s string) string { 2467 // NB: Sadly, we can't use strconv.Unquote because protoc will escape both 2468 // single and double quotes, but strconv.Unquote only allows one or the 2469 // other (based on actual surrounding quotes of its input argument). 2470 2471 var out []byte 2472 for len(s) > 0 { 2473 // regular character, or too short to be valid escape 2474 if s[0] != '\\' || len(s) < 2 { 2475 out = append(out, s[0]) 2476 s = s[1:] 2477 } else if c := escapeChars[s[1]]; c != 0 { 2478 // escape sequence 2479 out = append(out, c) 2480 s = s[2:] 2481 } else if s[1] == 'x' || s[1] == 'X' { 2482 // hex escape, e.g. "\x80 2483 if len(s) < 4 { 2484 // too short to be valid 2485 out = append(out, s[:2]...) 2486 s = s[2:] 2487 continue 2488 } 2489 v, err := strconv.ParseUint(s[2:4], 16, 8) 2490 if err != nil { 2491 out = append(out, s[:4]...) 2492 } else { 2493 out = append(out, byte(v)) 2494 } 2495 s = s[4:] 2496 } else if '0' <= s[1] && s[1] <= '7' { 2497 // octal escape, can vary from 1 to 3 octal digits; e.g., "\0" "\40" or "\164" 2498 // so consume up to 2 more bytes or up to end-of-string 2499 n := len(s[1:]) - len(strings.TrimLeft(s[1:], "01234567")) 2500 if n > 3 { 2501 n = 3 2502 } 2503 v, err := strconv.ParseUint(s[1:1+n], 8, 8) 2504 if err != nil { 2505 out = append(out, s[:1+n]...) 2506 } else { 2507 out = append(out, byte(v)) 2508 } 2509 s = s[1+n:] 2510 } else { 2511 // bad escape, just propagate the slash as-is 2512 out = append(out, s[0]) 2513 s = s[1:] 2514 } 2515 } 2516 2517 return string(out) 2518 } 2519 2520 func (g *Generator) generateExtension(ext *ExtensionDescriptor) { 2521 ccTypeName := ext.DescName() 2522 2523 extObj := g.ObjectNamed(*ext.Extendee) 2524 var extDesc *Descriptor 2525 if id, ok := extObj.(*ImportedDescriptor); ok { 2526 // This is extending a publicly imported message. 2527 // We need the underlying type for goTag. 2528 extDesc = id.o.(*Descriptor) 2529 } else { 2530 extDesc = extObj.(*Descriptor) 2531 } 2532 extendedType := "*" + g.TypeName(extObj) // always use the original 2533 field := ext.FieldDescriptorProto 2534 fieldType, wireType := g.GoType(ext.parent, field) 2535 tag := g.goTag(extDesc, field, wireType) 2536 g.RecordTypeUse(*ext.Extendee) 2537 if n := ext.FieldDescriptorProto.TypeName; n != nil { 2538 // foreign extension type 2539 g.RecordTypeUse(*n) 2540 } 2541 2542 typeName := ext.TypeName() 2543 2544 // Special case for proto2 message sets: If this extension is extending 2545 // proto2.bridge.MessageSet, and its final name component is "message_set_extension", 2546 // then drop that last component. 2547 // 2548 // TODO: This should be implemented in the text formatter rather than the generator. 2549 // In addition, the situation for when to apply this special case is implemented 2550 // differently in other languages: 2551 // https://github.com/google/protobuf/blob/aff10976/src/google/protobuf/text_format.cc#L1560 2552 if extDesc.GetOptions().GetMessageSetWireFormat() && typeName[len(typeName)-1] == "message_set_extension" { 2553 typeName = typeName[:len(typeName)-1] 2554 } 2555 2556 // For text formatting, the package must be exactly what the .proto file declares, 2557 // ignoring overrides such as the go_package option, and with no dot/underscore mapping. 2558 extName := strings.Join(typeName, ".") 2559 if g.file.Package != nil { 2560 extName = *g.file.Package + "." + extName 2561 } 2562 2563 g.P("var ", ccTypeName, " = &", g.Pkg["proto"], ".ExtensionDesc{") 2564 g.P("ExtendedType: (", extendedType, ")(nil),") 2565 g.P("ExtensionType: (", fieldType, ")(nil),") 2566 g.P("Field: ", field.Number, ",") 2567 g.P(`Name: "`, extName, `",`) 2568 g.P("Tag: ", tag, ",") 2569 g.P(`Filename: "`, g.file.GetName(), `",`) 2570 2571 g.P("}") 2572 g.P() 2573 2574 g.addInitf("%s.RegisterExtension(%s)", g.Pkg["proto"], ext.DescName()) 2575 2576 g.file.addExport(ext, constOrVarSymbol{ccTypeName, "var", ""}) 2577 } 2578 2579 func (g *Generator) generateInitFunction() { 2580 if len(g.init) == 0 { 2581 return 2582 } 2583 g.P("func init() {") 2584 for _, l := range g.init { 2585 g.P(l) 2586 } 2587 g.P("}") 2588 g.init = nil 2589 } 2590 2591 func (g *Generator) generateFileDescriptor(file *FileDescriptor) { 2592 // Make a copy and trim source_code_info data. 2593 // TODO: Trim this more when we know exactly what we need. 2594 pb := proto.Clone(file.FileDescriptorProto).(*descriptor.FileDescriptorProto) 2595 pb.SourceCodeInfo = nil 2596 2597 b, err := proto.Marshal(pb) 2598 if err != nil { 2599 g.Fail(err.Error()) 2600 } 2601 2602 var buf bytes.Buffer 2603 w, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression) 2604 w.Write(b) 2605 w.Close() 2606 b = buf.Bytes() 2607 2608 v := file.VarName() 2609 g.P() 2610 g.P("func init() { ", g.Pkg["proto"], ".RegisterFile(", strconv.Quote(*file.Name), ", ", v, ") }") 2611 g.P("var ", v, " = []byte{") 2612 g.P("// ", len(b), " bytes of a gzipped FileDescriptorProto") 2613 for len(b) > 0 { 2614 n := 16 2615 if n > len(b) { 2616 n = len(b) 2617 } 2618 2619 s := "" 2620 for _, c := range b[:n] { 2621 s += fmt.Sprintf("0x%02x,", c) 2622 } 2623 g.P(s) 2624 2625 b = b[n:] 2626 } 2627 g.P("}") 2628 } 2629 2630 func (g *Generator) generateEnumRegistration(enum *EnumDescriptor) { 2631 // // We always print the full (proto-world) package name here. 2632 pkg := enum.File().GetPackage() 2633 if pkg != "" { 2634 pkg += "." 2635 } 2636 // The full type name 2637 typeName := enum.TypeName() 2638 // The full type name, CamelCased. 2639 ccTypeName := CamelCaseSlice(typeName) 2640 g.addInitf("%s.RegisterEnum(%q, %[3]s_name, %[3]s_value)", g.Pkg["proto"], pkg+ccTypeName, ccTypeName) 2641 } 2642 2643 // And now lots of helper functions. 2644 2645 // Is c an ASCII lower-case letter? 2646 func isASCIILower(c byte) bool { 2647 return 'a' <= c && c <= 'z' 2648 } 2649 2650 // Is c an ASCII digit? 2651 func isASCIIDigit(c byte) bool { 2652 return '0' <= c && c <= '9' 2653 } 2654 2655 // CamelCase returns the CamelCased name. 2656 // If there is an interior underscore followed by a lower case letter, 2657 // drop the underscore and convert the letter to upper case. 2658 // There is a remote possibility of this rewrite causing a name collision, 2659 // but it's so remote we're prepared to pretend it's nonexistent - since the 2660 // C++ generator lowercases names, it's extremely unlikely to have two fields 2661 // with different capitalizations. 2662 // In short, _my_field_name_2 becomes XMyFieldName_2. 2663 func CamelCase(s string) string { 2664 if s == "" { 2665 return "" 2666 } 2667 t := make([]byte, 0, 32) 2668 i := 0 2669 if s[0] == '_' { 2670 // Need a capital letter; drop the '_'. 2671 t = append(t, 'X') 2672 i++ 2673 } 2674 // Invariant: if the next letter is lower case, it must be converted 2675 // to upper case. 2676 // That is, we process a word at a time, where words are marked by _ or 2677 // upper case letter. Digits are treated as words. 2678 for ; i < len(s); i++ { 2679 c := s[i] 2680 if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) { 2681 continue // Skip the underscore in s. 2682 } 2683 if isASCIIDigit(c) { 2684 t = append(t, c) 2685 continue 2686 } 2687 // Assume we have a letter now - if not, it's a bogus identifier. 2688 // The next word is a sequence of characters that must start upper case. 2689 if isASCIILower(c) { 2690 c ^= ' ' // Make it a capital letter. 2691 } 2692 t = append(t, c) // Guaranteed not lower case. 2693 // Accept lower case sequence that follows. 2694 for i+1 < len(s) && isASCIILower(s[i+1]) { 2695 i++ 2696 t = append(t, s[i]) 2697 } 2698 } 2699 return string(t) 2700 } 2701 2702 // CamelCaseSlice is like CamelCase, but the argument is a slice of strings to 2703 // be joined with "_". 2704 func CamelCaseSlice(elem []string) string { return CamelCase(strings.Join(elem, "_")) } 2705 2706 // dottedSlice turns a sliced name into a dotted name. 2707 func dottedSlice(elem []string) string { return strings.Join(elem, ".") } 2708 2709 // Is this field optional? 2710 func isOptional(field *descriptor.FieldDescriptorProto) bool { 2711 return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_OPTIONAL 2712 } 2713 2714 // Is this field required? 2715 func isRequired(field *descriptor.FieldDescriptorProto) bool { 2716 return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_REQUIRED 2717 } 2718 2719 // Is this field repeated? 2720 func isRepeated(field *descriptor.FieldDescriptorProto) bool { 2721 return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED 2722 } 2723 2724 // Is this field a scalar numeric type? 2725 func isScalar(field *descriptor.FieldDescriptorProto) bool { 2726 if field.Type == nil { 2727 return false 2728 } 2729 switch *field.Type { 2730 case descriptor.FieldDescriptorProto_TYPE_DOUBLE, 2731 descriptor.FieldDescriptorProto_TYPE_FLOAT, 2732 descriptor.FieldDescriptorProto_TYPE_INT64, 2733 descriptor.FieldDescriptorProto_TYPE_UINT64, 2734 descriptor.FieldDescriptorProto_TYPE_INT32, 2735 descriptor.FieldDescriptorProto_TYPE_FIXED64, 2736 descriptor.FieldDescriptorProto_TYPE_FIXED32, 2737 descriptor.FieldDescriptorProto_TYPE_BOOL, 2738 descriptor.FieldDescriptorProto_TYPE_UINT32, 2739 descriptor.FieldDescriptorProto_TYPE_ENUM, 2740 descriptor.FieldDescriptorProto_TYPE_SFIXED32, 2741 descriptor.FieldDescriptorProto_TYPE_SFIXED64, 2742 descriptor.FieldDescriptorProto_TYPE_SINT32, 2743 descriptor.FieldDescriptorProto_TYPE_SINT64: 2744 return true 2745 default: 2746 return false 2747 } 2748 } 2749 2750 // badToUnderscore is the mapping function used to generate Go names from package names, 2751 // which can be dotted in the input .proto file. It replaces non-identifier characters such as 2752 // dot or dash with underscore. 2753 func badToUnderscore(r rune) rune { 2754 if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' { 2755 return r 2756 } 2757 return '_' 2758 } 2759 2760 // baseName returns the last path element of the name, with the last dotted suffix removed. 2761 func baseName(name string) string { 2762 // First, find the last element 2763 if i := strings.LastIndex(name, "/"); i >= 0 { 2764 name = name[i+1:] 2765 } 2766 // Now drop the suffix 2767 if i := strings.LastIndex(name, "."); i >= 0 { 2768 name = name[0:i] 2769 } 2770 return name 2771 } 2772 2773 // The SourceCodeInfo message describes the location of elements of a parsed 2774 // .proto file by way of a "path", which is a sequence of integers that 2775 // describe the route from a FileDescriptorProto to the relevant submessage. 2776 // The path alternates between a field number of a repeated field, and an index 2777 // into that repeated field. The constants below define the field numbers that 2778 // are used. 2779 // 2780 // See descriptor.proto for more information about this. 2781 const ( 2782 // tag numbers in FileDescriptorProto 2783 packagePath = 2 // package 2784 messagePath = 4 // message_type 2785 enumPath = 5 // enum_type 2786 // tag numbers in DescriptorProto 2787 messageFieldPath = 2 // field 2788 messageMessagePath = 3 // nested_type 2789 messageEnumPath = 4 // enum_type 2790 messageOneofPath = 8 // oneof_decl 2791 // tag numbers in EnumDescriptorProto 2792 enumValuePath = 2 // value 2793 ) 2794 2795 var supportTypeAliases bool 2796 2797 func init() { 2798 for _, tag := range build.Default.ReleaseTags { 2799 if tag == "go1.9" { 2800 supportTypeAliases = true 2801 return 2802 } 2803 } 2804 }