generator.go 93 KB

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