generator.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 Google Inc. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  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. The code generator for the plugin for the Google protocol buffer compiler.
  33. It generates Go code from the protocol buffer description files read by the
  34. main routine.
  35. */
  36. package generator
  37. import (
  38. "bytes"
  39. "fmt"
  40. "log"
  41. "os"
  42. "path"
  43. "strings"
  44. "goprotobuf.googlecode.com/hg/proto"
  45. plugin "goprotobuf.googlecode.com/hg/compiler/plugin"
  46. descriptor "goprotobuf.googlecode.com/hg/compiler/descriptor"
  47. )
  48. // A Plugin provides functionality to add to the output during Go code generation,
  49. // such as to produce RPC stubs.
  50. type Plugin interface {
  51. // Name identifies the plugin.
  52. Name() string
  53. // Init is called once after data structures are built but before
  54. // code generation begins.
  55. Init(g *Generator)
  56. // Generate produces the code generated by the plugin for this file,
  57. // except for the imports, by calling the generator's methods P, In, and Out.
  58. Generate(file *FileDescriptor)
  59. // GenerateImports produces the import declarations for this file.
  60. // It is called after Generate.
  61. GenerateImports(file *FileDescriptor)
  62. }
  63. var plugins []Plugin
  64. // RegisterPlugin installs a (second-order) plugin to be run when the Go output is generated.
  65. // It is typically called during initialization.
  66. func RegisterPlugin(p Plugin) {
  67. n := len(plugins)
  68. if cap(plugins) == n {
  69. nplugins := make([]Plugin, n, n+10) // very unlikely to need more than this
  70. copy(nplugins, plugins)
  71. plugins = nplugins
  72. }
  73. plugins = plugins[0 : n+1]
  74. plugins[n] = p
  75. }
  76. // Each type we import as a protocol buffer (other than FileDescriptorProto) needs
  77. // a pointer to the FileDescriptorProto that represents it. These types achieve that
  78. // wrapping by placing each Proto inside a struct with the pointer to its File. The
  79. // structs have the same names as their contents, with "Proto" removed.
  80. // FileDescriptor is used to store the things that it points to.
  81. // The file and package name method are common to messages and enums.
  82. type common struct {
  83. File *descriptor.FileDescriptorProto // File this object comes from.
  84. }
  85. // PackageName is name in the package clause in the generated file.
  86. func (c *common) PackageName() string { return uniquePackageOf(c.File) }
  87. // Descriptor represents a protocol buffer message.
  88. type Descriptor struct {
  89. common
  90. *descriptor.DescriptorProto
  91. parent *Descriptor // The containing message, if any.
  92. nested []*Descriptor // Inner messages, if any.
  93. ext []*ExtensionDescriptor // Extensions, if any.
  94. typename []string // Cached typename vector.
  95. }
  96. // TypeName returns the elements of the dotted type name.
  97. // The package name is not part of this name.
  98. func (d *Descriptor) TypeName() []string {
  99. if d.typename != nil {
  100. return d.typename
  101. }
  102. n := 0
  103. for parent := d; parent != nil; parent = parent.parent {
  104. n++
  105. }
  106. s := make([]string, n, n)
  107. for parent := d; parent != nil; parent = parent.parent {
  108. n--
  109. s[n] = proto.GetString(parent.Name)
  110. }
  111. d.typename = s
  112. return s
  113. }
  114. // EnumDescriptor describes an enum. If it's at top level, its parent will be nil.
  115. // Otherwise it will be the descriptor of the message in which it is defined.
  116. type EnumDescriptor struct {
  117. common
  118. *descriptor.EnumDescriptorProto
  119. parent *Descriptor // The containing message, if any.
  120. typename []string // Cached typename vector.
  121. }
  122. // TypeName returns the elements of the dotted type name.
  123. // The package name is not part of this name.
  124. func (e *EnumDescriptor) TypeName() (s []string) {
  125. if e.typename != nil {
  126. return e.typename
  127. }
  128. name := proto.GetString(e.Name)
  129. if e.parent == nil {
  130. s = make([]string, 1)
  131. } else {
  132. pname := e.parent.TypeName()
  133. s = make([]string, len(pname)+1)
  134. copy(s, pname)
  135. }
  136. s[len(s)-1] = name
  137. e.typename = s
  138. return s
  139. }
  140. // Everything but the last element of the full type name, CamelCased.
  141. // The values of type Foo.Bar are call Foo_value1... not Foo_Bar_value1... .
  142. func (e *EnumDescriptor) prefix() string {
  143. typeName := e.TypeName()
  144. ccPrefix := CamelCaseSlice(typeName[0:len(typeName)-1]) + "_"
  145. if e.parent == nil {
  146. // If the enum is not part of a message, the prefix is just the type name.
  147. ccPrefix = CamelCase(*e.Name) + "_"
  148. }
  149. return ccPrefix
  150. }
  151. // The integer value of the named constant in this enumerated type.
  152. func (e *EnumDescriptor) integerValueAsString(name string) string {
  153. for _, c := range e.Value {
  154. if proto.GetString(c.Name) == name {
  155. return fmt.Sprint(proto.GetInt32(c.Number))
  156. }
  157. }
  158. log.Exit("cannot find value for enum constant")
  159. return ""
  160. }
  161. // ExtensionDescriptor desribes an extension. If it's at top level, its parent will be nil.
  162. // Otherwise it will be the descriptor of the message in which it is defined.
  163. type ExtensionDescriptor struct {
  164. common
  165. *descriptor.FieldDescriptorProto
  166. parent *Descriptor // The containing message, if any.
  167. }
  168. // TypeName returns the elements of the dotted type name.
  169. // The package name is not part of this name.
  170. func (e *ExtensionDescriptor) TypeName() (s []string) {
  171. name := proto.GetString(e.Name)
  172. if e.parent == nil {
  173. // top-level extension
  174. s = make([]string, 1)
  175. } else {
  176. pname := e.parent.TypeName()
  177. s = make([]string, len(pname)+1)
  178. copy(s, pname)
  179. }
  180. s[len(s)-1] = name
  181. return s
  182. }
  183. // FileDescriptor describes an protocol buffer descriptor file (.proto).
  184. // It includes slices of all the messages and enums defined within it.
  185. // Those slices are constructed by WrapTypes.
  186. type FileDescriptor struct {
  187. *descriptor.FileDescriptorProto
  188. desc []*Descriptor // All the messages defined in this file.
  189. enum []*EnumDescriptor // All the enums defined in this file.
  190. ext []*ExtensionDescriptor // All the top-level extensions defined in this file.
  191. }
  192. // PackageName is the package name we'll use in the generated code to refer to this file.
  193. func (d *FileDescriptor) PackageName() string { return uniquePackageOf(d.FileDescriptorProto) }
  194. // The package named defined in the input for this file, possibly dotted.
  195. // If the file does not define a package, use the base of the file name.
  196. func (d *FileDescriptor) originalPackageName() string {
  197. // Does the file have a package clause?
  198. pkg := proto.GetString(d.Package)
  199. if pkg != "" {
  200. return pkg
  201. }
  202. // Use the file base name.
  203. return BaseName(proto.GetString(d.Name))
  204. }
  205. // Object is an interface abstracting the abilities shared by enums and messages.
  206. type Object interface {
  207. PackageName() string // The name we use in our output (a_b_c), possibly renamed for uniqueness.
  208. TypeName() []string
  209. }
  210. // Each package name we generate must be unique. The package we're generating
  211. // gets its own name but every other package must have a unqiue name that does
  212. // not conflict in the code we generate. These names are chosen globally (although
  213. // they don't have to be, it simplifies things to do them globally).
  214. func uniquePackageOf(fd *descriptor.FileDescriptorProto) string {
  215. s, ok := uniquePackageName[fd]
  216. if !ok {
  217. log.Exit("internal error: no package name defined for", proto.GetString(fd.Name))
  218. }
  219. return s
  220. }
  221. // Generator is the type whose methods generate the output, stored in the associated response structure.
  222. type Generator struct {
  223. *bytes.Buffer
  224. Request *plugin.CodeGeneratorRequest // The input.
  225. Response *plugin.CodeGeneratorResponse // The output.
  226. Param map[string]string // Command-line parameters.
  227. ImportPrefix string // String to prefix to imported package file names.
  228. ImportMap map[string]string // Mapping from import name to generated name
  229. ProtoPkg string // The name under which we import the library's package proto.
  230. packageName string // What we're calling ourselves.
  231. allFiles []*FileDescriptor // All files in the tree
  232. genFiles []*FileDescriptor // Those files we will generate output for.
  233. file *FileDescriptor // The file we are compiling now.
  234. usedPackages map[string]bool // Names of packages used in current file.
  235. typeNameToObject map[string]Object // Key is a fully-qualified name in input syntax.
  236. indent string
  237. }
  238. // New creates a new generator and allocates the request and response protobufs.
  239. func New() *Generator {
  240. g := new(Generator)
  241. g.Buffer = new(bytes.Buffer)
  242. g.Request = plugin.NewCodeGeneratorRequest()
  243. g.Response = plugin.NewCodeGeneratorResponse()
  244. return g
  245. }
  246. // Error reports a problem, including an os.Error, and exits the program.
  247. func (g *Generator) Error(err os.Error, msgs ...string) {
  248. s := strings.Join(msgs, " ") + ":" + err.String()
  249. log.Stderr("protoc-gen-go: error: ", s)
  250. g.Response.Error = proto.String(s)
  251. os.Exit(1)
  252. }
  253. // Fail reports a problem and exits the program.
  254. func (g *Generator) Fail(msgs ...string) {
  255. s := strings.Join(msgs, " ")
  256. log.Stderr("protoc-gen-go: error: ", s)
  257. g.Response.Error = proto.String(s)
  258. os.Exit(1)
  259. }
  260. // CommandLineParameters breaks the comma-separated list of key=value pairs
  261. // in the parameter (a member of the request protobuf) into a key/value map.
  262. // It then sets file name mappings defined by those entries.
  263. func (g *Generator) CommandLineParameters(parameter string) {
  264. g.Param = make(map[string]string)
  265. for _, p := range strings.Split(parameter, ",", -1) {
  266. if i := strings.Index(p, "="); i < 0 {
  267. g.Param[p] = ""
  268. } else {
  269. g.Param[p[0:i]] = p[i+1:]
  270. }
  271. }
  272. g.ImportMap = make(map[string]string)
  273. for k, v := range g.Param {
  274. if k == "import_prefix" {
  275. g.ImportPrefix = v
  276. } else if len(k) > 0 && k[0] == 'M' {
  277. g.ImportMap[k[1:]] = v
  278. }
  279. }
  280. }
  281. // DefaultPackageName returns the package name printed for the object.
  282. // If its file is in a different package, it returns the package name we're using for this file, plus ".".
  283. // Otherwise it returns the empty string.
  284. func (g *Generator) DefaultPackageName(obj Object) string {
  285. pkg := obj.PackageName()
  286. if pkg == g.packageName {
  287. return ""
  288. }
  289. return pkg + "."
  290. }
  291. // For each input file, the unique package name to use, underscored.
  292. var uniquePackageName = make(map[*descriptor.FileDescriptorProto]string)
  293. // Package names already registered. Key is the name from the .proto file;
  294. // value is the name that appears in the generated code.
  295. var pkgNamesInUse = make(map[string]bool)
  296. // Create and remember a guaranteed unique package name for this file descriptor.
  297. // Pkg is the candidate name. If f is nil, it's a builtin package like "proto" and
  298. // has no file descriptor.
  299. func RegisterUniquePackageName(pkg string, f *FileDescriptor) string {
  300. for pkgNamesInUse[pkg] {
  301. // It's a duplicate; must rename.
  302. pkg += "X"
  303. }
  304. // Install it.
  305. pkgNamesInUse[pkg] = true
  306. pkg = strings.Map(DotToUnderscore, pkg)
  307. if f != nil {
  308. uniquePackageName[f.FileDescriptorProto] = pkg
  309. }
  310. return pkg
  311. }
  312. // SetPackageNames sets the package name for this run.
  313. // The package name must agree across all files being generated.
  314. // It also defines unique package names for all imported files.
  315. func (g *Generator) SetPackageNames() {
  316. // Register the name for this package. It will be the first name
  317. // registered so is guaranteed to be unmodified.
  318. pkg := g.genFiles[0].originalPackageName()
  319. g.packageName = RegisterUniquePackageName(pkg, g.genFiles[0])
  320. // Register the proto package name. It might collide with the
  321. // name of a package we import.
  322. g.ProtoPkg = RegisterUniquePackageName("proto", nil)
  323. for _, f := range g.genFiles {
  324. thisPkg := f.originalPackageName()
  325. if thisPkg != pkg {
  326. g.Fail("inconsistent package names:", thisPkg, pkg)
  327. }
  328. }
  329. AllFiles:
  330. for _, f := range g.allFiles {
  331. for _, genf := range g.genFiles {
  332. if f == genf {
  333. // In this package already.
  334. uniquePackageName[f.FileDescriptorProto] = g.packageName
  335. continue AllFiles
  336. }
  337. }
  338. RegisterUniquePackageName(f.originalPackageName(), f)
  339. }
  340. }
  341. // WrapTypes walks the incoming data, wrapping DescriptorProtos, EnumDescriptorProtos
  342. // and FileDescriptorProtos into file-referenced objects within the Generator.
  343. // It also creates the list of files to generate and so should be called before GenerateAllFiles.
  344. func (g *Generator) WrapTypes() {
  345. g.allFiles = make([]*FileDescriptor, len(g.Request.ProtoFile))
  346. for i, f := range g.Request.ProtoFile {
  347. // We must wrap the descriptors before we wrap the enums
  348. descs := wrapDescriptors(f)
  349. g.buildNestedDescriptors(descs)
  350. enums := wrapEnumDescriptors(f, descs)
  351. exts := wrapExtensions(f)
  352. g.allFiles[i] = &FileDescriptor{
  353. FileDescriptorProto: f,
  354. desc: descs,
  355. enum: enums,
  356. ext: exts,
  357. }
  358. }
  359. g.genFiles = make([]*FileDescriptor, len(g.Request.FileToGenerate))
  360. FindFiles:
  361. for i, fileName := range g.Request.FileToGenerate {
  362. // Search the list. This algorithm is n^2 but n is tiny.
  363. for _, file := range g.allFiles {
  364. if fileName == proto.GetString(file.Name) {
  365. g.genFiles[i] = file
  366. continue FindFiles
  367. }
  368. }
  369. g.Fail("could not find file named", fileName)
  370. }
  371. g.Response.File = make([]*plugin.CodeGeneratorResponse_File, len(g.genFiles))
  372. }
  373. // Scan the descriptors in this file. For each one, build the slice of nested descriptors
  374. func (g *Generator) buildNestedDescriptors(descs []*Descriptor) {
  375. for _, desc := range descs {
  376. if len(desc.NestedType) != 0 {
  377. desc.nested = make([]*Descriptor, len(desc.NestedType))
  378. n := 0
  379. for _, nest := range descs {
  380. if nest.parent == desc {
  381. desc.nested[n] = nest
  382. n++
  383. }
  384. }
  385. if n != len(desc.NestedType) {
  386. g.Fail("internal error: nesting failure for", proto.GetString(desc.Name))
  387. }
  388. }
  389. }
  390. }
  391. // Construct the Descriptor and add it to the slice
  392. func addDescriptor(sl []*Descriptor, desc *descriptor.DescriptorProto, parent *Descriptor, file *descriptor.FileDescriptorProto) []*Descriptor {
  393. d := &Descriptor{common{File: file}, desc, parent, nil, nil, nil}
  394. d.ext = make([]*ExtensionDescriptor, len(desc.Extension))
  395. for i, field := range desc.Extension {
  396. d.ext[i] = &ExtensionDescriptor{common{File: file}, field, d}
  397. }
  398. if len(sl) == cap(sl) {
  399. nsl := make([]*Descriptor, len(sl), 2*len(sl))
  400. copy(nsl, sl)
  401. sl = nsl
  402. }
  403. sl = sl[0 : len(sl)+1]
  404. sl[len(sl)-1] = d
  405. return sl
  406. }
  407. // Return a slice of all the Descriptors defined within this file
  408. func wrapDescriptors(file *descriptor.FileDescriptorProto) []*Descriptor {
  409. sl := make([]*Descriptor, 0, len(file.MessageType)+10)
  410. for _, desc := range file.MessageType {
  411. sl = wrapThisDescriptor(sl, desc, nil, file)
  412. }
  413. return sl
  414. }
  415. // Wrap this Descriptor, recursively
  416. func wrapThisDescriptor(sl []*Descriptor, desc *descriptor.DescriptorProto, parent *Descriptor, file *descriptor.FileDescriptorProto) []*Descriptor {
  417. sl = addDescriptor(sl, desc, parent, file)
  418. me := sl[len(sl)-1]
  419. for _, nested := range desc.NestedType {
  420. sl = wrapThisDescriptor(sl, nested, me, file)
  421. }
  422. return sl
  423. }
  424. // Construct the EnumDescriptor and add it to the slice
  425. func addEnumDescriptor(sl []*EnumDescriptor, desc *descriptor.EnumDescriptorProto, parent *Descriptor, file *descriptor.FileDescriptorProto) []*EnumDescriptor {
  426. if len(sl) == cap(sl) {
  427. nsl := make([]*EnumDescriptor, len(sl), 2*len(sl))
  428. copy(nsl, sl)
  429. sl = nsl
  430. }
  431. sl = sl[0 : len(sl)+1]
  432. sl[len(sl)-1] = &EnumDescriptor{common{File: file}, desc, parent, nil}
  433. return sl
  434. }
  435. // Return a slice of all the EnumDescriptors defined within this file
  436. func wrapEnumDescriptors(file *descriptor.FileDescriptorProto, descs []*Descriptor) []*EnumDescriptor {
  437. sl := make([]*EnumDescriptor, 0, len(file.EnumType)+10)
  438. // Top-level enums.
  439. for _, enum := range file.EnumType {
  440. sl = addEnumDescriptor(sl, enum, nil, file)
  441. }
  442. // Enums within messages. Enums within embedded messages appear in the outer-most message.
  443. for _, nested := range descs {
  444. for _, enum := range nested.EnumType {
  445. sl = addEnumDescriptor(sl, enum, nested, file)
  446. }
  447. }
  448. return sl
  449. }
  450. // Return a slice of all the top-level ExtensionDescriptors defined within this file.
  451. func wrapExtensions(file *descriptor.FileDescriptorProto) []*ExtensionDescriptor {
  452. sl := make([]*ExtensionDescriptor, len(file.Extension))
  453. for i, field := range file.Extension {
  454. sl[i] = &ExtensionDescriptor{common{File: file}, field, nil}
  455. }
  456. return sl
  457. }
  458. // BuildTypeNameMap builds the map from fully qualified type names to objects.
  459. // The key names for the map come from the input data, which puts a period at the beginning.
  460. // It should be called after SetPackageNames and before GenerateAllFiles.
  461. func (g *Generator) BuildTypeNameMap() {
  462. g.typeNameToObject = make(map[string]Object)
  463. for _, f := range g.allFiles {
  464. // The names in this loop are defined by the proto world, not us, so the
  465. // package name may be empty. If so, the dotted package name of X will
  466. // be ".X"; otherwise it will be ".pkg.X".
  467. dottedPkg := "." + proto.GetString(f.Package)
  468. if dottedPkg != "." {
  469. dottedPkg += "."
  470. }
  471. for _, enum := range f.enum {
  472. name := dottedPkg + dottedSlice(enum.TypeName())
  473. g.typeNameToObject[name] = enum
  474. }
  475. for _, desc := range f.desc {
  476. name := dottedPkg + dottedSlice(desc.TypeName())
  477. g.typeNameToObject[name] = desc
  478. }
  479. }
  480. }
  481. // ObjectNamed, given a fully-qualified input type name as it appears in the input data,
  482. // returns the descriptor for the message or enum with that name.
  483. func (g *Generator) ObjectNamed(typeName string) Object {
  484. f, ok := g.typeNameToObject[typeName]
  485. if !ok {
  486. g.Fail("can't find object with type", typeName)
  487. }
  488. return f
  489. }
  490. // P prints the arguments to the generated output. It handles strings and int32s, plus
  491. // handling indirections because they may be *string, etc.
  492. func (g *Generator) P(str ...interface{}) {
  493. g.WriteString(g.indent)
  494. for _, v := range str {
  495. switch s := v.(type) {
  496. case string:
  497. g.WriteString(s)
  498. case *string:
  499. g.WriteString(*s)
  500. case bool:
  501. g.WriteString(fmt.Sprintf("%t", s))
  502. case *bool:
  503. g.WriteString(fmt.Sprintf("%t", *s))
  504. case *int32:
  505. g.WriteString(fmt.Sprintf("%d", *s))
  506. case float64:
  507. g.WriteString(fmt.Sprintf("%g", s))
  508. case *float64:
  509. g.WriteString(fmt.Sprintf("%g", *s))
  510. default:
  511. g.Fail(fmt.Sprintf("unknown type in printer: %T", v))
  512. }
  513. }
  514. g.WriteByte('\n')
  515. }
  516. // In Indents the output one tab stop.
  517. func (g *Generator) In() { g.indent += "\t" }
  518. // Out unindents the output one tab stop.
  519. func (g *Generator) Out() {
  520. if len(g.indent) > 0 {
  521. g.indent = g.indent[1:]
  522. }
  523. }
  524. // GenerateAllFiles generates the output for all the files we're outputting.
  525. func (g *Generator) GenerateAllFiles() {
  526. // Initialize the plugins
  527. for _, p := range plugins {
  528. p.Init(g)
  529. }
  530. // Generate the output.
  531. for i, file := range g.genFiles {
  532. g.Reset()
  533. g.generate(file)
  534. g.Response.File[i] = plugin.NewCodeGeneratorResponse_File()
  535. g.Response.File[i].Name = proto.String(goFileName(*file.Name))
  536. g.Response.File[i].Content = proto.String(g.String())
  537. }
  538. }
  539. // Run all the plugins associated with the file.
  540. func (g *Generator) runPlugins(file *FileDescriptor) {
  541. for _, p := range plugins {
  542. p.Generate(file)
  543. }
  544. }
  545. // FileOf return the FileDescriptor for this FileDescriptorProto.
  546. func (g *Generator) FileOf(fd *descriptor.FileDescriptorProto) *FileDescriptor {
  547. for _, file := range g.allFiles {
  548. if file.FileDescriptorProto == fd {
  549. return file
  550. }
  551. }
  552. g.Fail("could not find file in table:", proto.GetString(fd.Name))
  553. return nil
  554. }
  555. // Fill the response protocol buffer with the generated output for all the files we're
  556. // supposed to generate.
  557. func (g *Generator) generate(file *FileDescriptor) {
  558. g.file = g.FileOf(file.FileDescriptorProto)
  559. g.usedPackages = make(map[string]bool)
  560. for _, enum := range g.file.enum {
  561. g.generateEnum(enum)
  562. }
  563. for _, desc := range g.file.desc {
  564. g.generateMessage(desc)
  565. }
  566. for _, ext := range g.file.ext {
  567. g.generateExtension(ext)
  568. }
  569. g.generateInitFunction()
  570. // Run the plugins before the imports so we know which imports are necessary.
  571. g.runPlugins(file)
  572. // Generate header and imports last, though they appear first in the output.
  573. rem := g.Buffer
  574. g.Buffer = new(bytes.Buffer)
  575. g.generateHeader()
  576. g.generateImports()
  577. g.Write(rem.Bytes())
  578. }
  579. // Generate the header, including package definition and imports
  580. func (g *Generator) generateHeader() {
  581. g.P("// Code generated by protoc-gen-go from ", Quote(*g.file.Name))
  582. g.P("// DO NOT EDIT!")
  583. g.P()
  584. g.P("package ", g.file.PackageName())
  585. g.P()
  586. }
  587. // Generate the header, including package definition and imports
  588. func (g *Generator) generateImports() {
  589. // We almost always need a proto import. Rather than computing when we
  590. // do, which is tricky when there's a plugin, just import it and
  591. // reference it later.
  592. g.P("import " + g.ProtoPkg + " " + Quote(g.ImportPrefix+"goprotobuf.googlecode.com/hg/proto"))
  593. for _, s := range g.file.Dependency {
  594. // Need to find the descriptor for this file
  595. for _, fd := range g.allFiles {
  596. // Do not import our own package.
  597. if fd.PackageName() == g.packageName {
  598. continue
  599. }
  600. if proto.GetString(fd.Name) == s {
  601. filename := goFileName(s)
  602. if substitution, ok := g.ImportMap[s]; ok {
  603. filename = substitution
  604. }
  605. filename = g.ImportPrefix + filename
  606. if strings.HasSuffix(filename, ".go") {
  607. filename = filename[0 : len(filename)-3]
  608. }
  609. if _, ok := g.usedPackages[fd.PackageName()]; ok {
  610. g.P("import ", fd.PackageName(), " ", Quote(filename))
  611. } else {
  612. log.Stderr("protoc-gen-go: discarding unused import: ", filename)
  613. }
  614. break
  615. }
  616. }
  617. }
  618. g.P()
  619. // TODO: may need to worry about uniqueness across plugins
  620. for _, p := range plugins {
  621. p.GenerateImports(g.file)
  622. g.P()
  623. }
  624. g.P("// Reference proto import to suppress error if it's not otherwise used.")
  625. g.P("var _ = ", g.ProtoPkg, ".GetString")
  626. g.P()
  627. }
  628. // Generate the enum definitions for this EnumDescriptor.
  629. func (g *Generator) generateEnum(enum *EnumDescriptor) {
  630. // The full type name
  631. typeName := enum.TypeName()
  632. // The full type name, CamelCased.
  633. ccTypeName := CamelCaseSlice(typeName)
  634. ccPrefix := enum.prefix()
  635. g.P("type ", ccTypeName, " int32")
  636. g.P("const (")
  637. g.In()
  638. for _, e := range enum.Value {
  639. g.P(ccPrefix+*e.Name, " = ", e.Number)
  640. }
  641. g.Out()
  642. g.P(")")
  643. g.P("var ", ccTypeName, "_name = map[int32] string {")
  644. g.In()
  645. generated := make(map[int32]bool) // avoid duplicate values
  646. for _, e := range enum.Value {
  647. duplicate := ""
  648. if _, present := generated[*e.Number]; present {
  649. duplicate = "// Duplicate value: "
  650. }
  651. g.P(duplicate, e.Number, ": ", Quote(*e.Name), ",")
  652. generated[*e.Number] = true
  653. }
  654. g.Out()
  655. g.P("}")
  656. g.P("var ", ccTypeName, "_value = map[string] int32 {")
  657. g.In()
  658. for _, e := range enum.Value {
  659. g.P(Quote(*e.Name), ": ", e.Number, ",")
  660. }
  661. g.Out()
  662. g.P("}")
  663. g.P("func New", ccTypeName, "(x int32) *", ccTypeName, " {")
  664. g.In()
  665. g.P("e := ", ccTypeName, "(x)")
  666. g.P("return &e")
  667. g.Out()
  668. g.P("}")
  669. g.P()
  670. }
  671. // The tag is a string like "PB(varint,2,opt,name=fieldname,def=7)" that
  672. // identifies details of the field for the protocol buffer marshaling and unmarshaling
  673. // code. The fields are:
  674. // wire encoding
  675. // protocol tag number
  676. // opt,req,rep for optional, required, or repeated
  677. // name= the original declared name
  678. // enum= the name of the enum type if it is an enum-typed field.
  679. // def= string representation of the default value, if any.
  680. // The default value must be in a representation that can be used at run-time
  681. // to generate the default value. Thus bools become 0 and 1, for instance.
  682. func (g *Generator) goTag(field *descriptor.FieldDescriptorProto, wiretype string) string {
  683. optrepreq := ""
  684. switch {
  685. case isOptional(field):
  686. optrepreq = "opt"
  687. case isRequired(field):
  688. optrepreq = "req"
  689. case isRepeated(field):
  690. optrepreq = "rep"
  691. }
  692. defaultValue := proto.GetString(field.DefaultValue)
  693. if defaultValue != "" {
  694. switch *field.Type {
  695. case descriptor.FieldDescriptorProto_TYPE_BOOL:
  696. if defaultValue == "true" {
  697. defaultValue = "1"
  698. } else {
  699. defaultValue = "0"
  700. }
  701. case descriptor.FieldDescriptorProto_TYPE_STRING,
  702. descriptor.FieldDescriptorProto_TYPE_BYTES:
  703. // Protect frogs.
  704. defaultValue = Quote(defaultValue)
  705. // Don't need the quotes
  706. defaultValue = defaultValue[1 : len(defaultValue)-1]
  707. case descriptor.FieldDescriptorProto_TYPE_ENUM:
  708. // For enums we need to provide the integer constant.
  709. obj := g.ObjectNamed(proto.GetString(field.TypeName))
  710. enum, ok := obj.(*EnumDescriptor)
  711. if !ok {
  712. g.Fail("enum type inconsistent for", CamelCaseSlice(obj.TypeName()))
  713. }
  714. defaultValue = enum.integerValueAsString(defaultValue)
  715. }
  716. defaultValue = ",def=" + defaultValue
  717. }
  718. enum := ""
  719. if *field.Type == descriptor.FieldDescriptorProto_TYPE_ENUM {
  720. obj := g.ObjectNamed(proto.GetString(field.TypeName))
  721. enum = ",enum=" + obj.PackageName() + "." + CamelCaseSlice(obj.TypeName())
  722. }
  723. name := proto.GetString(field.Name)
  724. if name == CamelCase(name) {
  725. name = ""
  726. } else {
  727. name = ",name=" + name
  728. }
  729. return Quote(fmt.Sprintf("PB(%s,%d,%s%s%s%s)",
  730. wiretype,
  731. proto.GetInt32(field.Number),
  732. optrepreq,
  733. name,
  734. enum,
  735. defaultValue))
  736. }
  737. func needsStar(typ descriptor.FieldDescriptorProto_Type) bool {
  738. switch typ {
  739. case descriptor.FieldDescriptorProto_TYPE_GROUP:
  740. return false
  741. case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
  742. return false
  743. case descriptor.FieldDescriptorProto_TYPE_BYTES:
  744. return false
  745. }
  746. return true
  747. }
  748. // TypeName is the printed name appropriate for an item. If the object is in the current file,
  749. // TypeName drops the package name and underscores the rest.
  750. // Otherwise the object is from another package; and the result is the underscored
  751. // package name followed by the item name.
  752. // The result always has an initial capital.
  753. func (g *Generator) TypeName(obj Object) string {
  754. return g.DefaultPackageName(obj) + CamelCaseSlice(obj.TypeName())
  755. }
  756. // TypeNameWithPackage is like TypeName, but always includes the package
  757. // name even if the object is in our own package.
  758. func (g *Generator) TypeNameWithPackage(obj Object) string {
  759. return obj.PackageName() + CamelCaseSlice(obj.TypeName())
  760. }
  761. // GoType returns a string representing the type name, and the wire type
  762. func (g *Generator) GoType(message *Descriptor, field *descriptor.FieldDescriptorProto) (typ string, wire string) {
  763. // TODO: Options.
  764. switch *field.Type {
  765. case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
  766. typ, wire = "float64", "fixed64"
  767. case descriptor.FieldDescriptorProto_TYPE_FLOAT:
  768. typ, wire = "float32", "fixed32"
  769. case descriptor.FieldDescriptorProto_TYPE_INT64:
  770. typ, wire = "int64", "varint"
  771. case descriptor.FieldDescriptorProto_TYPE_UINT64:
  772. typ, wire = "uint64", "varint"
  773. case descriptor.FieldDescriptorProto_TYPE_INT32:
  774. typ, wire = "int32", "varint"
  775. case descriptor.FieldDescriptorProto_TYPE_UINT32:
  776. typ, wire = "uint32", "varint"
  777. case descriptor.FieldDescriptorProto_TYPE_FIXED64:
  778. typ, wire = "uint64", "fixed64"
  779. case descriptor.FieldDescriptorProto_TYPE_FIXED32:
  780. typ, wire = "uint32", "fixed32"
  781. case descriptor.FieldDescriptorProto_TYPE_BOOL:
  782. typ, wire = "bool", "varint"
  783. case descriptor.FieldDescriptorProto_TYPE_STRING:
  784. typ, wire = "string", "bytes"
  785. case descriptor.FieldDescriptorProto_TYPE_GROUP:
  786. desc := g.ObjectNamed(proto.GetString(field.TypeName))
  787. typ, wire = "*"+g.TypeName(desc), "group"
  788. case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
  789. desc := g.ObjectNamed(proto.GetString(field.TypeName))
  790. typ, wire = "*"+g.TypeName(desc), "bytes"
  791. case descriptor.FieldDescriptorProto_TYPE_BYTES:
  792. typ, wire = "[]byte", "bytes"
  793. case descriptor.FieldDescriptorProto_TYPE_ENUM:
  794. desc := g.ObjectNamed(proto.GetString(field.TypeName))
  795. typ, wire = g.TypeName(desc), "varint"
  796. case descriptor.FieldDescriptorProto_TYPE_SFIXED32:
  797. typ, wire = "int32", "fixed32"
  798. case descriptor.FieldDescriptorProto_TYPE_SFIXED64:
  799. typ, wire = "int64", "fixed64"
  800. case descriptor.FieldDescriptorProto_TYPE_SINT32:
  801. typ, wire = "int32", "zigzag32"
  802. case descriptor.FieldDescriptorProto_TYPE_SINT64:
  803. typ, wire = "int64", "zigzag64"
  804. default:
  805. g.Fail("unknown type for", proto.GetString(field.Name))
  806. }
  807. if isRepeated(field) {
  808. typ = "[]" + typ
  809. } else if needsStar(*field.Type) {
  810. typ = "*" + typ
  811. }
  812. return
  813. }
  814. func (g *Generator) RecordTypeUse(t string) {
  815. if obj, ok := g.typeNameToObject[t]; ok {
  816. g.usedPackages[obj.PackageName()] = true
  817. }
  818. }
  819. // Generate the type and default constant definitions for this Descriptor.
  820. func (g *Generator) generateMessage(message *Descriptor) {
  821. // The full type name
  822. typeName := message.TypeName()
  823. // The full type name, CamelCased.
  824. ccTypeName := CamelCaseSlice(typeName)
  825. g.P("type ", ccTypeName, " struct {")
  826. g.In()
  827. for _, field := range message.Field {
  828. fieldname := CamelCase(*field.Name)
  829. typename, wiretype := g.GoType(message, field)
  830. tag := g.goTag(field, wiretype)
  831. g.P(fieldname, "\t", typename, "\t", tag)
  832. g.RecordTypeUse(proto.GetString(field.TypeName))
  833. }
  834. if len(message.ExtensionRange) > 0 {
  835. g.P("XXX_extensions\t\tmap[int32][]byte")
  836. }
  837. g.P("XXX_unrecognized\t[]byte")
  838. g.Out()
  839. g.P("}")
  840. // Reset function
  841. g.P("func (this *", ccTypeName, ") Reset() {")
  842. g.In()
  843. g.P("*this = ", ccTypeName, "{}")
  844. g.Out()
  845. g.P("}")
  846. // Extension support methods
  847. if len(message.ExtensionRange) > 0 {
  848. g.P()
  849. g.P("var extRange_", ccTypeName, " = []", g.ProtoPkg, ".ExtensionRange{")
  850. g.In()
  851. for _, r := range message.ExtensionRange {
  852. end := fmt.Sprint(*r.End - 1) // make range inclusive on both ends
  853. g.P(g.ProtoPkg+".ExtensionRange{", r.Start, ", ", end, "},")
  854. }
  855. g.Out()
  856. g.P("}")
  857. g.P("func (*", ccTypeName, ") ExtensionRangeArray() []", g.ProtoPkg, ".ExtensionRange {")
  858. g.In()
  859. g.P("return extRange_", ccTypeName)
  860. g.Out()
  861. g.P("}")
  862. g.P("func (this *", ccTypeName, ") ExtensionMap() map[int32][]byte {")
  863. g.In()
  864. g.P("if this.XXX_extensions == nil {")
  865. g.In()
  866. g.P("this.XXX_extensions = make(map[int32][]byte)")
  867. g.Out()
  868. g.P("}")
  869. g.P("return this.XXX_extensions")
  870. g.Out()
  871. g.P("}")
  872. }
  873. // Default constants
  874. for _, field := range message.Field {
  875. def := proto.GetString(field.DefaultValue)
  876. if def == "" {
  877. continue
  878. }
  879. fieldname := "Default_" + ccTypeName + "_" + CamelCase(*field.Name)
  880. typename, _ := g.GoType(message, field)
  881. if typename[0] == '*' {
  882. typename = typename[1:]
  883. }
  884. kind := "const "
  885. switch {
  886. case typename == "bool":
  887. case typename == "string":
  888. def = Quote(def)
  889. case typename == "[]byte":
  890. def = "[]byte(" + Quote(def) + ")"
  891. kind = "var "
  892. case *field.Type == descriptor.FieldDescriptorProto_TYPE_ENUM:
  893. // Must be an enum. Need to construct the prefixed name.
  894. obj := g.ObjectNamed(proto.GetString(field.TypeName))
  895. enum, ok := obj.(*EnumDescriptor)
  896. if !ok {
  897. log.Stderr("don't know how to generate constant for", fieldname)
  898. continue
  899. }
  900. def = g.DefaultPackageName(enum) + enum.prefix() + def
  901. }
  902. g.P(kind, fieldname, " ", typename, " = ", def)
  903. }
  904. g.P()
  905. for _, ext := range message.ext {
  906. g.generateExtension(ext)
  907. }
  908. }
  909. func (g *Generator) generateExtension(ext *ExtensionDescriptor) {
  910. // The full type name
  911. typeName := ext.TypeName()
  912. // Each scope of the extension is individually CamelCased, and all are joined with "_" with an "E_" prefix.
  913. for i, s := range typeName {
  914. typeName[i] = CamelCase(s)
  915. }
  916. ccTypeName := "E_" + strings.Join(typeName, "_")
  917. extendedType := "*" + g.TypeName(g.ObjectNamed(*ext.Extendee))
  918. field := ext.FieldDescriptorProto
  919. fieldType, wireType := g.GoType(ext.parent, field)
  920. tag := g.goTag(field, wireType)
  921. g.RecordTypeUse(*ext.Extendee)
  922. g.P("var ", ccTypeName, " = &", g.ProtoPkg, ".ExtensionDesc{")
  923. g.In()
  924. g.P("ExtendedType: (", extendedType, ")(nil),")
  925. g.P("ExtensionType: (", fieldType, ")(nil),")
  926. g.P("Field: ", field.Number, ",")
  927. g.P("Tag: ", tag, ",")
  928. g.Out()
  929. g.P("}")
  930. g.P()
  931. }
  932. func (g *Generator) generateInitFunction() {
  933. g.P("func init() {")
  934. g.In()
  935. for _, enum := range g.file.enum {
  936. g.generateEnumRegistration(enum)
  937. }
  938. g.Out()
  939. g.P("}")
  940. }
  941. func (g *Generator) generateEnumRegistration(enum *EnumDescriptor) {
  942. pkg := g.packageName + "." // We always print the full package name here.
  943. // The full type name
  944. typeName := enum.TypeName()
  945. // The full type name, CamelCased.
  946. ccTypeName := CamelCaseSlice(typeName)
  947. g.P(g.ProtoPkg+".RegisterEnum(", Quote(pkg+ccTypeName), ", ", ccTypeName+"_name, ", ccTypeName+"_value)")
  948. }
  949. // And now lots of helper functions.
  950. // Is c an ASCII lower-case letter?
  951. func isASCIILower(c byte) bool {
  952. return 'a' <= c && c <= 'z'
  953. }
  954. // Is c an ASCII digit?
  955. func isASCIIDigit(c byte) bool {
  956. return '0' <= c && c <= '9'
  957. }
  958. // CamelCase returns the CamelCased name.
  959. // If there is an interior underscore followed by a lower case letter,
  960. // drop the underscore and convert the letter to upper case.
  961. // There is a remote possibility of this rewrite causing a name collision,
  962. // but it's so remote we're prepared to pretend it's nonexistent - since the
  963. // C++ generator lowercases names, it's extremely unlikely to have two fields
  964. // with different capitalizations.
  965. // In short, _my_field_name_2 becomes XMyFieldName2.
  966. func CamelCase(s string) string {
  967. if s == "" {
  968. return ""
  969. }
  970. t := make([]byte, 0, 32)
  971. oneC := make([]byte, 1)
  972. i := 0
  973. if s[0] == '_' {
  974. // Need a capital letter; drop the '_'.
  975. oneC[0] = 'X'
  976. t = bytes.Add(t, oneC)
  977. i++
  978. }
  979. // Invariant: if the next letter is lower case, it must be converted
  980. // to upper case.
  981. // That is, we process a word at a time, where words are marked by _ or
  982. // upper case letter. Digits are treated as words.
  983. for ; i < len(s); i++ {
  984. c := s[i]
  985. oneC[0] = c
  986. if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) {
  987. continue // Skip the underscore in s.
  988. }
  989. if isASCIIDigit(c) {
  990. t = bytes.Add(t, oneC)
  991. continue
  992. }
  993. // Assume we have a letter now - if not, it's a bogus identifier.
  994. // The next word is a sequence of characters that must start upper case.
  995. if isASCIILower(c) {
  996. oneC[0] ^= ' ' // Make it a capital letter.
  997. }
  998. t = bytes.Add(t, oneC) // Guaranteed not lower case.
  999. // Accept lower case sequence that follows.
  1000. for i+1 < len(s) && isASCIILower(s[i+1]) {
  1001. i++
  1002. oneC[0] = s[i]
  1003. t = bytes.Add(t, oneC)
  1004. }
  1005. }
  1006. return string(t)
  1007. }
  1008. // CamelCaseSlice is like CamelCase, but the argument is a slice of strings to
  1009. // be joined with "_".
  1010. func CamelCaseSlice(elem []string) string { return CamelCase(strings.Join(elem, "_")) }
  1011. // dottedSlice turns a sliced name into a dotted name.
  1012. func dottedSlice(elem []string) string { return strings.Join(elem, ".") }
  1013. // Quote returns a Go-source quoted string representation of s.
  1014. func Quote(s string) string { return fmt.Sprintf("%q", s) }
  1015. // Given a .proto file name, return the output name for the generated Go program.
  1016. func goFileName(name string) string {
  1017. ext := path.Ext(name)
  1018. if ext == ".proto" || ext == ".protodevel" {
  1019. name = name[0 : len(name)-len(ext)]
  1020. }
  1021. return name + ".pb.go"
  1022. }
  1023. // Is this field optional?
  1024. func isOptional(field *descriptor.FieldDescriptorProto) bool {
  1025. return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_OPTIONAL
  1026. }
  1027. // Is this field required?
  1028. func isRequired(field *descriptor.FieldDescriptorProto) bool {
  1029. return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_REQUIRED
  1030. }
  1031. // Is this field repeated?
  1032. func isRepeated(field *descriptor.FieldDescriptorProto) bool {
  1033. return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED
  1034. }
  1035. // DotToUnderscore is the mapping function used to generate Go names from package names,
  1036. // which can be dotted in the input .proto file. It maps dots to underscores.
  1037. // Because we also get here from package names generated from file names, it also maps
  1038. // minus signs to underscores.
  1039. func DotToUnderscore(rune int) int {
  1040. switch rune {
  1041. case '.', '-':
  1042. return '_'
  1043. }
  1044. return rune
  1045. }
  1046. // BaseName returns the last path element of the name, with the last dotted suffix removed.
  1047. func BaseName(name string) string {
  1048. // First, find the last element
  1049. if i := strings.LastIndex(name, "/"); i >= 0 {
  1050. name = name[i+1:]
  1051. }
  1052. // Now drop the suffix
  1053. if i := strings.LastIndex(name, "."); i >= 0 {
  1054. name = name[0:i]
  1055. }
  1056. return name
  1057. }