blob: 5e1cd837e2a0b4036088513949a3dc8acc44e14b [file] [log] [blame]
Damien Neil220c2022018-08-15 11:24:18 -07001// 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
5// Package protogen provides support for writing protoc plugins.
6//
7// Plugins for protoc, the Protocol Buffers Compiler, are programs which read
8// a CodeGeneratorRequest protocol buffer from standard input and write a
9// CodeGeneratorResponse protocol buffer to standard output. This package
10// provides support for writing plugins which generate Go code.
11package protogen
12
13import (
Damien Neilc7d07d92018-08-22 13:46:02 -070014 "bufio"
Damien Neil220c2022018-08-15 11:24:18 -070015 "bytes"
16 "fmt"
Damien Neilc7d07d92018-08-22 13:46:02 -070017 "go/parser"
18 "go/printer"
19 "go/token"
Damien Neil220c2022018-08-15 11:24:18 -070020 "io/ioutil"
21 "os"
Damien Neil082ce922018-09-06 10:23:53 -070022 "path"
Damien Neil220c2022018-08-15 11:24:18 -070023 "path/filepath"
Damien Neild9016772018-08-23 14:39:30 -070024 "sort"
25 "strconv"
Damien Neil220c2022018-08-15 11:24:18 -070026 "strings"
27
28 "github.com/golang/protobuf/proto"
29 descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
30 pluginpb "github.com/golang/protobuf/protoc-gen-go/plugin"
Damien Neild9016772018-08-23 14:39:30 -070031 "golang.org/x/tools/go/ast/astutil"
Damien Neilabc6fc12018-08-23 14:39:30 -070032 "google.golang.org/proto/reflect/protoreflect"
33 "google.golang.org/proto/reflect/protoregistry"
34 "google.golang.org/proto/reflect/prototype"
Damien Neil220c2022018-08-15 11:24:18 -070035)
36
37// Run executes a function as a protoc plugin.
38//
39// It reads a CodeGeneratorRequest message from os.Stdin, invokes the plugin
40// function, and writes a CodeGeneratorResponse message to os.Stdout.
41//
42// If a failure occurs while reading or writing, Run prints an error to
43// os.Stderr and calls os.Exit(1).
Damien Neil3cf6e622018-09-11 13:53:14 -070044//
45// Passing a nil options is equivalent to passing a zero-valued one.
46func Run(opts *Options, f func(*Plugin) error) {
47 if err := run(opts, f); err != nil {
Damien Neil220c2022018-08-15 11:24:18 -070048 fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err)
49 os.Exit(1)
50 }
51}
52
Damien Neil3cf6e622018-09-11 13:53:14 -070053func run(opts *Options, f func(*Plugin) error) error {
Damien Neil220c2022018-08-15 11:24:18 -070054 in, err := ioutil.ReadAll(os.Stdin)
55 if err != nil {
56 return err
57 }
58 req := &pluginpb.CodeGeneratorRequest{}
59 if err := proto.Unmarshal(in, req); err != nil {
60 return err
61 }
Damien Neil3cf6e622018-09-11 13:53:14 -070062 gen, err := New(req, opts)
Damien Neil220c2022018-08-15 11:24:18 -070063 if err != nil {
64 return err
65 }
66 if err := f(gen); err != nil {
67 // Errors from the plugin function are reported by setting the
68 // error field in the CodeGeneratorResponse.
69 //
70 // In contrast, errors that indicate a problem in protoc
71 // itself (unparsable input, I/O errors, etc.) are reported
72 // to stderr.
73 gen.Error(err)
74 }
75 resp := gen.Response()
76 out, err := proto.Marshal(resp)
77 if err != nil {
78 return err
79 }
80 if _, err := os.Stdout.Write(out); err != nil {
81 return err
82 }
83 return nil
84}
85
86// A Plugin is a protoc plugin invocation.
87type Plugin struct {
88 // Request is the CodeGeneratorRequest provided by protoc.
89 Request *pluginpb.CodeGeneratorRequest
90
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 filesByName map[string]*File
96
Damien Neil658051b2018-09-10 12:26:21 -070097 fileReg *protoregistry.Files
98 messagesByName map[protoreflect.FullName]*Message
99 enumsByName map[protoreflect.FullName]*Enum
100 pathType pathType
101 genFiles []*GeneratedFile
102 err error
Damien Neil220c2022018-08-15 11:24:18 -0700103}
104
Damien Neil3cf6e622018-09-11 13:53:14 -0700105// Options are optional parameters to New.
106type Options struct {
107 // If ParamFunc is non-nil, it will be called with each unknown
108 // generator parameter.
109 //
110 // Plugins for protoc can accept parameters from the command line,
111 // passed in the --<lang>_out protoc, separated from the output
112 // directory with a colon; e.g.,
113 //
114 // --go_out=<param1>=<value1>,<param2>=<value2>:<output_directory>
115 //
116 // Parameters passed in this fashion as a comma-separated list of
117 // key=value pairs will be passed to the ParamFunc.
118 //
119 // The (flag.FlagSet).Set method matches this function signature,
120 // so parameters can be converted into flags as in the following:
121 //
122 // var flags flag.FlagSet
123 // value := flags.Bool("param", false, "")
124 // opts := &protogen.Options{
125 // ParamFunc: flags.Set,
126 // }
127 // protogen.Run(opts, func(p *protogen.Plugin) error {
128 // if *value { ... }
129 // })
130 ParamFunc func(name, value string) error
131}
132
Damien Neil220c2022018-08-15 11:24:18 -0700133// New returns a new Plugin.
Damien Neil3cf6e622018-09-11 13:53:14 -0700134//
135// Passing a nil Options is equivalent to passing a zero-valued one.
136func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
137 if opts == nil {
138 opts = &Options{}
139 }
Damien Neil220c2022018-08-15 11:24:18 -0700140 gen := &Plugin{
Damien Neil658051b2018-09-10 12:26:21 -0700141 Request: req,
142 filesByName: make(map[string]*File),
143 fileReg: protoregistry.NewFiles(),
144 messagesByName: make(map[protoreflect.FullName]*Message),
145 enumsByName: make(map[protoreflect.FullName]*Enum),
Damien Neil220c2022018-08-15 11:24:18 -0700146 }
147
Damien Neil082ce922018-09-06 10:23:53 -0700148 packageNames := make(map[string]GoPackageName) // filename -> package name
149 importPaths := make(map[string]GoImportPath) // filename -> import path
150 var packageImportPath GoImportPath
Damien Neil220c2022018-08-15 11:24:18 -0700151 for _, param := range strings.Split(req.GetParameter(), ",") {
152 var value string
153 if i := strings.Index(param, "="); i >= 0 {
154 value = param[i+1:]
155 param = param[0:i]
156 }
157 switch param {
158 case "":
159 // Ignore.
160 case "import_prefix":
161 // TODO
162 case "import_path":
Damien Neil082ce922018-09-06 10:23:53 -0700163 packageImportPath = GoImportPath(value)
Damien Neil220c2022018-08-15 11:24:18 -0700164 case "paths":
Damien Neil082ce922018-09-06 10:23:53 -0700165 switch value {
166 case "import":
167 gen.pathType = pathTypeImport
168 case "source_relative":
169 gen.pathType = pathTypeSourceRelative
170 default:
171 return nil, fmt.Errorf(`unknown path type %q: want "import" or "source_relative"`, value)
172 }
Damien Neil220c2022018-08-15 11:24:18 -0700173 case "annotate_code":
174 // TODO
175 default:
Damien Neil3cf6e622018-09-11 13:53:14 -0700176 if param[0] == 'M' {
177 importPaths[param[1:]] = GoImportPath(value)
178 continue
Damien Neil220c2022018-08-15 11:24:18 -0700179 }
Damien Neil3cf6e622018-09-11 13:53:14 -0700180 if opts.ParamFunc != nil {
181 if err := opts.ParamFunc(param, value); err != nil {
182 return nil, err
183 }
184 }
Damien Neil082ce922018-09-06 10:23:53 -0700185 }
186 }
187
188 // Figure out the import path and package name for each file.
189 //
190 // The rules here are complicated and have grown organically over time.
191 // Interactions between different ways of specifying package information
192 // may be surprising.
193 //
194 // The recommended approach is to include a go_package option in every
195 // .proto source file specifying the full import path of the Go package
196 // associated with this file.
197 //
198 // option go_package = "github.com/golang/protobuf/ptypes/any";
199 //
200 // Build systems which want to exert full control over import paths may
201 // specify M<filename>=<import_path> flags.
202 //
203 // Other approaches are not recommend.
204 generatedFileNames := make(map[string]bool)
205 for _, name := range gen.Request.FileToGenerate {
206 generatedFileNames[name] = true
207 }
208 // We need to determine the import paths before the package names,
209 // because the Go package name for a file is sometimes derived from
210 // different file in the same package.
211 packageNameForImportPath := make(map[GoImportPath]GoPackageName)
212 for _, fdesc := range gen.Request.ProtoFile {
213 filename := fdesc.GetName()
214 packageName, importPath := goPackageOption(fdesc)
215 switch {
216 case importPaths[filename] != "":
217 // Command line: M=foo.proto=quux/bar
218 //
219 // Explicit mapping of source file to import path.
220 case generatedFileNames[filename] && packageImportPath != "":
221 // Command line: import_path=quux/bar
222 //
223 // The import_path flag sets the import path for every file that
224 // we generate code for.
225 importPaths[filename] = packageImportPath
226 case importPath != "":
227 // Source file: option go_package = "quux/bar";
228 //
229 // The go_package option sets the import path. Most users should use this.
230 importPaths[filename] = importPath
231 default:
232 // Source filename.
233 //
234 // Last resort when nothing else is available.
235 importPaths[filename] = GoImportPath(path.Dir(filename))
236 }
237 if packageName != "" {
238 packageNameForImportPath[importPaths[filename]] = packageName
239 }
240 }
241 for _, fdesc := range gen.Request.ProtoFile {
242 filename := fdesc.GetName()
243 packageName, _ := goPackageOption(fdesc)
244 defaultPackageName := packageNameForImportPath[importPaths[filename]]
245 switch {
246 case packageName != "":
247 // Source file: option go_package = "quux/bar";
248 packageNames[filename] = packageName
249 case defaultPackageName != "":
250 // A go_package option in another file in the same package.
251 //
252 // This is a poor choice in general, since every source file should
253 // contain a go_package option. Supported mainly for historical
254 // compatibility.
255 packageNames[filename] = defaultPackageName
256 case generatedFileNames[filename] && packageImportPath != "":
257 // Command line: import_path=quux/bar
258 packageNames[filename] = cleanPackageName(path.Base(string(packageImportPath)))
259 case fdesc.GetPackage() != "":
260 // Source file: package quux.bar;
261 packageNames[filename] = cleanPackageName(fdesc.GetPackage())
262 default:
263 // Source filename.
264 packageNames[filename] = cleanPackageName(baseName(filename))
265 }
266 }
267
268 // Consistency check: Every file with the same Go import path should have
269 // the same Go package name.
270 packageFiles := make(map[GoImportPath][]string)
271 for filename, importPath := range importPaths {
272 packageFiles[importPath] = append(packageFiles[importPath], filename)
273 }
274 for importPath, filenames := range packageFiles {
275 for i := 1; i < len(filenames); i++ {
276 if a, b := packageNames[filenames[0]], packageNames[filenames[i]]; a != b {
277 return nil, fmt.Errorf("Go package %v has inconsistent names %v (%v) and %v (%v)",
278 importPath, a, filenames[0], b, filenames[i])
279 }
Damien Neil220c2022018-08-15 11:24:18 -0700280 }
281 }
282
283 for _, fdesc := range gen.Request.ProtoFile {
Damien Neil082ce922018-09-06 10:23:53 -0700284 filename := fdesc.GetName()
285 if gen.filesByName[filename] != nil {
286 return nil, fmt.Errorf("duplicate file name: %q", filename)
287 }
288 f, err := newFile(gen, fdesc, packageNames[filename], importPaths[filename])
Damien Neilabc6fc12018-08-23 14:39:30 -0700289 if err != nil {
290 return nil, err
291 }
Damien Neil220c2022018-08-15 11:24:18 -0700292 gen.Files = append(gen.Files, f)
Damien Neil082ce922018-09-06 10:23:53 -0700293 gen.filesByName[filename] = f
Damien Neil220c2022018-08-15 11:24:18 -0700294 }
Damien Neil082ce922018-09-06 10:23:53 -0700295 for _, filename := range gen.Request.FileToGenerate {
296 f, ok := gen.FileByName(filename)
Damien Neil220c2022018-08-15 11:24:18 -0700297 if !ok {
Damien Neil082ce922018-09-06 10:23:53 -0700298 return nil, fmt.Errorf("no descriptor for generated file: %v", filename)
Damien Neil220c2022018-08-15 11:24:18 -0700299 }
300 f.Generate = true
301 }
302 return gen, nil
303}
304
305// Error records an error in code generation. The generator will report the
306// error back to protoc and will not produce output.
307func (gen *Plugin) Error(err error) {
308 if gen.err == nil {
309 gen.err = err
310 }
311}
312
313// Response returns the generator output.
314func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse {
315 resp := &pluginpb.CodeGeneratorResponse{}
316 if gen.err != nil {
317 resp.Error = proto.String(gen.err.Error())
318 return resp
319 }
320 for _, gf := range gen.genFiles {
Damien Neilc7d07d92018-08-22 13:46:02 -0700321 content, err := gf.Content()
322 if err != nil {
323 return &pluginpb.CodeGeneratorResponse{
324 Error: proto.String(err.Error()),
325 }
326 }
Damien Neil220c2022018-08-15 11:24:18 -0700327 resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
Damien Neild9016772018-08-23 14:39:30 -0700328 Name: proto.String(gf.filename),
Damien Neilc7d07d92018-08-22 13:46:02 -0700329 Content: proto.String(string(content)),
Damien Neil220c2022018-08-15 11:24:18 -0700330 })
331 }
332 return resp
333}
334
335// FileByName returns the file with the given name.
336func (gen *Plugin) FileByName(name string) (f *File, ok bool) {
337 f, ok = gen.filesByName[name]
338 return f, ok
339}
340
Damien Neilc7d07d92018-08-22 13:46:02 -0700341// A File describes a .proto source file.
Damien Neil220c2022018-08-15 11:24:18 -0700342type File struct {
Damien Neil7779e052018-09-07 14:14:06 -0700343 Desc protoreflect.FileDescriptor
344 Proto *descpb.FileDescriptorProto
Damien Neil220c2022018-08-15 11:24:18 -0700345
Damien Neil082ce922018-09-06 10:23:53 -0700346 GoPackageName GoPackageName // name of this file's Go package
347 GoImportPath GoImportPath // import path of this file's Go package
348 Messages []*Message // top-level message declarations
Damien Neil46abb572018-09-07 12:45:37 -0700349 Enums []*Enum // top-level enum declarations
Damien Neil082ce922018-09-06 10:23:53 -0700350 Generate bool // true if we should generate code for this file
351
352 // GeneratedFilenamePrefix is used to construct filenames for generated
353 // files associated with this source file.
354 //
355 // For example, the source file "dir/foo.proto" might have a filename prefix
356 // of "dir/foo". Appending ".pb.go" produces an output file of "dir/foo.pb.go".
357 GeneratedFilenamePrefix string
Damien Neil220c2022018-08-15 11:24:18 -0700358}
359
Damien Neil082ce922018-09-06 10:23:53 -0700360func newFile(gen *Plugin, p *descpb.FileDescriptorProto, packageName GoPackageName, importPath GoImportPath) (*File, error) {
Damien Neilabc6fc12018-08-23 14:39:30 -0700361 desc, err := prototype.NewFileFromDescriptorProto(p, gen.fileReg)
362 if err != nil {
363 return nil, fmt.Errorf("invalid FileDescriptorProto %q: %v", p.GetName(), err)
364 }
365 if err := gen.fileReg.Register(desc); err != nil {
366 return nil, fmt.Errorf("cannot register descriptor %q: %v", p.GetName(), err)
367 }
Damien Neilc7d07d92018-08-22 13:46:02 -0700368 f := &File{
Damien Neil082ce922018-09-06 10:23:53 -0700369 Desc: desc,
Damien Neil7779e052018-09-07 14:14:06 -0700370 Proto: p,
Damien Neil082ce922018-09-06 10:23:53 -0700371 GoPackageName: packageName,
372 GoImportPath: importPath,
Damien Neil220c2022018-08-15 11:24:18 -0700373 }
Damien Neil082ce922018-09-06 10:23:53 -0700374
375 // Determine the prefix for generated Go files.
376 prefix := p.GetName()
377 if ext := path.Ext(prefix); ext == ".proto" || ext == ".protodevel" {
378 prefix = prefix[:len(prefix)-len(ext)]
379 }
380 if gen.pathType == pathTypeImport {
381 // If paths=import (the default) and the file contains a go_package option
382 // with a full import path, the output filename is derived from the Go import
383 // path.
384 //
385 // Pass the paths=source_relative flag to always derive the output filename
386 // from the input filename instead.
387 if _, importPath := goPackageOption(p); importPath != "" {
388 prefix = path.Join(string(importPath), path.Base(prefix))
389 }
390 }
391 f.GeneratedFilenamePrefix = prefix
392
Damien Neilabc6fc12018-08-23 14:39:30 -0700393 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil658051b2018-09-10 12:26:21 -0700394 message, err := newMessage(gen, f, nil, mdescs.Get(i))
395 if err != nil {
396 return nil, err
397 }
398 f.Messages = append(f.Messages, message)
Damien Neilc7d07d92018-08-22 13:46:02 -0700399 }
Damien Neil46abb572018-09-07 12:45:37 -0700400 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
401 f.Enums = append(f.Enums, newEnum(gen, f, nil, edescs.Get(i)))
402 }
Damien Neilabc6fc12018-08-23 14:39:30 -0700403 return f, nil
Damien Neilc7d07d92018-08-22 13:46:02 -0700404}
405
Damien Neil082ce922018-09-06 10:23:53 -0700406// goPackageOption interprets a file's go_package option.
407// If there is no go_package, it returns ("", "").
408// If there's a simple name, it returns (pkg, "").
409// If the option implies an import path, it returns (pkg, impPath).
410func goPackageOption(d *descpb.FileDescriptorProto) (pkg GoPackageName, impPath GoImportPath) {
411 opt := d.GetOptions().GetGoPackage()
412 if opt == "" {
413 return "", ""
414 }
415 // A semicolon-delimited suffix delimits the import path and package name.
416 if i := strings.Index(opt, ";"); i >= 0 {
417 return cleanPackageName(opt[i+1:]), GoImportPath(opt[:i])
418 }
419 // The presence of a slash implies there's an import path.
420 if i := strings.LastIndex(opt, "/"); i >= 0 {
421 return cleanPackageName(opt[i+1:]), GoImportPath(opt)
422 }
423 return cleanPackageName(opt), ""
424}
425
Damien Neilc7d07d92018-08-22 13:46:02 -0700426// A Message describes a message.
427type Message struct {
Damien Neilabc6fc12018-08-23 14:39:30 -0700428 Desc protoreflect.MessageDescriptor
Damien Neilc7d07d92018-08-22 13:46:02 -0700429
430 GoIdent GoIdent // name of the generated Go type
Damien Neil658051b2018-09-10 12:26:21 -0700431 Fields []*Field // message field declarations
Damien Neilc7d07d92018-08-22 13:46:02 -0700432 Messages []*Message // nested message declarations
Damien Neil46abb572018-09-07 12:45:37 -0700433 Enums []*Enum // nested enum declarations
Damien Neilcab8dfe2018-09-06 14:51:28 -0700434 Path []int32 // location path of this message
Damien Neilc7d07d92018-08-22 13:46:02 -0700435}
436
Damien Neil658051b2018-09-10 12:26:21 -0700437func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) (*Message, error) {
Damien Neilcab8dfe2018-09-06 14:51:28 -0700438 var path []int32
439 if parent != nil {
440 path = pathAppend(parent.Path, messageMessageField, int32(desc.Index()))
441 } else {
442 path = []int32{fileMessageField, int32(desc.Index())}
443 }
Damien Neil46abb572018-09-07 12:45:37 -0700444 message := &Message{
Damien Neilabc6fc12018-08-23 14:39:30 -0700445 Desc: desc,
446 GoIdent: newGoIdent(f, desc),
Damien Neilcab8dfe2018-09-06 14:51:28 -0700447 Path: path,
Damien Neilc7d07d92018-08-22 13:46:02 -0700448 }
Damien Neil658051b2018-09-10 12:26:21 -0700449 gen.messagesByName[desc.FullName()] = message
Damien Neilabc6fc12018-08-23 14:39:30 -0700450 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil658051b2018-09-10 12:26:21 -0700451 m, err := newMessage(gen, f, message, mdescs.Get(i))
452 if err != nil {
453 return nil, err
454 }
455 message.Messages = append(message.Messages, m)
Damien Neilc7d07d92018-08-22 13:46:02 -0700456 }
Damien Neil46abb572018-09-07 12:45:37 -0700457 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
458 message.Enums = append(message.Enums, newEnum(gen, f, message, edescs.Get(i)))
459 }
Damien Neil658051b2018-09-10 12:26:21 -0700460 for i, fdescs := 0, desc.Fields(); i < fdescs.Len(); i++ {
461 field, err := newField(gen, f, message, fdescs.Get(i))
462 if err != nil {
463 return nil, err
464 }
465 message.Fields = append(message.Fields, field)
466 }
467
468 // Field name conflict resolution.
469 //
470 // We assume well-known method names that may be attached to a generated
471 // message type, as well as a 'Get*' method for each field. For each
472 // field in turn, we add _s to its name until there are no conflicts.
473 //
474 // Any change to the following set of method names is a potential
475 // incompatible API change because it may change generated field names.
476 //
477 // TODO: If we ever support a 'go_name' option to set the Go name of a
478 // field, we should consider dropping this entirely. The conflict
479 // resolution algorithm is subtle and surprising (changing the order
480 // in which fields appear in the .proto source file can change the
481 // names of fields in generated code), and does not adapt well to
482 // adding new per-field methods such as setters.
483 usedNames := map[string]bool{
484 "Reset": true,
485 "String": true,
486 "ProtoMessage": true,
487 "Marshal": true,
488 "Unmarshal": true,
489 "ExtensionRangeArray": true,
490 "ExtensionMap": true,
491 "Descriptor": true,
492 }
493 makeNameUnique := func(name string) string {
494 for usedNames[name] || usedNames["Get"+name] {
495 name += "_"
496 }
497 usedNames[name] = true
498 usedNames["Get"+name] = true
499 return name
500 }
501 for _, field := range message.Fields {
502 field.GoIdent.GoName = makeNameUnique(field.GoIdent.GoName)
503 // TODO: If this is the first field of a oneof that we haven't seen
504 // before, generate the name for the oneof.
505 }
506
507 return message, nil
508}
509
510// A Field describes a message field.
511type Field struct {
512 Desc protoreflect.FieldDescriptor
513
514 // GoIdent is the base name of this field's Go fields and methods.
515 // For code generated by protoc-gen-go, this means a field named
516 // '{{GoIdent}}' and a getter method named 'Get{{GoIdent}}'.
517 GoIdent GoIdent
518
519 MessageType *Message // type for message or group fields; nil otherwise
520 EnumType *Enum // type for enum fields; nil otherwise
521 Path []int32 // location path of this field
522}
523
524func newField(gen *Plugin, f *File, message *Message, desc protoreflect.FieldDescriptor) (*Field, error) {
525 field := &Field{
526 Desc: desc,
527 GoIdent: GoIdent{
528 GoName: camelCase(string(desc.Name())),
529 GoImportPath: f.GoImportPath,
530 },
531 Path: pathAppend(message.Path, messageFieldField, int32(desc.Index())),
532 }
533 switch desc.Kind() {
534 case protoreflect.MessageKind, protoreflect.GroupKind:
535 mname := desc.MessageType().FullName()
536 message, ok := gen.messagesByName[mname]
537 if !ok {
538 return nil, fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), mname)
539 }
540 field.MessageType = message
541 case protoreflect.EnumKind:
542 ename := desc.EnumType().FullName()
543 enum, ok := gen.enumsByName[ename]
544 if !ok {
545 return nil, fmt.Errorf("field %v: no descriptor for enum %v", desc.FullName(), ename)
546 }
547 field.EnumType = enum
548 }
549 return field, nil
Damien Neil46abb572018-09-07 12:45:37 -0700550}
551
552// An Enum describes an enum.
553type Enum struct {
554 Desc protoreflect.EnumDescriptor
555
556 GoIdent GoIdent // name of the generated Go type
557 Values []*EnumValue // enum values
558 Path []int32 // location path of this enum
559}
560
561func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum {
562 var path []int32
563 if parent != nil {
564 path = pathAppend(parent.Path, messageEnumField, int32(desc.Index()))
565 } else {
566 path = []int32{fileEnumField, int32(desc.Index())}
567 }
568 enum := &Enum{
569 Desc: desc,
570 GoIdent: newGoIdent(f, desc),
571 Path: path,
572 }
Damien Neil658051b2018-09-10 12:26:21 -0700573 gen.enumsByName[desc.FullName()] = enum
Damien Neil46abb572018-09-07 12:45:37 -0700574 for i, evdescs := 0, enum.Desc.Values(); i < evdescs.Len(); i++ {
575 enum.Values = append(enum.Values, newEnumValue(gen, f, parent, enum, evdescs.Get(i)))
576 }
577 return enum
578}
579
580// An EnumValue describes an enum value.
581type EnumValue struct {
582 Desc protoreflect.EnumValueDescriptor
583
584 GoIdent GoIdent // name of the generated Go type
585 Path []int32 // location path of this enum value
586}
587
588func newEnumValue(gen *Plugin, f *File, message *Message, enum *Enum, desc protoreflect.EnumValueDescriptor) *EnumValue {
589 // A top-level enum value's name is: EnumName_ValueName
590 // An enum value contained in a message is: MessageName_ValueName
591 //
592 // Enum value names are not camelcased.
593 parentIdent := enum.GoIdent
594 if message != nil {
595 parentIdent = message.GoIdent
596 }
597 name := parentIdent.GoName + "_" + string(desc.Name())
598 return &EnumValue{
599 Desc: desc,
600 GoIdent: GoIdent{
601 GoName: name,
602 GoImportPath: f.GoImportPath,
603 },
604 Path: pathAppend(enum.Path, enumValueField, int32(desc.Index())),
605 }
Damien Neil220c2022018-08-15 11:24:18 -0700606}
607
608// A GeneratedFile is a generated file.
609type GeneratedFile struct {
Damien Neild9016772018-08-23 14:39:30 -0700610 filename string
611 goImportPath GoImportPath
612 buf bytes.Buffer
613 packageNames map[GoImportPath]GoPackageName
614 usedPackageNames map[GoPackageName]bool
Damien Neil220c2022018-08-15 11:24:18 -0700615}
616
Damien Neild9016772018-08-23 14:39:30 -0700617// NewGeneratedFile creates a new generated file with the given filename
618// and import path.
619func (gen *Plugin) NewGeneratedFile(filename string, goImportPath GoImportPath) *GeneratedFile {
Damien Neil220c2022018-08-15 11:24:18 -0700620 g := &GeneratedFile{
Damien Neild9016772018-08-23 14:39:30 -0700621 filename: filename,
622 goImportPath: goImportPath,
623 packageNames: make(map[GoImportPath]GoPackageName),
624 usedPackageNames: make(map[GoPackageName]bool),
Damien Neil220c2022018-08-15 11:24:18 -0700625 }
626 gen.genFiles = append(gen.genFiles, g)
627 return g
628}
629
630// P prints a line to the generated output. It converts each parameter to a
631// string following the same rules as fmt.Print. It never inserts spaces
632// between parameters.
633//
634// TODO: .meta file annotations.
635func (g *GeneratedFile) P(v ...interface{}) {
636 for _, x := range v {
Damien Neild9016772018-08-23 14:39:30 -0700637 switch x := x.(type) {
638 case GoIdent:
Damien Neil46abb572018-09-07 12:45:37 -0700639 fmt.Fprint(&g.buf, g.QualifiedGoIdent(x))
Damien Neild9016772018-08-23 14:39:30 -0700640 default:
641 fmt.Fprint(&g.buf, x)
642 }
Damien Neil220c2022018-08-15 11:24:18 -0700643 }
644 fmt.Fprintln(&g.buf)
645}
646
Damien Neil46abb572018-09-07 12:45:37 -0700647// QualifiedGoIdent returns the string to use for a Go identifier.
648//
649// If the identifier is from a different Go package than the generated file,
650// the returned name will be qualified (package.name) and an import statement
651// for the identifier's package will be included in the file.
652func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string {
653 if ident.GoImportPath == g.goImportPath {
654 return ident.GoName
655 }
656 if packageName, ok := g.packageNames[ident.GoImportPath]; ok {
657 return string(packageName) + "." + ident.GoName
658 }
659 packageName := cleanPackageName(baseName(string(ident.GoImportPath)))
660 for i, orig := 1, packageName; g.usedPackageNames[packageName]; i++ {
661 packageName = orig + GoPackageName(strconv.Itoa(i))
662 }
663 g.packageNames[ident.GoImportPath] = packageName
664 g.usedPackageNames[packageName] = true
665 return string(packageName) + "." + ident.GoName
666}
667
Damien Neil220c2022018-08-15 11:24:18 -0700668// Write implements io.Writer.
669func (g *GeneratedFile) Write(p []byte) (n int, err error) {
670 return g.buf.Write(p)
671}
672
673// Content returns the contents of the generated file.
Damien Neilc7d07d92018-08-22 13:46:02 -0700674func (g *GeneratedFile) Content() ([]byte, error) {
Damien Neild9016772018-08-23 14:39:30 -0700675 if !strings.HasSuffix(g.filename, ".go") {
Damien Neilc7d07d92018-08-22 13:46:02 -0700676 return g.buf.Bytes(), nil
677 }
678
679 // Reformat generated code.
680 original := g.buf.Bytes()
681 fset := token.NewFileSet()
682 ast, err := parser.ParseFile(fset, "", original, parser.ParseComments)
683 if err != nil {
684 // Print out the bad code with line numbers.
685 // This should never happen in practice, but it can while changing generated code
686 // so consider this a debugging aid.
687 var src bytes.Buffer
688 s := bufio.NewScanner(bytes.NewReader(original))
689 for line := 1; s.Scan(); line++ {
690 fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
691 }
Damien Neild9016772018-08-23 14:39:30 -0700692 return nil, fmt.Errorf("%v: unparsable Go source: %v\n%v", g.filename, err, src.String())
Damien Neilc7d07d92018-08-22 13:46:02 -0700693 }
Damien Neild9016772018-08-23 14:39:30 -0700694
695 // Add imports.
696 var importPaths []string
697 for importPath := range g.packageNames {
698 importPaths = append(importPaths, string(importPath))
699 }
700 sort.Strings(importPaths)
701 for _, importPath := range importPaths {
702 astutil.AddNamedImport(fset, ast, string(g.packageNames[GoImportPath(importPath)]), importPath)
703 }
704
Damien Neilc7d07d92018-08-22 13:46:02 -0700705 var out bytes.Buffer
706 if err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(&out, fset, ast); err != nil {
Damien Neild9016772018-08-23 14:39:30 -0700707 return nil, fmt.Errorf("%v: can not reformat Go source: %v", g.filename, err)
Damien Neilc7d07d92018-08-22 13:46:02 -0700708 }
Damien Neild9016772018-08-23 14:39:30 -0700709 // TODO: Annotations.
Damien Neilc7d07d92018-08-22 13:46:02 -0700710 return out.Bytes(), nil
711
Damien Neil220c2022018-08-15 11:24:18 -0700712}
Damien Neil082ce922018-09-06 10:23:53 -0700713
714type pathType int
715
716const (
717 pathTypeImport pathType = iota
718 pathTypeSourceRelative
719)
Damien Neilcab8dfe2018-09-06 14:51:28 -0700720
721// The SourceCodeInfo message describes the location of elements of a parsed
722// .proto file by way of a "path", which is a sequence of integers that
723// describe the route from a FileDescriptorProto to the relevant submessage.
724// The path alternates between a field number of a repeated field, and an index
725// into that repeated field. The constants below define the field numbers that
726// are used.
727//
728// See descriptor.proto for more information about this.
729const (
730 // field numbers in FileDescriptorProto
731 filePackageField = 2 // package
732 fileMessageField = 4 // message_type
Damien Neil46abb572018-09-07 12:45:37 -0700733 fileEnumField = 5 // enum_type
Damien Neilcab8dfe2018-09-06 14:51:28 -0700734 // field numbers in DescriptorProto
735 messageFieldField = 2 // field
736 messageMessageField = 3 // nested_type
737 messageEnumField = 4 // enum_type
738 messageOneofField = 8 // oneof_decl
739 // field numbers in EnumDescriptorProto
740 enumValueField = 2 // value
741)
742
743// pathAppend appends elements to a location path.
744// It does not alias the original path.
745func pathAppend(path []int32, a ...int32) []int32 {
746 var n []int32
747 n = append(n, path...)
748 n = append(n, a...)
749 return n
750}