generator.go 89 KB

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