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