generator.go 112 KB

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