blob: 753325e1ea121e3ae077dff7d23f569a6e6b320e [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 Neil1ec33152018-09-13 13:12:36 -070017 "go/ast"
Damien Neilc7d07d92018-08-22 13:46:02 -070018 "go/parser"
19 "go/printer"
20 "go/token"
Damien Neil220c2022018-08-15 11:24:18 -070021 "io/ioutil"
22 "os"
Damien Neil082ce922018-09-06 10:23:53 -070023 "path"
Damien Neil220c2022018-08-15 11:24:18 -070024 "path/filepath"
Damien Neild9016772018-08-23 14:39:30 -070025 "sort"
26 "strconv"
Damien Neil220c2022018-08-15 11:24:18 -070027 "strings"
28
29 "github.com/golang/protobuf/proto"
30 descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
31 pluginpb "github.com/golang/protobuf/protoc-gen-go/plugin"
Damien Neild9016772018-08-23 14:39:30 -070032 "golang.org/x/tools/go/ast/astutil"
Damien Neilabc6fc12018-08-23 14:39:30 -070033 "google.golang.org/proto/reflect/protoreflect"
34 "google.golang.org/proto/reflect/protoregistry"
35 "google.golang.org/proto/reflect/prototype"
Damien Neil220c2022018-08-15 11:24:18 -070036)
37
38// Run executes a function as a protoc plugin.
39//
40// It reads a CodeGeneratorRequest message from os.Stdin, invokes the plugin
41// function, and writes a CodeGeneratorResponse message to os.Stdout.
42//
43// If a failure occurs while reading or writing, Run prints an error to
44// os.Stderr and calls os.Exit(1).
Damien Neil3cf6e622018-09-11 13:53:14 -070045//
46// Passing a nil options is equivalent to passing a zero-valued one.
47func Run(opts *Options, f func(*Plugin) error) {
48 if err := run(opts, f); err != nil {
Damien Neil220c2022018-08-15 11:24:18 -070049 fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err)
50 os.Exit(1)
51 }
52}
53
Damien Neil3cf6e622018-09-11 13:53:14 -070054func run(opts *Options, f func(*Plugin) error) error {
Damien Neil220c2022018-08-15 11:24:18 -070055 in, err := ioutil.ReadAll(os.Stdin)
56 if err != nil {
57 return err
58 }
59 req := &pluginpb.CodeGeneratorRequest{}
60 if err := proto.Unmarshal(in, req); err != nil {
61 return err
62 }
Damien Neil3cf6e622018-09-11 13:53:14 -070063 gen, err := New(req, opts)
Damien Neil220c2022018-08-15 11:24:18 -070064 if err != nil {
65 return err
66 }
67 if err := f(gen); err != nil {
68 // Errors from the plugin function are reported by setting the
69 // error field in the CodeGeneratorResponse.
70 //
71 // In contrast, errors that indicate a problem in protoc
72 // itself (unparsable input, I/O errors, etc.) are reported
73 // to stderr.
74 gen.Error(err)
75 }
76 resp := gen.Response()
77 out, err := proto.Marshal(resp)
78 if err != nil {
79 return err
80 }
81 if _, err := os.Stdout.Write(out); err != nil {
82 return err
83 }
84 return nil
85}
86
87// A Plugin is a protoc plugin invocation.
88type Plugin struct {
89 // Request is the CodeGeneratorRequest provided by protoc.
90 Request *pluginpb.CodeGeneratorRequest
91
92 // Files is the set of files to generate and everything they import.
93 // Files appear in topological order, so each file appears before any
94 // file that imports it.
95 Files []*File
96 filesByName map[string]*File
97
Damien Neil658051b2018-09-10 12:26:21 -070098 fileReg *protoregistry.Files
99 messagesByName map[protoreflect.FullName]*Message
100 enumsByName map[protoreflect.FullName]*Enum
101 pathType pathType
102 genFiles []*GeneratedFile
103 err error
Damien Neil220c2022018-08-15 11:24:18 -0700104}
105
Damien Neil3cf6e622018-09-11 13:53:14 -0700106// Options are optional parameters to New.
107type Options struct {
108 // If ParamFunc is non-nil, it will be called with each unknown
109 // generator parameter.
110 //
111 // Plugins for protoc can accept parameters from the command line,
112 // passed in the --<lang>_out protoc, separated from the output
113 // directory with a colon; e.g.,
114 //
115 // --go_out=<param1>=<value1>,<param2>=<value2>:<output_directory>
116 //
117 // Parameters passed in this fashion as a comma-separated list of
118 // key=value pairs will be passed to the ParamFunc.
119 //
120 // The (flag.FlagSet).Set method matches this function signature,
121 // so parameters can be converted into flags as in the following:
122 //
123 // var flags flag.FlagSet
124 // value := flags.Bool("param", false, "")
125 // opts := &protogen.Options{
126 // ParamFunc: flags.Set,
127 // }
128 // protogen.Run(opts, func(p *protogen.Plugin) error {
129 // if *value { ... }
130 // })
131 ParamFunc func(name, value string) error
132}
133
Damien Neil220c2022018-08-15 11:24:18 -0700134// New returns a new Plugin.
Damien Neil3cf6e622018-09-11 13:53:14 -0700135//
136// Passing a nil Options is equivalent to passing a zero-valued one.
137func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
138 if opts == nil {
139 opts = &Options{}
140 }
Damien Neil220c2022018-08-15 11:24:18 -0700141 gen := &Plugin{
Damien Neil658051b2018-09-10 12:26:21 -0700142 Request: req,
143 filesByName: make(map[string]*File),
144 fileReg: protoregistry.NewFiles(),
145 messagesByName: make(map[protoreflect.FullName]*Message),
146 enumsByName: make(map[protoreflect.FullName]*Enum),
Damien Neil220c2022018-08-15 11:24:18 -0700147 }
148
Damien Neil082ce922018-09-06 10:23:53 -0700149 packageNames := make(map[string]GoPackageName) // filename -> package name
150 importPaths := make(map[string]GoImportPath) // filename -> import path
151 var packageImportPath GoImportPath
Damien Neil220c2022018-08-15 11:24:18 -0700152 for _, param := range strings.Split(req.GetParameter(), ",") {
153 var value string
154 if i := strings.Index(param, "="); i >= 0 {
155 value = param[i+1:]
156 param = param[0:i]
157 }
158 switch param {
159 case "":
160 // Ignore.
161 case "import_prefix":
162 // TODO
163 case "import_path":
Damien Neil082ce922018-09-06 10:23:53 -0700164 packageImportPath = GoImportPath(value)
Damien Neil220c2022018-08-15 11:24:18 -0700165 case "paths":
Damien Neil082ce922018-09-06 10:23:53 -0700166 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 }
Damien Neil220c2022018-08-15 11:24:18 -0700174 case "annotate_code":
175 // TODO
176 default:
Damien Neil3cf6e622018-09-11 13:53:14 -0700177 if param[0] == 'M' {
178 importPaths[param[1:]] = GoImportPath(value)
179 continue
Damien Neil220c2022018-08-15 11:24:18 -0700180 }
Damien Neil3cf6e622018-09-11 13:53:14 -0700181 if opts.ParamFunc != nil {
182 if err := opts.ParamFunc(param, value); err != nil {
183 return nil, err
184 }
185 }
Damien Neil082ce922018-09-06 10:23:53 -0700186 }
187 }
188
189 // Figure out the import path and package name for each file.
190 //
191 // The rules here are complicated and have grown organically over time.
192 // Interactions between different ways of specifying package information
193 // may be surprising.
194 //
195 // The recommended approach is to include a go_package option in every
196 // .proto source file specifying the full import path of the Go package
197 // associated with this file.
198 //
199 // option go_package = "github.com/golang/protobuf/ptypes/any";
200 //
201 // Build systems which want to exert full control over import paths may
202 // specify M<filename>=<import_path> flags.
203 //
204 // Other approaches are not recommend.
205 generatedFileNames := make(map[string]bool)
206 for _, name := range gen.Request.FileToGenerate {
207 generatedFileNames[name] = true
208 }
209 // We need to determine the import paths before the package names,
210 // because the Go package name for a file is sometimes derived from
211 // different file in the same package.
212 packageNameForImportPath := make(map[GoImportPath]GoPackageName)
213 for _, fdesc := range gen.Request.ProtoFile {
214 filename := fdesc.GetName()
215 packageName, importPath := goPackageOption(fdesc)
216 switch {
217 case importPaths[filename] != "":
218 // Command line: M=foo.proto=quux/bar
219 //
220 // Explicit mapping of source file to import path.
221 case generatedFileNames[filename] && packageImportPath != "":
222 // Command line: import_path=quux/bar
223 //
224 // The import_path flag sets the import path for every file that
225 // we generate code for.
226 importPaths[filename] = packageImportPath
227 case importPath != "":
228 // Source file: option go_package = "quux/bar";
229 //
230 // The go_package option sets the import path. Most users should use this.
231 importPaths[filename] = importPath
232 default:
233 // Source filename.
234 //
235 // Last resort when nothing else is available.
236 importPaths[filename] = GoImportPath(path.Dir(filename))
237 }
238 if packageName != "" {
239 packageNameForImportPath[importPaths[filename]] = packageName
240 }
241 }
242 for _, fdesc := range gen.Request.ProtoFile {
243 filename := fdesc.GetName()
244 packageName, _ := goPackageOption(fdesc)
245 defaultPackageName := packageNameForImportPath[importPaths[filename]]
246 switch {
247 case packageName != "":
248 // Source file: option go_package = "quux/bar";
249 packageNames[filename] = packageName
250 case defaultPackageName != "":
251 // A go_package option in another file in the same package.
252 //
253 // This is a poor choice in general, since every source file should
254 // contain a go_package option. Supported mainly for historical
255 // compatibility.
256 packageNames[filename] = defaultPackageName
257 case generatedFileNames[filename] && packageImportPath != "":
258 // Command line: import_path=quux/bar
259 packageNames[filename] = cleanPackageName(path.Base(string(packageImportPath)))
260 case fdesc.GetPackage() != "":
261 // Source file: package quux.bar;
262 packageNames[filename] = cleanPackageName(fdesc.GetPackage())
263 default:
264 // Source filename.
265 packageNames[filename] = cleanPackageName(baseName(filename))
266 }
267 }
268
269 // Consistency check: Every file with the same Go import path should have
270 // the same Go package name.
271 packageFiles := make(map[GoImportPath][]string)
272 for filename, importPath := range importPaths {
273 packageFiles[importPath] = append(packageFiles[importPath], filename)
274 }
275 for importPath, filenames := range packageFiles {
276 for i := 1; i < len(filenames); i++ {
277 if a, b := packageNames[filenames[0]], packageNames[filenames[i]]; a != b {
278 return nil, fmt.Errorf("Go package %v has inconsistent names %v (%v) and %v (%v)",
279 importPath, a, filenames[0], b, filenames[i])
280 }
Damien Neil220c2022018-08-15 11:24:18 -0700281 }
282 }
283
284 for _, fdesc := range gen.Request.ProtoFile {
Damien Neil082ce922018-09-06 10:23:53 -0700285 filename := fdesc.GetName()
286 if gen.filesByName[filename] != nil {
287 return nil, fmt.Errorf("duplicate file name: %q", filename)
288 }
289 f, err := newFile(gen, fdesc, packageNames[filename], importPaths[filename])
Damien Neilabc6fc12018-08-23 14:39:30 -0700290 if err != nil {
291 return nil, err
292 }
Damien Neil220c2022018-08-15 11:24:18 -0700293 gen.Files = append(gen.Files, f)
Damien Neil082ce922018-09-06 10:23:53 -0700294 gen.filesByName[filename] = f
Damien Neil220c2022018-08-15 11:24:18 -0700295 }
Damien Neil082ce922018-09-06 10:23:53 -0700296 for _, filename := range gen.Request.FileToGenerate {
297 f, ok := gen.FileByName(filename)
Damien Neil220c2022018-08-15 11:24:18 -0700298 if !ok {
Damien Neil082ce922018-09-06 10:23:53 -0700299 return nil, fmt.Errorf("no descriptor for generated file: %v", filename)
Damien Neil220c2022018-08-15 11:24:18 -0700300 }
301 f.Generate = true
302 }
303 return gen, nil
304}
305
306// Error records an error in code generation. The generator will report the
307// error back to protoc and will not produce output.
308func (gen *Plugin) Error(err error) {
309 if gen.err == nil {
310 gen.err = err
311 }
312}
313
314// Response returns the generator output.
315func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse {
316 resp := &pluginpb.CodeGeneratorResponse{}
317 if gen.err != nil {
318 resp.Error = proto.String(gen.err.Error())
319 return resp
320 }
321 for _, gf := range gen.genFiles {
Damien Neilc7d07d92018-08-22 13:46:02 -0700322 content, err := gf.Content()
323 if err != nil {
324 return &pluginpb.CodeGeneratorResponse{
325 Error: proto.String(err.Error()),
326 }
327 }
Damien Neil220c2022018-08-15 11:24:18 -0700328 resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
Damien Neild9016772018-08-23 14:39:30 -0700329 Name: proto.String(gf.filename),
Damien Neilc7d07d92018-08-22 13:46:02 -0700330 Content: proto.String(string(content)),
Damien Neil220c2022018-08-15 11:24:18 -0700331 })
332 }
333 return resp
334}
335
336// FileByName returns the file with the given name.
337func (gen *Plugin) FileByName(name string) (f *File, ok bool) {
338 f, ok = gen.filesByName[name]
339 return f, ok
340}
341
Damien Neilc7d07d92018-08-22 13:46:02 -0700342// A File describes a .proto source file.
Damien Neil220c2022018-08-15 11:24:18 -0700343type File struct {
Damien Neil7779e052018-09-07 14:14:06 -0700344 Desc protoreflect.FileDescriptor
345 Proto *descpb.FileDescriptorProto
Damien Neil220c2022018-08-15 11:24:18 -0700346
Damien Neil082ce922018-09-06 10:23:53 -0700347 GoPackageName GoPackageName // name of this file's Go package
348 GoImportPath GoImportPath // import path of this file's Go package
349 Messages []*Message // top-level message declarations
Damien Neil46abb572018-09-07 12:45:37 -0700350 Enums []*Enum // top-level enum declarations
Damien Neil993c04d2018-09-14 15:41:11 -0700351 Extensions []*Extension // top-level extension declarations
Damien Neil082ce922018-09-06 10:23:53 -0700352 Generate bool // true if we should generate code for this file
353
354 // GeneratedFilenamePrefix is used to construct filenames for generated
355 // files associated with this source file.
356 //
357 // For example, the source file "dir/foo.proto" might have a filename prefix
358 // of "dir/foo". Appending ".pb.go" produces an output file of "dir/foo.pb.go".
359 GeneratedFilenamePrefix string
Damien Neil220c2022018-08-15 11:24:18 -0700360}
361
Damien Neil082ce922018-09-06 10:23:53 -0700362func newFile(gen *Plugin, p *descpb.FileDescriptorProto, packageName GoPackageName, importPath GoImportPath) (*File, error) {
Damien Neilabc6fc12018-08-23 14:39:30 -0700363 desc, err := prototype.NewFileFromDescriptorProto(p, gen.fileReg)
364 if err != nil {
365 return nil, fmt.Errorf("invalid FileDescriptorProto %q: %v", p.GetName(), err)
366 }
367 if err := gen.fileReg.Register(desc); err != nil {
368 return nil, fmt.Errorf("cannot register descriptor %q: %v", p.GetName(), err)
369 }
Damien Neilc7d07d92018-08-22 13:46:02 -0700370 f := &File{
Damien Neil082ce922018-09-06 10:23:53 -0700371 Desc: desc,
Damien Neil7779e052018-09-07 14:14:06 -0700372 Proto: p,
Damien Neil082ce922018-09-06 10:23:53 -0700373 GoPackageName: packageName,
374 GoImportPath: importPath,
Damien Neil220c2022018-08-15 11:24:18 -0700375 }
Damien Neil082ce922018-09-06 10:23:53 -0700376
377 // Determine the prefix for generated Go files.
378 prefix := p.GetName()
379 if ext := path.Ext(prefix); ext == ".proto" || ext == ".protodevel" {
380 prefix = prefix[:len(prefix)-len(ext)]
381 }
382 if gen.pathType == pathTypeImport {
383 // If paths=import (the default) and the file contains a go_package option
384 // with a full import path, the output filename is derived from the Go import
385 // path.
386 //
387 // Pass the paths=source_relative flag to always derive the output filename
388 // from the input filename instead.
389 if _, importPath := goPackageOption(p); importPath != "" {
390 prefix = path.Join(string(importPath), path.Base(prefix))
391 }
392 }
393 f.GeneratedFilenamePrefix = prefix
394
Damien Neilabc6fc12018-08-23 14:39:30 -0700395 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700396 f.Messages = append(f.Messages, newMessage(gen, f, nil, mdescs.Get(i)))
Damien Neilc7d07d92018-08-22 13:46:02 -0700397 }
Damien Neil46abb572018-09-07 12:45:37 -0700398 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
399 f.Enums = append(f.Enums, newEnum(gen, f, nil, edescs.Get(i)))
400 }
Damien Neil993c04d2018-09-14 15:41:11 -0700401 for i, extdescs := 0, desc.Extensions(); i < extdescs.Len(); i++ {
402 f.Extensions = append(f.Extensions, newField(gen, f, nil, extdescs.Get(i)))
403 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700404 for _, message := range f.Messages {
Damien Neil993c04d2018-09-14 15:41:11 -0700405 if err := message.init(gen); err != nil {
406 return nil, err
407 }
408 }
409 for _, extension := range f.Extensions {
410 if err := extension.init(gen); err != nil {
411 return nil, err
412 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700413 }
Damien Neilabc6fc12018-08-23 14:39:30 -0700414 return f, nil
Damien Neilc7d07d92018-08-22 13:46:02 -0700415}
416
Damien Neil082ce922018-09-06 10:23:53 -0700417// goPackageOption interprets a file's go_package option.
418// If there is no go_package, it returns ("", "").
419// If there's a simple name, it returns (pkg, "").
420// If the option implies an import path, it returns (pkg, impPath).
421func goPackageOption(d *descpb.FileDescriptorProto) (pkg GoPackageName, impPath GoImportPath) {
422 opt := d.GetOptions().GetGoPackage()
423 if opt == "" {
424 return "", ""
425 }
426 // A semicolon-delimited suffix delimits the import path and package name.
427 if i := strings.Index(opt, ";"); i >= 0 {
428 return cleanPackageName(opt[i+1:]), GoImportPath(opt[:i])
429 }
430 // The presence of a slash implies there's an import path.
431 if i := strings.LastIndex(opt, "/"); i >= 0 {
432 return cleanPackageName(opt[i+1:]), GoImportPath(opt)
433 }
434 return cleanPackageName(opt), ""
435}
436
Damien Neilc7d07d92018-08-22 13:46:02 -0700437// A Message describes a message.
438type Message struct {
Damien Neilabc6fc12018-08-23 14:39:30 -0700439 Desc protoreflect.MessageDescriptor
Damien Neilc7d07d92018-08-22 13:46:02 -0700440
Damien Neil993c04d2018-09-14 15:41:11 -0700441 GoIdent GoIdent // name of the generated Go type
442 Fields []*Field // message field declarations
443 Oneofs []*Oneof // oneof declarations
444 Messages []*Message // nested message declarations
445 Enums []*Enum // nested enum declarations
446 Extensions []*Extension // nested extension declarations
447 Path []int32 // location path of this message
Damien Neilc7d07d92018-08-22 13:46:02 -0700448}
449
Damien Neil1fa78d82018-09-13 13:12:36 -0700450func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) *Message {
Damien Neilcab8dfe2018-09-06 14:51:28 -0700451 var path []int32
452 if parent != nil {
453 path = pathAppend(parent.Path, messageMessageField, int32(desc.Index()))
454 } else {
455 path = []int32{fileMessageField, int32(desc.Index())}
456 }
Damien Neil46abb572018-09-07 12:45:37 -0700457 message := &Message{
Damien Neilabc6fc12018-08-23 14:39:30 -0700458 Desc: desc,
459 GoIdent: newGoIdent(f, desc),
Damien Neilcab8dfe2018-09-06 14:51:28 -0700460 Path: path,
Damien Neilc7d07d92018-08-22 13:46:02 -0700461 }
Damien Neil658051b2018-09-10 12:26:21 -0700462 gen.messagesByName[desc.FullName()] = message
Damien Neilabc6fc12018-08-23 14:39:30 -0700463 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700464 message.Messages = append(message.Messages, newMessage(gen, f, message, mdescs.Get(i)))
Damien Neilc7d07d92018-08-22 13:46:02 -0700465 }
Damien Neil46abb572018-09-07 12:45:37 -0700466 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
467 message.Enums = append(message.Enums, newEnum(gen, f, message, edescs.Get(i)))
468 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700469 for i, odescs := 0, desc.Oneofs(); i < odescs.Len(); i++ {
470 message.Oneofs = append(message.Oneofs, newOneof(gen, f, message, odescs.Get(i)))
471 }
Damien Neil658051b2018-09-10 12:26:21 -0700472 for i, fdescs := 0, desc.Fields(); i < fdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700473 message.Fields = append(message.Fields, newField(gen, f, message, fdescs.Get(i)))
Damien Neil658051b2018-09-10 12:26:21 -0700474 }
Damien Neil993c04d2018-09-14 15:41:11 -0700475 for i, extdescs := 0, desc.Extensions(); i < extdescs.Len(); i++ {
476 message.Extensions = append(message.Extensions, newField(gen, f, message, extdescs.Get(i)))
477 }
Damien Neil658051b2018-09-10 12:26:21 -0700478
479 // Field name conflict resolution.
480 //
481 // We assume well-known method names that may be attached to a generated
482 // message type, as well as a 'Get*' method for each field. For each
483 // field in turn, we add _s to its name until there are no conflicts.
484 //
485 // Any change to the following set of method names is a potential
486 // incompatible API change because it may change generated field names.
487 //
488 // TODO: If we ever support a 'go_name' option to set the Go name of a
489 // field, we should consider dropping this entirely. The conflict
490 // resolution algorithm is subtle and surprising (changing the order
491 // in which fields appear in the .proto source file can change the
492 // names of fields in generated code), and does not adapt well to
493 // adding new per-field methods such as setters.
494 usedNames := map[string]bool{
495 "Reset": true,
496 "String": true,
497 "ProtoMessage": true,
498 "Marshal": true,
499 "Unmarshal": true,
500 "ExtensionRangeArray": true,
501 "ExtensionMap": true,
502 "Descriptor": true,
503 }
504 makeNameUnique := func(name string) string {
505 for usedNames[name] || usedNames["Get"+name] {
506 name += "_"
507 }
508 usedNames[name] = true
509 usedNames["Get"+name] = true
510 return name
511 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700512 seenOneofs := make(map[int]bool)
Damien Neil658051b2018-09-10 12:26:21 -0700513 for _, field := range message.Fields {
Damien Neil1fa78d82018-09-13 13:12:36 -0700514 field.GoName = makeNameUnique(field.GoName)
515 if field.OneofType != nil {
516 if !seenOneofs[field.OneofType.Desc.Index()] {
517 // If this is a field in a oneof that we haven't seen before,
518 // make the name for that oneof unique as well.
519 field.OneofType.GoName = makeNameUnique(field.OneofType.GoName)
520 seenOneofs[field.OneofType.Desc.Index()] = true
521 }
522 }
Damien Neil658051b2018-09-10 12:26:21 -0700523 }
524
Damien Neil1fa78d82018-09-13 13:12:36 -0700525 return message
Damien Neil658051b2018-09-10 12:26:21 -0700526}
527
Damien Neil0bd5a382018-09-13 15:07:10 -0700528func (message *Message) init(gen *Plugin) error {
529 for _, child := range message.Messages {
530 if err := child.init(gen); err != nil {
531 return err
532 }
533 }
534 for _, field := range message.Fields {
535 if err := field.init(gen); err != nil {
536 return err
537 }
538 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700539 for _, oneof := range message.Oneofs {
540 oneof.init(gen, message)
541 }
Damien Neil993c04d2018-09-14 15:41:11 -0700542 for _, extension := range message.Extensions {
543 if err := extension.init(gen); err != nil {
544 return err
545 }
546 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700547 return nil
548}
549
Damien Neil658051b2018-09-10 12:26:21 -0700550// A Field describes a message field.
551type Field struct {
552 Desc protoreflect.FieldDescriptor
553
Damien Neil1fa78d82018-09-13 13:12:36 -0700554 // GoName is the base name of this field's Go field and methods.
Damien Neil658051b2018-09-10 12:26:21 -0700555 // For code generated by protoc-gen-go, this means a field named
Damien Neil1fa78d82018-09-13 13:12:36 -0700556 // '{{GoName}}' and a getter method named 'Get{{GoName}}'.
557 GoName string
Damien Neil658051b2018-09-10 12:26:21 -0700558
Damien Neil993c04d2018-09-14 15:41:11 -0700559 ParentMessage *Message // message in which this field is defined; nil if top-level extension
560 ExtendedType *Message // extended message for extension fields; nil otherwise
561 MessageType *Message // type for message or group fields; nil otherwise
562 EnumType *Enum // type for enum fields; nil otherwise
563 OneofType *Oneof // containing oneof; nil if not part of a oneof
564 Path []int32 // location path of this field
Damien Neil658051b2018-09-10 12:26:21 -0700565}
566
Damien Neil1fa78d82018-09-13 13:12:36 -0700567func newField(gen *Plugin, f *File, message *Message, desc protoreflect.FieldDescriptor) *Field {
Damien Neil993c04d2018-09-14 15:41:11 -0700568 var path []int32
569 switch {
570 case desc.ExtendedType() != nil && message == nil:
571 path = []int32{fileExtensionField, int32(desc.Index())}
572 case desc.ExtendedType() != nil && message != nil:
573 path = pathAppend(message.Path, messageExtensionField, int32(desc.Index()))
574 default:
575 path = pathAppend(message.Path, messageFieldField, int32(desc.Index()))
576 }
Damien Neil658051b2018-09-10 12:26:21 -0700577 field := &Field{
Damien Neil993c04d2018-09-14 15:41:11 -0700578 Desc: desc,
579 GoName: camelCase(string(desc.Name())),
580 ParentMessage: message,
581 Path: path,
Damien Neil658051b2018-09-10 12:26:21 -0700582 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700583 if desc.OneofType() != nil {
584 field.OneofType = message.Oneofs[desc.OneofType().Index()]
585 }
586 return field
Damien Neil0bd5a382018-09-13 15:07:10 -0700587}
588
Damien Neil993c04d2018-09-14 15:41:11 -0700589// Extension is an alias of Field for documentation.
590type Extension = Field
591
Damien Neil0bd5a382018-09-13 15:07:10 -0700592func (field *Field) init(gen *Plugin) error {
593 desc := field.Desc
Damien Neil658051b2018-09-10 12:26:21 -0700594 switch desc.Kind() {
595 case protoreflect.MessageKind, protoreflect.GroupKind:
596 mname := desc.MessageType().FullName()
597 message, ok := gen.messagesByName[mname]
598 if !ok {
Damien Neil0bd5a382018-09-13 15:07:10 -0700599 return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), mname)
Damien Neil658051b2018-09-10 12:26:21 -0700600 }
601 field.MessageType = message
602 case protoreflect.EnumKind:
Damien Neil0bd5a382018-09-13 15:07:10 -0700603 ename := field.Desc.EnumType().FullName()
Damien Neil658051b2018-09-10 12:26:21 -0700604 enum, ok := gen.enumsByName[ename]
605 if !ok {
Damien Neil0bd5a382018-09-13 15:07:10 -0700606 return fmt.Errorf("field %v: no descriptor for enum %v", desc.FullName(), ename)
Damien Neil658051b2018-09-10 12:26:21 -0700607 }
608 field.EnumType = enum
609 }
Damien Neil993c04d2018-09-14 15:41:11 -0700610 if desc.ExtendedType() != nil {
611 mname := desc.ExtendedType().FullName()
612 message, ok := gen.messagesByName[mname]
613 if !ok {
614 return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), mname)
615 }
616 field.ExtendedType = message
617 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700618 return nil
Damien Neil46abb572018-09-07 12:45:37 -0700619}
620
Damien Neil1fa78d82018-09-13 13:12:36 -0700621// A Oneof describes a oneof field.
622type Oneof struct {
623 Desc protoreflect.OneofDescriptor
624
Damien Neil993c04d2018-09-14 15:41:11 -0700625 GoName string // Go field name of this oneof
626 ParentMessage *Message // message in which this oneof occurs
627 Fields []*Field // fields that are part of this oneof
628 Path []int32 // location path of this oneof
Damien Neil1fa78d82018-09-13 13:12:36 -0700629}
630
631func newOneof(gen *Plugin, f *File, message *Message, desc protoreflect.OneofDescriptor) *Oneof {
632 return &Oneof{
Damien Neil993c04d2018-09-14 15:41:11 -0700633 Desc: desc,
634 ParentMessage: message,
635 GoName: camelCase(string(desc.Name())),
636 Path: pathAppend(message.Path, messageOneofField, int32(desc.Index())),
Damien Neil1fa78d82018-09-13 13:12:36 -0700637 }
638}
639
640func (oneof *Oneof) init(gen *Plugin, parent *Message) {
641 for i, fdescs := 0, oneof.Desc.Fields(); i < fdescs.Len(); i++ {
642 oneof.Fields = append(oneof.Fields, parent.Fields[fdescs.Get(i).Index()])
643 }
644}
645
Damien Neil46abb572018-09-07 12:45:37 -0700646// An Enum describes an enum.
647type Enum struct {
648 Desc protoreflect.EnumDescriptor
649
650 GoIdent GoIdent // name of the generated Go type
651 Values []*EnumValue // enum values
652 Path []int32 // location path of this enum
653}
654
655func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum {
656 var path []int32
657 if parent != nil {
658 path = pathAppend(parent.Path, messageEnumField, int32(desc.Index()))
659 } else {
660 path = []int32{fileEnumField, int32(desc.Index())}
661 }
662 enum := &Enum{
663 Desc: desc,
664 GoIdent: newGoIdent(f, desc),
665 Path: path,
666 }
Damien Neil658051b2018-09-10 12:26:21 -0700667 gen.enumsByName[desc.FullName()] = enum
Damien Neil46abb572018-09-07 12:45:37 -0700668 for i, evdescs := 0, enum.Desc.Values(); i < evdescs.Len(); i++ {
669 enum.Values = append(enum.Values, newEnumValue(gen, f, parent, enum, evdescs.Get(i)))
670 }
671 return enum
672}
673
674// An EnumValue describes an enum value.
675type EnumValue struct {
676 Desc protoreflect.EnumValueDescriptor
677
678 GoIdent GoIdent // name of the generated Go type
679 Path []int32 // location path of this enum value
680}
681
682func newEnumValue(gen *Plugin, f *File, message *Message, enum *Enum, desc protoreflect.EnumValueDescriptor) *EnumValue {
683 // A top-level enum value's name is: EnumName_ValueName
684 // An enum value contained in a message is: MessageName_ValueName
685 //
686 // Enum value names are not camelcased.
687 parentIdent := enum.GoIdent
688 if message != nil {
689 parentIdent = message.GoIdent
690 }
691 name := parentIdent.GoName + "_" + string(desc.Name())
692 return &EnumValue{
693 Desc: desc,
694 GoIdent: GoIdent{
695 GoName: name,
696 GoImportPath: f.GoImportPath,
697 },
698 Path: pathAppend(enum.Path, enumValueField, int32(desc.Index())),
699 }
Damien Neil220c2022018-08-15 11:24:18 -0700700}
701
702// A GeneratedFile is a generated file.
703type GeneratedFile struct {
Damien Neild9016772018-08-23 14:39:30 -0700704 filename string
705 goImportPath GoImportPath
706 buf bytes.Buffer
707 packageNames map[GoImportPath]GoPackageName
708 usedPackageNames map[GoPackageName]bool
Damien Neil220c2022018-08-15 11:24:18 -0700709}
710
Damien Neild9016772018-08-23 14:39:30 -0700711// NewGeneratedFile creates a new generated file with the given filename
712// and import path.
713func (gen *Plugin) NewGeneratedFile(filename string, goImportPath GoImportPath) *GeneratedFile {
Damien Neil220c2022018-08-15 11:24:18 -0700714 g := &GeneratedFile{
Damien Neild9016772018-08-23 14:39:30 -0700715 filename: filename,
716 goImportPath: goImportPath,
717 packageNames: make(map[GoImportPath]GoPackageName),
718 usedPackageNames: make(map[GoPackageName]bool),
Damien Neil220c2022018-08-15 11:24:18 -0700719 }
720 gen.genFiles = append(gen.genFiles, g)
721 return g
722}
723
724// P prints a line to the generated output. It converts each parameter to a
725// string following the same rules as fmt.Print. It never inserts spaces
726// between parameters.
727//
728// TODO: .meta file annotations.
729func (g *GeneratedFile) P(v ...interface{}) {
730 for _, x := range v {
Damien Neild9016772018-08-23 14:39:30 -0700731 switch x := x.(type) {
732 case GoIdent:
Damien Neil46abb572018-09-07 12:45:37 -0700733 fmt.Fprint(&g.buf, g.QualifiedGoIdent(x))
Damien Neild9016772018-08-23 14:39:30 -0700734 default:
735 fmt.Fprint(&g.buf, x)
736 }
Damien Neil220c2022018-08-15 11:24:18 -0700737 }
738 fmt.Fprintln(&g.buf)
739}
740
Damien Neil46abb572018-09-07 12:45:37 -0700741// QualifiedGoIdent returns the string to use for a Go identifier.
742//
743// If the identifier is from a different Go package than the generated file,
744// the returned name will be qualified (package.name) and an import statement
745// for the identifier's package will be included in the file.
746func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string {
747 if ident.GoImportPath == g.goImportPath {
748 return ident.GoName
749 }
750 if packageName, ok := g.packageNames[ident.GoImportPath]; ok {
751 return string(packageName) + "." + ident.GoName
752 }
753 packageName := cleanPackageName(baseName(string(ident.GoImportPath)))
754 for i, orig := 1, packageName; g.usedPackageNames[packageName]; i++ {
755 packageName = orig + GoPackageName(strconv.Itoa(i))
756 }
757 g.packageNames[ident.GoImportPath] = packageName
758 g.usedPackageNames[packageName] = true
759 return string(packageName) + "." + ident.GoName
760}
761
Damien Neil220c2022018-08-15 11:24:18 -0700762// Write implements io.Writer.
763func (g *GeneratedFile) Write(p []byte) (n int, err error) {
764 return g.buf.Write(p)
765}
766
767// Content returns the contents of the generated file.
Damien Neilc7d07d92018-08-22 13:46:02 -0700768func (g *GeneratedFile) Content() ([]byte, error) {
Damien Neild9016772018-08-23 14:39:30 -0700769 if !strings.HasSuffix(g.filename, ".go") {
Damien Neilc7d07d92018-08-22 13:46:02 -0700770 return g.buf.Bytes(), nil
771 }
772
773 // Reformat generated code.
774 original := g.buf.Bytes()
775 fset := token.NewFileSet()
Damien Neil1ec33152018-09-13 13:12:36 -0700776 file, err := parser.ParseFile(fset, "", original, parser.ParseComments)
Damien Neilc7d07d92018-08-22 13:46:02 -0700777 if err != nil {
778 // Print out the bad code with line numbers.
779 // This should never happen in practice, but it can while changing generated code
780 // so consider this a debugging aid.
781 var src bytes.Buffer
782 s := bufio.NewScanner(bytes.NewReader(original))
783 for line := 1; s.Scan(); line++ {
784 fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
785 }
Damien Neild9016772018-08-23 14:39:30 -0700786 return nil, fmt.Errorf("%v: unparsable Go source: %v\n%v", g.filename, err, src.String())
Damien Neilc7d07d92018-08-22 13:46:02 -0700787 }
Damien Neild9016772018-08-23 14:39:30 -0700788
789 // Add imports.
790 var importPaths []string
791 for importPath := range g.packageNames {
792 importPaths = append(importPaths, string(importPath))
793 }
794 sort.Strings(importPaths)
795 for _, importPath := range importPaths {
Damien Neil1ec33152018-09-13 13:12:36 -0700796 astutil.AddNamedImport(fset, file, string(g.packageNames[GoImportPath(importPath)]), importPath)
Damien Neild9016772018-08-23 14:39:30 -0700797 }
Damien Neil1ec33152018-09-13 13:12:36 -0700798 ast.SortImports(fset, file)
Damien Neild9016772018-08-23 14:39:30 -0700799
Damien Neilc7d07d92018-08-22 13:46:02 -0700800 var out bytes.Buffer
Damien Neil1ec33152018-09-13 13:12:36 -0700801 if err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(&out, fset, file); err != nil {
Damien Neild9016772018-08-23 14:39:30 -0700802 return nil, fmt.Errorf("%v: can not reformat Go source: %v", g.filename, err)
Damien Neilc7d07d92018-08-22 13:46:02 -0700803 }
Damien Neild9016772018-08-23 14:39:30 -0700804 // TODO: Annotations.
Damien Neilc7d07d92018-08-22 13:46:02 -0700805 return out.Bytes(), nil
806
Damien Neil220c2022018-08-15 11:24:18 -0700807}
Damien Neil082ce922018-09-06 10:23:53 -0700808
809type pathType int
810
811const (
812 pathTypeImport pathType = iota
813 pathTypeSourceRelative
814)
Damien Neilcab8dfe2018-09-06 14:51:28 -0700815
816// The SourceCodeInfo message describes the location of elements of a parsed
817// .proto file by way of a "path", which is a sequence of integers that
818// describe the route from a FileDescriptorProto to the relevant submessage.
819// The path alternates between a field number of a repeated field, and an index
820// into that repeated field. The constants below define the field numbers that
821// are used.
822//
823// See descriptor.proto for more information about this.
824const (
825 // field numbers in FileDescriptorProto
Damien Neil993c04d2018-09-14 15:41:11 -0700826 filePackageField = 2 // package
827 fileMessageField = 4 // message_type
828 fileEnumField = 5 // enum_type
829 fileExtensionField = 7 // extension
Damien Neilcab8dfe2018-09-06 14:51:28 -0700830 // field numbers in DescriptorProto
Damien Neil993c04d2018-09-14 15:41:11 -0700831 messageFieldField = 2 // field
832 messageMessageField = 3 // nested_type
833 messageEnumField = 4 // enum_type
834 messageExtensionField = 6 // extension
835 messageOneofField = 8 // oneof_decl
Damien Neilcab8dfe2018-09-06 14:51:28 -0700836 // field numbers in EnumDescriptorProto
837 enumValueField = 2 // value
838)
839
840// pathAppend appends elements to a location path.
841// It does not alias the original path.
842func pathAppend(path []int32, a ...int32) []int32 {
843 var n []int32
844 n = append(n, path...)
845 n = append(n, a...)
846 return n
847}