protogen.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. // Copyright 2018 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 protogen provides support for writing protoc plugins.
  5. //
  6. // Plugins for protoc, the Protocol Buffer compiler,
  7. // are programs which read a [pluginpb.CodeGeneratorRequest] message from standard input
  8. // and write a [pluginpb.CodeGeneratorResponse] message to standard output.
  9. // This package provides support for writing plugins which generate Go code.
  10. package protogen
  11. import (
  12. "bufio"
  13. "bytes"
  14. "fmt"
  15. "go/ast"
  16. "go/parser"
  17. "go/printer"
  18. "go/token"
  19. "go/types"
  20. "io"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "sort"
  25. "strconv"
  26. "strings"
  27. "google.golang.org/protobuf/encoding/prototext"
  28. "google.golang.org/protobuf/internal/genid"
  29. "google.golang.org/protobuf/internal/strs"
  30. "google.golang.org/protobuf/proto"
  31. "google.golang.org/protobuf/reflect/protodesc"
  32. "google.golang.org/protobuf/reflect/protoreflect"
  33. "google.golang.org/protobuf/reflect/protoregistry"
  34. "google.golang.org/protobuf/types/descriptorpb"
  35. "google.golang.org/protobuf/types/dynamicpb"
  36. "google.golang.org/protobuf/types/pluginpb"
  37. )
  38. const goPackageDocURL = "https://protobuf.dev/reference/go/go-generated#package"
  39. // Run executes a function as a protoc plugin.
  40. //
  41. // It reads a [pluginpb.CodeGeneratorRequest] message from [os.Stdin], invokes the plugin
  42. // function, and writes a [pluginpb.CodeGeneratorResponse] message to [os.Stdout].
  43. //
  44. // If a failure occurs while reading or writing, Run prints an error to
  45. // [os.Stderr] and calls [os.Exit](1).
  46. func (opts Options) Run(f func(*Plugin) error) {
  47. if err := run(opts, f); err != nil {
  48. fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err)
  49. os.Exit(1)
  50. }
  51. }
  52. func run(opts Options, f func(*Plugin) error) error {
  53. if len(os.Args) > 1 {
  54. return fmt.Errorf("unknown argument %q (this program should be run by protoc, not directly)", os.Args[1])
  55. }
  56. in, err := io.ReadAll(os.Stdin)
  57. if err != nil {
  58. return err
  59. }
  60. req := &pluginpb.CodeGeneratorRequest{}
  61. if err := proto.Unmarshal(in, req); err != nil {
  62. return err
  63. }
  64. gen, err := opts.New(req)
  65. if err != nil {
  66. return err
  67. }
  68. if err := f(gen); err != nil {
  69. // Errors from the plugin function are reported by setting the
  70. // error field in the CodeGeneratorResponse.
  71. //
  72. // In contrast, errors that indicate a problem in protoc
  73. // itself (unparsable input, I/O errors, etc.) are reported
  74. // to stderr.
  75. gen.Error(err)
  76. }
  77. resp := gen.Response()
  78. out, err := proto.Marshal(resp)
  79. if err != nil {
  80. return err
  81. }
  82. if _, err := os.Stdout.Write(out); err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. // A Plugin is a protoc plugin invocation.
  88. type Plugin struct {
  89. // Request is the CodeGeneratorRequest provided by protoc.
  90. Request *pluginpb.CodeGeneratorRequest
  91. // Files is the set of files to generate and everything they import.
  92. // Files appear in topological order, so each file appears before any
  93. // file that imports it.
  94. Files []*File
  95. FilesByPath map[string]*File
  96. // SupportedFeatures is the set of protobuf language features supported by
  97. // this generator plugin. See the documentation for
  98. // google.protobuf.CodeGeneratorResponse.supported_features for details.
  99. SupportedFeatures uint64
  100. SupportedEditionsMinimum descriptorpb.Edition
  101. SupportedEditionsMaximum descriptorpb.Edition
  102. fileReg *protoregistry.Files
  103. enumsByName map[protoreflect.FullName]*Enum
  104. messagesByName map[protoreflect.FullName]*Message
  105. annotateCode bool
  106. pathType pathType
  107. module string
  108. genFiles []*GeneratedFile
  109. opts Options
  110. err error
  111. }
  112. type Options struct {
  113. // If ParamFunc is non-nil, it will be called with each unknown
  114. // generator parameter.
  115. //
  116. // Plugins for protoc can accept parameters from the command line,
  117. // passed in the --<lang>_out protoc, separated from the output
  118. // directory with a colon; e.g.,
  119. //
  120. // --go_out=<param1>=<value1>,<param2>=<value2>:<output_directory>
  121. //
  122. // Parameters passed in this fashion as a comma-separated list of
  123. // key=value pairs will be passed to the ParamFunc.
  124. //
  125. // The (flag.FlagSet).Set method matches this function signature,
  126. // so parameters can be converted into flags as in the following:
  127. //
  128. // var flags flag.FlagSet
  129. // value := flags.Bool("param", false, "")
  130. // opts := &protogen.Options{
  131. // ParamFunc: flags.Set,
  132. // }
  133. // opts.Run(func(p *protogen.Plugin) error {
  134. // if *value { ... }
  135. // })
  136. ParamFunc func(name, value string) error
  137. // ImportRewriteFunc is called with the import path of each package
  138. // imported by a generated file. It returns the import path to use
  139. // for this package.
  140. ImportRewriteFunc func(GoImportPath) GoImportPath
  141. }
  142. // New returns a new Plugin.
  143. func (opts Options) New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) {
  144. gen := &Plugin{
  145. Request: req,
  146. FilesByPath: make(map[string]*File),
  147. fileReg: new(protoregistry.Files),
  148. enumsByName: make(map[protoreflect.FullName]*Enum),
  149. messagesByName: make(map[protoreflect.FullName]*Message),
  150. opts: opts,
  151. }
  152. packageNames := make(map[string]GoPackageName) // filename -> package name
  153. importPaths := make(map[string]GoImportPath) // filename -> import path
  154. for _, param := range strings.Split(req.GetParameter(), ",") {
  155. var value string
  156. if i := strings.Index(param, "="); i >= 0 {
  157. value = param[i+1:]
  158. param = param[0:i]
  159. }
  160. switch param {
  161. case "":
  162. // Ignore.
  163. case "module":
  164. gen.module = value
  165. case "paths":
  166. switch value {
  167. case "import":
  168. gen.pathType = pathTypeImport
  169. case "source_relative":
  170. gen.pathType = pathTypeSourceRelative
  171. default:
  172. return nil, fmt.Errorf(`unknown path type %q: want "import" or "source_relative"`, value)
  173. }
  174. case "annotate_code":
  175. switch value {
  176. case "true", "":
  177. gen.annotateCode = true
  178. case "false":
  179. default:
  180. return nil, fmt.Errorf(`bad value for parameter %q: want "true" or "false"`, param)
  181. }
  182. default:
  183. if param[0] == 'M' {
  184. impPath, pkgName := splitImportPathAndPackageName(value)
  185. if pkgName != "" {
  186. packageNames[param[1:]] = pkgName
  187. }
  188. if impPath != "" {
  189. importPaths[param[1:]] = impPath
  190. }
  191. continue
  192. }
  193. if opts.ParamFunc != nil {
  194. if err := opts.ParamFunc(param, value); err != nil {
  195. return nil, err
  196. }
  197. }
  198. }
  199. }
  200. // When the module= option is provided, we strip the module name
  201. // prefix from generated files. This only makes sense if generated
  202. // filenames are based on the import path.
  203. if gen.module != "" && gen.pathType == pathTypeSourceRelative {
  204. return nil, fmt.Errorf("cannot use module= with paths=source_relative")
  205. }
  206. // Figure out the import path and package name for each file.
  207. //
  208. // The rules here are complicated and have grown organically over time.
  209. // Interactions between different ways of specifying package information
  210. // may be surprising.
  211. //
  212. // The recommended approach is to include a go_package option in every
  213. // .proto source file specifying the full import path of the Go package
  214. // associated with this file.
  215. //
  216. // option go_package = "google.golang.org/protobuf/types/known/anypb";
  217. //
  218. // Alternatively, build systems which want to exert full control over
  219. // import paths may specify M<filename>=<import_path> flags.
  220. for _, fdesc := range gen.Request.ProtoFile {
  221. // The "M" command-line flags take precedence over
  222. // the "go_package" option in the .proto source file.
  223. filename := fdesc.GetName()
  224. impPath, pkgName := splitImportPathAndPackageName(fdesc.GetOptions().GetGoPackage())
  225. if importPaths[filename] == "" && impPath != "" {
  226. importPaths[filename] = impPath
  227. }
  228. if packageNames[filename] == "" && pkgName != "" {
  229. packageNames[filename] = pkgName
  230. }
  231. switch {
  232. case importPaths[filename] == "":
  233. // The import path must be specified one way or another.
  234. return nil, fmt.Errorf(
  235. "unable to determine Go import path for %q\n\n"+
  236. "Please specify either:\n"+
  237. "\t• a \"go_package\" option in the .proto source file, or\n"+
  238. "\t• a \"M\" argument on the command line.\n\n"+
  239. "See %v for more information.\n",
  240. fdesc.GetName(), goPackageDocURL)
  241. case !strings.Contains(string(importPaths[filename]), ".") &&
  242. !strings.Contains(string(importPaths[filename]), "/"):
  243. // Check that import paths contain at least a dot or slash to avoid
  244. // a common mistake where import path is confused with package name.
  245. return nil, fmt.Errorf(
  246. "invalid Go import path %q for %q\n\n"+
  247. "The import path must contain at least one period ('.') or forward slash ('/') character.\n\n"+
  248. "See %v for more information.\n",
  249. string(importPaths[filename]), fdesc.GetName(), goPackageDocURL)
  250. case packageNames[filename] == "":
  251. // If the package name is not explicitly specified,
  252. // then derive a reasonable package name from the import path.
  253. //
  254. // NOTE: The package name is derived first from the import path in
  255. // the "go_package" option (if present) before trying the "M" flag.
  256. // The inverted order for this is because the primary use of the "M"
  257. // flag is by build systems that have full control over the
  258. // import paths all packages, where it is generally expected that
  259. // the Go package name still be identical for the Go toolchain and
  260. // for custom build systems like Bazel.
  261. if impPath == "" {
  262. impPath = importPaths[filename]
  263. }
  264. packageNames[filename] = cleanPackageName(path.Base(string(impPath)))
  265. }
  266. }
  267. // Consistency check: Every file with the same Go import path should have
  268. // the same Go package name.
  269. packageFiles := make(map[GoImportPath][]string)
  270. for filename, importPath := range importPaths {
  271. if _, ok := packageNames[filename]; !ok {
  272. // Skip files mentioned in a M<file>=<import_path> parameter
  273. // but which do not appear in the CodeGeneratorRequest.
  274. continue
  275. }
  276. packageFiles[importPath] = append(packageFiles[importPath], filename)
  277. }
  278. for importPath, filenames := range packageFiles {
  279. for i := 1; i < len(filenames); i++ {
  280. if a, b := packageNames[filenames[0]], packageNames[filenames[i]]; a != b {
  281. return nil, fmt.Errorf("Go package %v has inconsistent names %v (%v) and %v (%v)",
  282. importPath, a, filenames[0], b, filenames[i])
  283. }
  284. }
  285. }
  286. // The extracted types from the full import set
  287. typeRegistry := newExtensionRegistry()
  288. for _, fdesc := range gen.Request.ProtoFile {
  289. filename := fdesc.GetName()
  290. if gen.FilesByPath[filename] != nil {
  291. return nil, fmt.Errorf("duplicate file name: %q", filename)
  292. }
  293. f, err := newFile(gen, fdesc, packageNames[filename], importPaths[filename])
  294. if err != nil {
  295. return nil, err
  296. }
  297. gen.Files = append(gen.Files, f)
  298. gen.FilesByPath[filename] = f
  299. if err = typeRegistry.registerAllExtensionsFromFile(f.Desc); err != nil {
  300. return nil, err
  301. }
  302. }
  303. for _, filename := range gen.Request.FileToGenerate {
  304. f, ok := gen.FilesByPath[filename]
  305. if !ok {
  306. return nil, fmt.Errorf("no descriptor for generated file: %v", filename)
  307. }
  308. f.Generate = true
  309. }
  310. // Create fully-linked descriptors if new extensions were found
  311. if typeRegistry.hasNovelExtensions() {
  312. for _, f := range gen.Files {
  313. b, err := proto.Marshal(f.Proto.ProtoReflect().Interface())
  314. if err != nil {
  315. return nil, err
  316. }
  317. err = proto.UnmarshalOptions{Resolver: typeRegistry}.Unmarshal(b, f.Proto)
  318. if err != nil {
  319. return nil, err
  320. }
  321. }
  322. }
  323. return gen, nil
  324. }
  325. // Error records an error in code generation. The generator will report the
  326. // error back to protoc and will not produce output.
  327. func (gen *Plugin) Error(err error) {
  328. if gen.err == nil {
  329. gen.err = err
  330. }
  331. }
  332. // Response returns the generator output.
  333. func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse {
  334. resp := &pluginpb.CodeGeneratorResponse{}
  335. if gen.err != nil {
  336. resp.Error = proto.String(gen.err.Error())
  337. return resp
  338. }
  339. for _, g := range gen.genFiles {
  340. if g.skip {
  341. continue
  342. }
  343. content, err := g.Content()
  344. if err != nil {
  345. return &pluginpb.CodeGeneratorResponse{
  346. Error: proto.String(err.Error()),
  347. }
  348. }
  349. filename := g.filename
  350. if gen.module != "" {
  351. trim := gen.module + "/"
  352. if !strings.HasPrefix(filename, trim) {
  353. return &pluginpb.CodeGeneratorResponse{
  354. Error: proto.String(fmt.Sprintf("%v: generated file does not match prefix %q", filename, gen.module)),
  355. }
  356. }
  357. filename = strings.TrimPrefix(filename, trim)
  358. }
  359. resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
  360. Name: proto.String(filename),
  361. Content: proto.String(string(content)),
  362. })
  363. if gen.annotateCode && strings.HasSuffix(g.filename, ".go") {
  364. meta, err := g.metaFile(content)
  365. if err != nil {
  366. return &pluginpb.CodeGeneratorResponse{
  367. Error: proto.String(err.Error()),
  368. }
  369. }
  370. resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
  371. Name: proto.String(filename + ".meta"),
  372. Content: proto.String(meta),
  373. })
  374. }
  375. }
  376. if gen.SupportedFeatures > 0 {
  377. resp.SupportedFeatures = proto.Uint64(gen.SupportedFeatures)
  378. }
  379. if gen.SupportedEditionsMinimum != descriptorpb.Edition_EDITION_UNKNOWN && gen.SupportedEditionsMaximum != descriptorpb.Edition_EDITION_UNKNOWN {
  380. resp.MinimumEdition = proto.Int32(int32(gen.SupportedEditionsMinimum))
  381. resp.MaximumEdition = proto.Int32(int32(gen.SupportedEditionsMaximum))
  382. }
  383. return resp
  384. }
  385. // A File describes a .proto source file.
  386. type File struct {
  387. Desc protoreflect.FileDescriptor
  388. Proto *descriptorpb.FileDescriptorProto
  389. GoDescriptorIdent GoIdent // name of Go variable for the file descriptor
  390. GoPackageName GoPackageName // name of this file's Go package
  391. GoImportPath GoImportPath // import path of this file's Go package
  392. Enums []*Enum // top-level enum declarations
  393. Messages []*Message // top-level message declarations
  394. Extensions []*Extension // top-level extension declarations
  395. Services []*Service // top-level service declarations
  396. Generate bool // true if we should generate code for this file
  397. // GeneratedFilenamePrefix is used to construct filenames for generated
  398. // files associated with this source file.
  399. //
  400. // For example, the source file "dir/foo.proto" might have a filename prefix
  401. // of "dir/foo". Appending ".pb.go" produces an output file of "dir/foo.pb.go".
  402. GeneratedFilenamePrefix string
  403. location Location
  404. }
  405. func newFile(gen *Plugin, p *descriptorpb.FileDescriptorProto, packageName GoPackageName, importPath GoImportPath) (*File, error) {
  406. desc, err := protodesc.NewFile(p, gen.fileReg)
  407. if err != nil {
  408. return nil, fmt.Errorf("invalid FileDescriptorProto %q: %v", p.GetName(), err)
  409. }
  410. if err := gen.fileReg.RegisterFile(desc); err != nil {
  411. return nil, fmt.Errorf("cannot register descriptor %q: %v", p.GetName(), err)
  412. }
  413. f := &File{
  414. Desc: desc,
  415. Proto: p,
  416. GoPackageName: packageName,
  417. GoImportPath: importPath,
  418. location: Location{SourceFile: desc.Path()},
  419. }
  420. // Determine the prefix for generated Go files.
  421. prefix := p.GetName()
  422. if ext := path.Ext(prefix); ext == ".proto" || ext == ".protodevel" {
  423. prefix = prefix[:len(prefix)-len(ext)]
  424. }
  425. switch gen.pathType {
  426. case pathTypeImport:
  427. // If paths=import, the output filename is derived from the Go import path.
  428. prefix = path.Join(string(f.GoImportPath), path.Base(prefix))
  429. case pathTypeSourceRelative:
  430. // If paths=source_relative, the output filename is derived from
  431. // the input filename.
  432. }
  433. f.GoDescriptorIdent = GoIdent{
  434. GoName: "File_" + strs.GoSanitized(p.GetName()),
  435. GoImportPath: f.GoImportPath,
  436. }
  437. f.GeneratedFilenamePrefix = prefix
  438. for i, eds := 0, desc.Enums(); i < eds.Len(); i++ {
  439. f.Enums = append(f.Enums, newEnum(gen, f, nil, eds.Get(i)))
  440. }
  441. for i, mds := 0, desc.Messages(); i < mds.Len(); i++ {
  442. f.Messages = append(f.Messages, newMessage(gen, f, nil, mds.Get(i)))
  443. }
  444. for i, xds := 0, desc.Extensions(); i < xds.Len(); i++ {
  445. f.Extensions = append(f.Extensions, newField(gen, f, nil, xds.Get(i)))
  446. }
  447. for i, sds := 0, desc.Services(); i < sds.Len(); i++ {
  448. f.Services = append(f.Services, newService(gen, f, sds.Get(i)))
  449. }
  450. for _, message := range f.Messages {
  451. if err := message.resolveDependencies(gen); err != nil {
  452. return nil, err
  453. }
  454. }
  455. for _, extension := range f.Extensions {
  456. if err := extension.resolveDependencies(gen); err != nil {
  457. return nil, err
  458. }
  459. }
  460. for _, service := range f.Services {
  461. for _, method := range service.Methods {
  462. if err := method.resolveDependencies(gen); err != nil {
  463. return nil, err
  464. }
  465. }
  466. }
  467. return f, nil
  468. }
  469. // splitImportPathAndPackageName splits off the optional Go package name
  470. // from the Go import path when separated by a ';' delimiter.
  471. func splitImportPathAndPackageName(s string) (GoImportPath, GoPackageName) {
  472. if i := strings.Index(s, ";"); i >= 0 {
  473. return GoImportPath(s[:i]), GoPackageName(s[i+1:])
  474. }
  475. return GoImportPath(s), ""
  476. }
  477. // An Enum describes an enum.
  478. type Enum struct {
  479. Desc protoreflect.EnumDescriptor
  480. GoIdent GoIdent // name of the generated Go type
  481. Values []*EnumValue // enum value declarations
  482. Location Location // location of this enum
  483. Comments CommentSet // comments associated with this enum
  484. }
  485. func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum {
  486. var loc Location
  487. if parent != nil {
  488. loc = parent.Location.appendPath(genid.DescriptorProto_EnumType_field_number, desc.Index())
  489. } else {
  490. loc = f.location.appendPath(genid.FileDescriptorProto_EnumType_field_number, desc.Index())
  491. }
  492. enum := &Enum{
  493. Desc: desc,
  494. GoIdent: newGoIdent(f, desc),
  495. Location: loc,
  496. Comments: makeCommentSet(f.Desc.SourceLocations().ByDescriptor(desc)),
  497. }
  498. gen.enumsByName[desc.FullName()] = enum
  499. for i, vds := 0, enum.Desc.Values(); i < vds.Len(); i++ {
  500. enum.Values = append(enum.Values, newEnumValue(gen, f, parent, enum, vds.Get(i)))
  501. }
  502. return enum
  503. }
  504. // An EnumValue describes an enum value.
  505. type EnumValue struct {
  506. Desc protoreflect.EnumValueDescriptor
  507. GoIdent GoIdent // name of the generated Go declaration
  508. Parent *Enum // enum in which this value is declared
  509. Location Location // location of this enum value
  510. Comments CommentSet // comments associated with this enum value
  511. }
  512. func newEnumValue(gen *Plugin, f *File, message *Message, enum *Enum, desc protoreflect.EnumValueDescriptor) *EnumValue {
  513. // A top-level enum value's name is: EnumName_ValueName
  514. // An enum value contained in a message is: MessageName_ValueName
  515. //
  516. // For historical reasons, enum value names are not camel-cased.
  517. parentIdent := enum.GoIdent
  518. if message != nil {
  519. parentIdent = message.GoIdent
  520. }
  521. name := parentIdent.GoName + "_" + string(desc.Name())
  522. loc := enum.Location.appendPath(genid.EnumDescriptorProto_Value_field_number, desc.Index())
  523. return &EnumValue{
  524. Desc: desc,
  525. GoIdent: f.GoImportPath.Ident(name),
  526. Parent: enum,
  527. Location: loc,
  528. Comments: makeCommentSet(f.Desc.SourceLocations().ByDescriptor(desc)),
  529. }
  530. }
  531. // A Message describes a message.
  532. type Message struct {
  533. Desc protoreflect.MessageDescriptor
  534. GoIdent GoIdent // name of the generated Go type
  535. Fields []*Field // message field declarations
  536. Oneofs []*Oneof // message oneof declarations
  537. Enums []*Enum // nested enum declarations
  538. Messages []*Message // nested message declarations
  539. Extensions []*Extension // nested extension declarations
  540. Location Location // location of this message
  541. Comments CommentSet // comments associated with this message
  542. }
  543. func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) *Message {
  544. var loc Location
  545. if parent != nil {
  546. loc = parent.Location.appendPath(genid.DescriptorProto_NestedType_field_number, desc.Index())
  547. } else {
  548. loc = f.location.appendPath(genid.FileDescriptorProto_MessageType_field_number, desc.Index())
  549. }
  550. message := &Message{
  551. Desc: desc,
  552. GoIdent: newGoIdent(f, desc),
  553. Location: loc,
  554. Comments: makeCommentSet(f.Desc.SourceLocations().ByDescriptor(desc)),
  555. }
  556. gen.messagesByName[desc.FullName()] = message
  557. for i, eds := 0, desc.Enums(); i < eds.Len(); i++ {
  558. message.Enums = append(message.Enums, newEnum(gen, f, message, eds.Get(i)))
  559. }
  560. for i, mds := 0, desc.Messages(); i < mds.Len(); i++ {
  561. message.Messages = append(message.Messages, newMessage(gen, f, message, mds.Get(i)))
  562. }
  563. for i, fds := 0, desc.Fields(); i < fds.Len(); i++ {
  564. message.Fields = append(message.Fields, newField(gen, f, message, fds.Get(i)))
  565. }
  566. for i, ods := 0, desc.Oneofs(); i < ods.Len(); i++ {
  567. message.Oneofs = append(message.Oneofs, newOneof(gen, f, message, ods.Get(i)))
  568. }
  569. for i, xds := 0, desc.Extensions(); i < xds.Len(); i++ {
  570. message.Extensions = append(message.Extensions, newField(gen, f, message, xds.Get(i)))
  571. }
  572. // Resolve local references between fields and oneofs.
  573. for _, field := range message.Fields {
  574. if od := field.Desc.ContainingOneof(); od != nil {
  575. oneof := message.Oneofs[od.Index()]
  576. field.Oneof = oneof
  577. oneof.Fields = append(oneof.Fields, field)
  578. }
  579. }
  580. // Field name conflict resolution.
  581. //
  582. // We assume well-known method names that may be attached to a generated
  583. // message type, as well as a 'Get*' method for each field. For each
  584. // field in turn, we add _s to its name until there are no conflicts.
  585. //
  586. // Any change to the following set of method names is a potential
  587. // incompatible API change because it may change generated field names.
  588. //
  589. // TODO: If we ever support a 'go_name' option to set the Go name of a
  590. // field, we should consider dropping this entirely. The conflict
  591. // resolution algorithm is subtle and surprising (changing the order
  592. // in which fields appear in the .proto source file can change the
  593. // names of fields in generated code), and does not adapt well to
  594. // adding new per-field methods such as setters.
  595. usedNames := map[string]bool{
  596. "Reset": true,
  597. "String": true,
  598. "ProtoMessage": true,
  599. "Marshal": true,
  600. "Unmarshal": true,
  601. "ExtensionRangeArray": true,
  602. "ExtensionMap": true,
  603. "Descriptor": true,
  604. }
  605. makeNameUnique := func(name string, hasGetter bool) string {
  606. for usedNames[name] || (hasGetter && usedNames["Get"+name]) {
  607. name += "_"
  608. }
  609. usedNames[name] = true
  610. usedNames["Get"+name] = hasGetter
  611. return name
  612. }
  613. for _, field := range message.Fields {
  614. field.GoName = makeNameUnique(field.GoName, true)
  615. field.GoIdent.GoName = message.GoIdent.GoName + "_" + field.GoName
  616. if field.Oneof != nil && field.Oneof.Fields[0] == field {
  617. // Make the name for a oneof unique as well. For historical reasons,
  618. // this assumes that a getter method is not generated for oneofs.
  619. // This is incorrect, but fixing it breaks existing code.
  620. field.Oneof.GoName = makeNameUnique(field.Oneof.GoName, false)
  621. field.Oneof.GoIdent.GoName = message.GoIdent.GoName + "_" + field.Oneof.GoName
  622. }
  623. }
  624. // Oneof field name conflict resolution.
  625. //
  626. // This conflict resolution is incomplete as it does not consider collisions
  627. // with other oneof field types, but fixing it breaks existing code.
  628. for _, field := range message.Fields {
  629. if field.Oneof != nil {
  630. Loop:
  631. for {
  632. for _, nestedMessage := range message.Messages {
  633. if nestedMessage.GoIdent == field.GoIdent {
  634. field.GoIdent.GoName += "_"
  635. continue Loop
  636. }
  637. }
  638. for _, nestedEnum := range message.Enums {
  639. if nestedEnum.GoIdent == field.GoIdent {
  640. field.GoIdent.GoName += "_"
  641. continue Loop
  642. }
  643. }
  644. break Loop
  645. }
  646. }
  647. }
  648. return message
  649. }
  650. func (message *Message) resolveDependencies(gen *Plugin) error {
  651. for _, field := range message.Fields {
  652. if err := field.resolveDependencies(gen); err != nil {
  653. return err
  654. }
  655. }
  656. for _, message := range message.Messages {
  657. if err := message.resolveDependencies(gen); err != nil {
  658. return err
  659. }
  660. }
  661. for _, extension := range message.Extensions {
  662. if err := extension.resolveDependencies(gen); err != nil {
  663. return err
  664. }
  665. }
  666. return nil
  667. }
  668. // A Field describes a message field.
  669. type Field struct {
  670. Desc protoreflect.FieldDescriptor
  671. // GoName is the base name of this field's Go field and methods.
  672. // For code generated by protoc-gen-go, this means a field named
  673. // '{{GoName}}' and a getter method named 'Get{{GoName}}'.
  674. GoName string // e.g., "FieldName"
  675. // GoIdent is the base name of a top-level declaration for this field.
  676. // For code generated by protoc-gen-go, this means a wrapper type named
  677. // '{{GoIdent}}' for members fields of a oneof, and a variable named
  678. // 'E_{{GoIdent}}' for extension fields.
  679. GoIdent GoIdent // e.g., "MessageName_FieldName"
  680. Parent *Message // message in which this field is declared; nil if top-level extension
  681. Oneof *Oneof // containing oneof; nil if not part of a oneof
  682. Extendee *Message // extended message for extension fields; nil otherwise
  683. Enum *Enum // type for enum fields; nil otherwise
  684. Message *Message // type for message or group fields; nil otherwise
  685. Location Location // location of this field
  686. Comments CommentSet // comments associated with this field
  687. }
  688. func newField(gen *Plugin, f *File, message *Message, desc protoreflect.FieldDescriptor) *Field {
  689. var loc Location
  690. switch {
  691. case desc.IsExtension() && message == nil:
  692. loc = f.location.appendPath(genid.FileDescriptorProto_Extension_field_number, desc.Index())
  693. case desc.IsExtension() && message != nil:
  694. loc = message.Location.appendPath(genid.DescriptorProto_Extension_field_number, desc.Index())
  695. default:
  696. loc = message.Location.appendPath(genid.DescriptorProto_Field_field_number, desc.Index())
  697. }
  698. camelCased := strs.GoCamelCase(string(desc.Name()))
  699. var parentPrefix string
  700. if message != nil {
  701. parentPrefix = message.GoIdent.GoName + "_"
  702. }
  703. field := &Field{
  704. Desc: desc,
  705. GoName: camelCased,
  706. GoIdent: GoIdent{
  707. GoImportPath: f.GoImportPath,
  708. GoName: parentPrefix + camelCased,
  709. },
  710. Parent: message,
  711. Location: loc,
  712. Comments: makeCommentSet(f.Desc.SourceLocations().ByDescriptor(desc)),
  713. }
  714. return field
  715. }
  716. func (field *Field) resolveDependencies(gen *Plugin) error {
  717. desc := field.Desc
  718. switch desc.Kind() {
  719. case protoreflect.EnumKind:
  720. name := field.Desc.Enum().FullName()
  721. enum, ok := gen.enumsByName[name]
  722. if !ok {
  723. return fmt.Errorf("field %v: no descriptor for enum %v", desc.FullName(), name)
  724. }
  725. field.Enum = enum
  726. case protoreflect.MessageKind, protoreflect.GroupKind:
  727. name := desc.Message().FullName()
  728. message, ok := gen.messagesByName[name]
  729. if !ok {
  730. return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), name)
  731. }
  732. field.Message = message
  733. }
  734. if desc.IsExtension() {
  735. name := desc.ContainingMessage().FullName()
  736. message, ok := gen.messagesByName[name]
  737. if !ok {
  738. return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), name)
  739. }
  740. field.Extendee = message
  741. }
  742. return nil
  743. }
  744. // A Oneof describes a message oneof.
  745. type Oneof struct {
  746. Desc protoreflect.OneofDescriptor
  747. // GoName is the base name of this oneof's Go field and methods.
  748. // For code generated by protoc-gen-go, this means a field named
  749. // '{{GoName}}' and a getter method named 'Get{{GoName}}'.
  750. GoName string // e.g., "OneofName"
  751. // GoIdent is the base name of a top-level declaration for this oneof.
  752. GoIdent GoIdent // e.g., "MessageName_OneofName"
  753. Parent *Message // message in which this oneof is declared
  754. Fields []*Field // fields that are part of this oneof
  755. Location Location // location of this oneof
  756. Comments CommentSet // comments associated with this oneof
  757. }
  758. func newOneof(gen *Plugin, f *File, message *Message, desc protoreflect.OneofDescriptor) *Oneof {
  759. loc := message.Location.appendPath(genid.DescriptorProto_OneofDecl_field_number, desc.Index())
  760. camelCased := strs.GoCamelCase(string(desc.Name()))
  761. parentPrefix := message.GoIdent.GoName + "_"
  762. return &Oneof{
  763. Desc: desc,
  764. Parent: message,
  765. GoName: camelCased,
  766. GoIdent: GoIdent{
  767. GoImportPath: f.GoImportPath,
  768. GoName: parentPrefix + camelCased,
  769. },
  770. Location: loc,
  771. Comments: makeCommentSet(f.Desc.SourceLocations().ByDescriptor(desc)),
  772. }
  773. }
  774. // Extension is an alias of [Field] for documentation.
  775. type Extension = Field
  776. // A Service describes a service.
  777. type Service struct {
  778. Desc protoreflect.ServiceDescriptor
  779. GoName string
  780. Methods []*Method // service method declarations
  781. Location Location // location of this service
  782. Comments CommentSet // comments associated with this service
  783. }
  784. func newService(gen *Plugin, f *File, desc protoreflect.ServiceDescriptor) *Service {
  785. loc := f.location.appendPath(genid.FileDescriptorProto_Service_field_number, desc.Index())
  786. service := &Service{
  787. Desc: desc,
  788. GoName: strs.GoCamelCase(string(desc.Name())),
  789. Location: loc,
  790. Comments: makeCommentSet(f.Desc.SourceLocations().ByDescriptor(desc)),
  791. }
  792. for i, mds := 0, desc.Methods(); i < mds.Len(); i++ {
  793. service.Methods = append(service.Methods, newMethod(gen, f, service, mds.Get(i)))
  794. }
  795. return service
  796. }
  797. // A Method describes a method in a service.
  798. type Method struct {
  799. Desc protoreflect.MethodDescriptor
  800. GoName string
  801. Parent *Service // service in which this method is declared
  802. Input *Message
  803. Output *Message
  804. Location Location // location of this method
  805. Comments CommentSet // comments associated with this method
  806. }
  807. func newMethod(gen *Plugin, f *File, service *Service, desc protoreflect.MethodDescriptor) *Method {
  808. loc := service.Location.appendPath(genid.ServiceDescriptorProto_Method_field_number, desc.Index())
  809. method := &Method{
  810. Desc: desc,
  811. GoName: strs.GoCamelCase(string(desc.Name())),
  812. Parent: service,
  813. Location: loc,
  814. Comments: makeCommentSet(f.Desc.SourceLocations().ByDescriptor(desc)),
  815. }
  816. return method
  817. }
  818. func (method *Method) resolveDependencies(gen *Plugin) error {
  819. desc := method.Desc
  820. inName := desc.Input().FullName()
  821. in, ok := gen.messagesByName[inName]
  822. if !ok {
  823. return fmt.Errorf("method %v: no descriptor for type %v", desc.FullName(), inName)
  824. }
  825. method.Input = in
  826. outName := desc.Output().FullName()
  827. out, ok := gen.messagesByName[outName]
  828. if !ok {
  829. return fmt.Errorf("method %v: no descriptor for type %v", desc.FullName(), outName)
  830. }
  831. method.Output = out
  832. return nil
  833. }
  834. // A GeneratedFile is a generated file.
  835. type GeneratedFile struct {
  836. gen *Plugin
  837. skip bool
  838. filename string
  839. goImportPath GoImportPath
  840. buf bytes.Buffer
  841. packageNames map[GoImportPath]GoPackageName
  842. usedPackageNames map[GoPackageName]bool
  843. manualImports map[GoImportPath]bool
  844. annotations map[string][]Annotation
  845. }
  846. // NewGeneratedFile creates a new generated file with the given filename
  847. // and import path.
  848. func (gen *Plugin) NewGeneratedFile(filename string, goImportPath GoImportPath) *GeneratedFile {
  849. g := &GeneratedFile{
  850. gen: gen,
  851. filename: filename,
  852. goImportPath: goImportPath,
  853. packageNames: make(map[GoImportPath]GoPackageName),
  854. usedPackageNames: make(map[GoPackageName]bool),
  855. manualImports: make(map[GoImportPath]bool),
  856. annotations: make(map[string][]Annotation),
  857. }
  858. // All predeclared identifiers in Go are already used.
  859. for _, s := range types.Universe.Names() {
  860. g.usedPackageNames[GoPackageName(s)] = true
  861. }
  862. gen.genFiles = append(gen.genFiles, g)
  863. return g
  864. }
  865. // P prints a line to the generated output. It converts each parameter to a
  866. // string following the same rules as [fmt.Print]. It never inserts spaces
  867. // between parameters.
  868. func (g *GeneratedFile) P(v ...any) {
  869. for _, x := range v {
  870. switch x := x.(type) {
  871. case GoIdent:
  872. fmt.Fprint(&g.buf, g.QualifiedGoIdent(x))
  873. default:
  874. fmt.Fprint(&g.buf, x)
  875. }
  876. }
  877. fmt.Fprintln(&g.buf)
  878. }
  879. // QualifiedGoIdent returns the string to use for a Go identifier.
  880. //
  881. // If the identifier is from a different Go package than the generated file,
  882. // the returned name will be qualified (package.name) and an import statement
  883. // for the identifier's package will be included in the file.
  884. func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string {
  885. if ident.GoImportPath == g.goImportPath {
  886. return ident.GoName
  887. }
  888. if packageName, ok := g.packageNames[ident.GoImportPath]; ok {
  889. return string(packageName) + "." + ident.GoName
  890. }
  891. packageName := cleanPackageName(path.Base(string(ident.GoImportPath)))
  892. for i, orig := 1, packageName; g.usedPackageNames[packageName]; i++ {
  893. packageName = orig + GoPackageName(strconv.Itoa(i))
  894. }
  895. g.packageNames[ident.GoImportPath] = packageName
  896. g.usedPackageNames[packageName] = true
  897. return string(packageName) + "." + ident.GoName
  898. }
  899. // Import ensures a package is imported by the generated file.
  900. //
  901. // Packages referenced by [GeneratedFile.QualifiedGoIdent] are automatically imported.
  902. // Explicitly importing a package with Import is generally only necessary
  903. // when the import will be blank (import _ "package").
  904. func (g *GeneratedFile) Import(importPath GoImportPath) {
  905. g.manualImports[importPath] = true
  906. }
  907. // Write implements [io.Writer].
  908. func (g *GeneratedFile) Write(p []byte) (n int, err error) {
  909. return g.buf.Write(p)
  910. }
  911. // Skip removes the generated file from the plugin output.
  912. func (g *GeneratedFile) Skip() {
  913. g.skip = true
  914. }
  915. // Unskip reverts a previous call to [GeneratedFile.Skip],
  916. // re-including the generated file in the plugin output.
  917. func (g *GeneratedFile) Unskip() {
  918. g.skip = false
  919. }
  920. // Annotate associates a symbol in a generated Go file with a location in a
  921. // source .proto file.
  922. //
  923. // The symbol may refer to a type, constant, variable, function, method, or
  924. // struct field. The "T.sel" syntax is used to identify the method or field
  925. // 'sel' on type 'T'.
  926. //
  927. // Deprecated: Use the [GeneratedFile.AnnotateSymbol] method instead.
  928. func (g *GeneratedFile) Annotate(symbol string, loc Location) {
  929. g.AnnotateSymbol(symbol, Annotation{Location: loc})
  930. }
  931. // An Annotation provides semantic detail for a generated proto element.
  932. //
  933. // See the google.protobuf.GeneratedCodeInfo.Annotation documentation in
  934. // descriptor.proto for details.
  935. type Annotation struct {
  936. // Location is the source .proto file for the element.
  937. Location Location
  938. // Semantic is the symbol's effect on the element in the original .proto file.
  939. Semantic *descriptorpb.GeneratedCodeInfo_Annotation_Semantic
  940. }
  941. // AnnotateSymbol associates a symbol in a generated Go file with a location
  942. // in a source .proto file and a semantic type.
  943. //
  944. // The symbol may refer to a type, constant, variable, function, method, or
  945. // struct field. The "T.sel" syntax is used to identify the method or field
  946. // 'sel' on type 'T'.
  947. func (g *GeneratedFile) AnnotateSymbol(symbol string, info Annotation) {
  948. g.annotations[symbol] = append(g.annotations[symbol], info)
  949. }
  950. // Content returns the contents of the generated file.
  951. func (g *GeneratedFile) Content() ([]byte, error) {
  952. if !strings.HasSuffix(g.filename, ".go") {
  953. return g.buf.Bytes(), nil
  954. }
  955. // Reformat generated code.
  956. original := g.buf.Bytes()
  957. fset := token.NewFileSet()
  958. file, err := parser.ParseFile(fset, "", original, parser.ParseComments)
  959. if err != nil {
  960. // Print out the bad code with line numbers.
  961. // This should never happen in practice, but it can while changing generated code
  962. // so consider this a debugging aid.
  963. var src bytes.Buffer
  964. s := bufio.NewScanner(bytes.NewReader(original))
  965. for line := 1; s.Scan(); line++ {
  966. fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
  967. }
  968. return nil, fmt.Errorf("%v: unparsable Go source: %v\n%v", g.filename, err, src.String())
  969. }
  970. // Collect a sorted list of all imports.
  971. var importPaths [][2]string
  972. rewriteImport := func(importPath string) string {
  973. if f := g.gen.opts.ImportRewriteFunc; f != nil {
  974. return string(f(GoImportPath(importPath)))
  975. }
  976. return importPath
  977. }
  978. for importPath := range g.packageNames {
  979. pkgName := string(g.packageNames[GoImportPath(importPath)])
  980. pkgPath := rewriteImport(string(importPath))
  981. importPaths = append(importPaths, [2]string{pkgName, pkgPath})
  982. }
  983. for importPath := range g.manualImports {
  984. if _, ok := g.packageNames[importPath]; !ok {
  985. pkgPath := rewriteImport(string(importPath))
  986. importPaths = append(importPaths, [2]string{"_", pkgPath})
  987. }
  988. }
  989. sort.Slice(importPaths, func(i, j int) bool {
  990. return importPaths[i][1] < importPaths[j][1]
  991. })
  992. // Modify the AST to include a new import block.
  993. if len(importPaths) > 0 {
  994. // Insert block after package statement or
  995. // possible comment attached to the end of the package statement.
  996. pos := file.Package
  997. tokFile := fset.File(file.Package)
  998. pkgLine := tokFile.Line(file.Package)
  999. for _, c := range file.Comments {
  1000. if tokFile.Line(c.Pos()) > pkgLine {
  1001. break
  1002. }
  1003. pos = c.End()
  1004. }
  1005. // Construct the import block.
  1006. impDecl := &ast.GenDecl{
  1007. Tok: token.IMPORT,
  1008. TokPos: pos,
  1009. Lparen: pos,
  1010. Rparen: pos,
  1011. }
  1012. for _, importPath := range importPaths {
  1013. impDecl.Specs = append(impDecl.Specs, &ast.ImportSpec{
  1014. Name: &ast.Ident{
  1015. Name: importPath[0],
  1016. NamePos: pos,
  1017. },
  1018. Path: &ast.BasicLit{
  1019. Kind: token.STRING,
  1020. Value: strconv.Quote(importPath[1]),
  1021. ValuePos: pos,
  1022. },
  1023. EndPos: pos,
  1024. })
  1025. }
  1026. file.Decls = append([]ast.Decl{impDecl}, file.Decls...)
  1027. }
  1028. var out bytes.Buffer
  1029. if err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(&out, fset, file); err != nil {
  1030. return nil, fmt.Errorf("%v: can not reformat Go source: %v", g.filename, err)
  1031. }
  1032. return out.Bytes(), nil
  1033. }
  1034. func (g *GeneratedFile) generatedCodeInfo(content []byte) (*descriptorpb.GeneratedCodeInfo, error) {
  1035. fset := token.NewFileSet()
  1036. astFile, err := parser.ParseFile(fset, "", content, 0)
  1037. if err != nil {
  1038. return nil, err
  1039. }
  1040. info := &descriptorpb.GeneratedCodeInfo{}
  1041. seenAnnotations := make(map[string]bool)
  1042. annotate := func(s string, ident *ast.Ident) {
  1043. seenAnnotations[s] = true
  1044. for _, a := range g.annotations[s] {
  1045. info.Annotation = append(info.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{
  1046. SourceFile: proto.String(a.Location.SourceFile),
  1047. Path: a.Location.Path,
  1048. Begin: proto.Int32(int32(fset.Position(ident.Pos()).Offset)),
  1049. End: proto.Int32(int32(fset.Position(ident.End()).Offset)),
  1050. Semantic: a.Semantic,
  1051. })
  1052. }
  1053. }
  1054. for _, decl := range astFile.Decls {
  1055. switch decl := decl.(type) {
  1056. case *ast.GenDecl:
  1057. for _, spec := range decl.Specs {
  1058. switch spec := spec.(type) {
  1059. case *ast.TypeSpec:
  1060. annotate(spec.Name.Name, spec.Name)
  1061. switch st := spec.Type.(type) {
  1062. case *ast.StructType:
  1063. for _, field := range st.Fields.List {
  1064. for _, name := range field.Names {
  1065. annotate(spec.Name.Name+"."+name.Name, name)
  1066. }
  1067. }
  1068. case *ast.InterfaceType:
  1069. for _, field := range st.Methods.List {
  1070. for _, name := range field.Names {
  1071. annotate(spec.Name.Name+"."+name.Name, name)
  1072. }
  1073. }
  1074. }
  1075. case *ast.ValueSpec:
  1076. for _, name := range spec.Names {
  1077. annotate(name.Name, name)
  1078. }
  1079. }
  1080. }
  1081. case *ast.FuncDecl:
  1082. if decl.Recv == nil {
  1083. annotate(decl.Name.Name, decl.Name)
  1084. } else {
  1085. recv := decl.Recv.List[0].Type
  1086. if s, ok := recv.(*ast.StarExpr); ok {
  1087. recv = s.X
  1088. }
  1089. if id, ok := recv.(*ast.Ident); ok {
  1090. annotate(id.Name+"."+decl.Name.Name, decl.Name)
  1091. }
  1092. }
  1093. }
  1094. }
  1095. for a := range g.annotations {
  1096. if !seenAnnotations[a] {
  1097. return nil, fmt.Errorf("%v: no symbol matching annotation %q", g.filename, a)
  1098. }
  1099. }
  1100. return info, nil
  1101. }
  1102. // metaFile returns the contents of the file's metadata file, which is a
  1103. // text formatted string of the google.protobuf.GeneratedCodeInfo.
  1104. func (g *GeneratedFile) metaFile(content []byte) (string, error) {
  1105. info, err := g.generatedCodeInfo(content)
  1106. if err != nil {
  1107. return "", err
  1108. }
  1109. b, err := prototext.Marshal(info)
  1110. if err != nil {
  1111. return "", err
  1112. }
  1113. return string(b), nil
  1114. }
  1115. // A GoIdent is a Go identifier, consisting of a name and import path.
  1116. // The name is a single identifier and may not be a dot-qualified selector.
  1117. type GoIdent struct {
  1118. GoName string
  1119. GoImportPath GoImportPath
  1120. }
  1121. func (id GoIdent) String() string { return fmt.Sprintf("%q.%v", id.GoImportPath, id.GoName) }
  1122. // newGoIdent returns the Go identifier for a descriptor.
  1123. func newGoIdent(f *File, d protoreflect.Descriptor) GoIdent {
  1124. name := strings.TrimPrefix(string(d.FullName()), string(f.Desc.Package())+".")
  1125. return GoIdent{
  1126. GoName: strs.GoCamelCase(name),
  1127. GoImportPath: f.GoImportPath,
  1128. }
  1129. }
  1130. // A GoImportPath is the import path of a Go package.
  1131. // For example: "google.golang.org/protobuf/compiler/protogen"
  1132. type GoImportPath string
  1133. func (p GoImportPath) String() string { return strconv.Quote(string(p)) }
  1134. // Ident returns a GoIdent with s as the GoName and p as the GoImportPath.
  1135. func (p GoImportPath) Ident(s string) GoIdent {
  1136. return GoIdent{GoName: s, GoImportPath: p}
  1137. }
  1138. // A GoPackageName is the name of a Go package. e.g., "protobuf".
  1139. type GoPackageName string
  1140. // cleanPackageName converts a string to a valid Go package name.
  1141. func cleanPackageName(name string) GoPackageName {
  1142. return GoPackageName(strs.GoSanitized(name))
  1143. }
  1144. type pathType int
  1145. const (
  1146. pathTypeImport pathType = iota
  1147. pathTypeSourceRelative
  1148. )
  1149. // A Location is a location in a .proto source file.
  1150. //
  1151. // See the google.protobuf.SourceCodeInfo documentation in descriptor.proto
  1152. // for details.
  1153. type Location struct {
  1154. SourceFile string
  1155. Path protoreflect.SourcePath
  1156. }
  1157. // appendPath add elements to a Location's path, returning a new Location.
  1158. func (loc Location) appendPath(num protoreflect.FieldNumber, idx int) Location {
  1159. loc.Path = append(protoreflect.SourcePath(nil), loc.Path...) // make copy
  1160. loc.Path = append(loc.Path, int32(num), int32(idx))
  1161. return loc
  1162. }
  1163. // CommentSet is a set of leading and trailing comments associated
  1164. // with a .proto descriptor declaration.
  1165. type CommentSet struct {
  1166. LeadingDetached []Comments
  1167. Leading Comments
  1168. Trailing Comments
  1169. }
  1170. func makeCommentSet(loc protoreflect.SourceLocation) CommentSet {
  1171. var leadingDetached []Comments
  1172. for _, s := range loc.LeadingDetachedComments {
  1173. leadingDetached = append(leadingDetached, Comments(s))
  1174. }
  1175. return CommentSet{
  1176. LeadingDetached: leadingDetached,
  1177. Leading: Comments(loc.LeadingComments),
  1178. Trailing: Comments(loc.TrailingComments),
  1179. }
  1180. }
  1181. // Comments is a comments string as provided by protoc.
  1182. type Comments string
  1183. // String formats the comments by inserting // to the start of each line,
  1184. // ensuring that there is a trailing newline.
  1185. // An empty comment is formatted as an empty string.
  1186. func (c Comments) String() string {
  1187. if c == "" {
  1188. return ""
  1189. }
  1190. var b []byte
  1191. for _, line := range strings.Split(strings.TrimSuffix(string(c), "\n"), "\n") {
  1192. b = append(b, "//"...)
  1193. b = append(b, line...)
  1194. b = append(b, "\n"...)
  1195. }
  1196. return string(b)
  1197. }
  1198. // extensionRegistry allows registration of new extensions defined in the .proto
  1199. // file for which we are generating bindings.
  1200. //
  1201. // Lookups consult the local type registry first and fall back to the base type
  1202. // registry which defaults to protoregistry.GlobalTypes.
  1203. type extensionRegistry struct {
  1204. base *protoregistry.Types
  1205. local *protoregistry.Types
  1206. }
  1207. func newExtensionRegistry() *extensionRegistry {
  1208. return &extensionRegistry{
  1209. base: protoregistry.GlobalTypes,
  1210. local: &protoregistry.Types{},
  1211. }
  1212. }
  1213. // FindExtensionByName implements proto.UnmarshalOptions.FindExtensionByName
  1214. func (e *extensionRegistry) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
  1215. if xt, err := e.local.FindExtensionByName(field); err == nil {
  1216. return xt, nil
  1217. }
  1218. return e.base.FindExtensionByName(field)
  1219. }
  1220. // FindExtensionByNumber implements proto.UnmarshalOptions.FindExtensionByNumber
  1221. func (e *extensionRegistry) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
  1222. if xt, err := e.local.FindExtensionByNumber(message, field); err == nil {
  1223. return xt, nil
  1224. }
  1225. return e.base.FindExtensionByNumber(message, field)
  1226. }
  1227. func (e *extensionRegistry) hasNovelExtensions() bool {
  1228. return e.local.NumExtensions() > 0
  1229. }
  1230. func (e *extensionRegistry) registerAllExtensionsFromFile(f protoreflect.FileDescriptor) error {
  1231. if err := e.registerAllExtensions(f.Extensions()); err != nil {
  1232. return err
  1233. }
  1234. return nil
  1235. }
  1236. func (e *extensionRegistry) registerAllExtensionsFromMessage(ms protoreflect.MessageDescriptors) error {
  1237. for i := 0; i < ms.Len(); i++ {
  1238. m := ms.Get(i)
  1239. if err := e.registerAllExtensions(m.Extensions()); err != nil {
  1240. return err
  1241. }
  1242. }
  1243. return nil
  1244. }
  1245. func (e *extensionRegistry) registerAllExtensions(exts protoreflect.ExtensionDescriptors) error {
  1246. for i := 0; i < exts.Len(); i++ {
  1247. if err := e.registerExtension(exts.Get(i)); err != nil {
  1248. return err
  1249. }
  1250. }
  1251. return nil
  1252. }
  1253. // registerExtension adds the given extension to the type registry if an
  1254. // extension with that full name does not exist yet.
  1255. func (e *extensionRegistry) registerExtension(xd protoreflect.ExtensionDescriptor) error {
  1256. if _, err := e.FindExtensionByName(xd.FullName()); err != protoregistry.NotFound {
  1257. // Either the extension already exists or there was an error, either way we're done.
  1258. return err
  1259. }
  1260. return e.local.RegisterExtension(dynamicpb.NewExtensionType(xd))
  1261. }