blob: 4e76c3073bdef8b0f7fee2eab2695e25090ab13f [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"
Joe Tsai01ab2962018-09-21 17:44:00 -070032 "github.com/golang/protobuf/v2/reflect/protoreflect"
33 "github.com/golang/protobuf/v2/reflect/protoregistry"
34 "github.com/golang/protobuf/v2/reflect/prototype"
Damien Neild9016772018-08-23 14:39:30 -070035 "golang.org/x/tools/go/ast/astutil"
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
Damien Neil1fa8ab02018-09-27 15:51:05 -0700103 opts *Options
Damien Neil658051b2018-09-10 12:26:21 -0700104 err error
Damien Neil220c2022018-08-15 11:24:18 -0700105}
106
Damien Neil3cf6e622018-09-11 13:53:14 -0700107// Options are optional parameters to New.
108type Options struct {
109 // If ParamFunc is non-nil, it will be called with each unknown
110 // generator parameter.
111 //
112 // Plugins for protoc can accept parameters from the command line,
113 // passed in the --<lang>_out protoc, separated from the output
114 // directory with a colon; e.g.,
115 //
116 // --go_out=<param1>=<value1>,<param2>=<value2>:<output_directory>
117 //
118 // Parameters passed in this fashion as a comma-separated list of
119 // key=value pairs will be passed to the ParamFunc.
120 //
121 // The (flag.FlagSet).Set method matches this function signature,
122 // so parameters can be converted into flags as in the following:
123 //
124 // var flags flag.FlagSet
125 // value := flags.Bool("param", false, "")
126 // opts := &protogen.Options{
127 // ParamFunc: flags.Set,
128 // }
129 // protogen.Run(opts, func(p *protogen.Plugin) error {
130 // if *value { ... }
131 // })
132 ParamFunc func(name, value string) error
Damien Neil1fa8ab02018-09-27 15:51:05 -0700133
134 // ImportRewriteFunc is called with the import path of each package
135 // imported by a generated file. It returns the import path to use
136 // for this package.
137 ImportRewriteFunc func(GoImportPath) GoImportPath
Damien Neil3cf6e622018-09-11 13:53:14 -0700138}
139
Damien Neil220c2022018-08-15 11:24:18 -0700140// New returns a new Plugin.
Damien Neil3cf6e622018-09-11 13:53:14 -0700141//
142// Passing a nil Options is equivalent to passing a zero-valued one.
143func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
144 if opts == nil {
145 opts = &Options{}
146 }
Damien Neil220c2022018-08-15 11:24:18 -0700147 gen := &Plugin{
Damien Neil658051b2018-09-10 12:26:21 -0700148 Request: req,
149 filesByName: make(map[string]*File),
150 fileReg: protoregistry.NewFiles(),
151 messagesByName: make(map[protoreflect.FullName]*Message),
152 enumsByName: make(map[protoreflect.FullName]*Enum),
Damien Neil1fa8ab02018-09-27 15:51:05 -0700153 opts: opts,
Damien Neil220c2022018-08-15 11:24:18 -0700154 }
155
Damien Neil082ce922018-09-06 10:23:53 -0700156 packageNames := make(map[string]GoPackageName) // filename -> package name
157 importPaths := make(map[string]GoImportPath) // filename -> import path
158 var packageImportPath GoImportPath
Damien Neil220c2022018-08-15 11:24:18 -0700159 for _, param := range strings.Split(req.GetParameter(), ",") {
160 var value string
161 if i := strings.Index(param, "="); i >= 0 {
162 value = param[i+1:]
163 param = param[0:i]
164 }
165 switch param {
166 case "":
167 // Ignore.
Damien Neil220c2022018-08-15 11:24:18 -0700168 case "import_path":
Damien Neil082ce922018-09-06 10:23:53 -0700169 packageImportPath = GoImportPath(value)
Damien Neil220c2022018-08-15 11:24:18 -0700170 case "paths":
Damien Neil082ce922018-09-06 10:23:53 -0700171 switch value {
172 case "import":
173 gen.pathType = pathTypeImport
174 case "source_relative":
175 gen.pathType = pathTypeSourceRelative
176 default:
177 return nil, fmt.Errorf(`unknown path type %q: want "import" or "source_relative"`, value)
178 }
Damien Neil220c2022018-08-15 11:24:18 -0700179 case "annotate_code":
180 // TODO
181 default:
Damien Neil3cf6e622018-09-11 13:53:14 -0700182 if param[0] == 'M' {
183 importPaths[param[1:]] = GoImportPath(value)
184 continue
Damien Neil220c2022018-08-15 11:24:18 -0700185 }
Damien Neil3cf6e622018-09-11 13:53:14 -0700186 if opts.ParamFunc != nil {
187 if err := opts.ParamFunc(param, value); err != nil {
188 return nil, err
189 }
190 }
Damien Neil082ce922018-09-06 10:23:53 -0700191 }
192 }
193
194 // Figure out the import path and package name for each file.
195 //
196 // The rules here are complicated and have grown organically over time.
197 // Interactions between different ways of specifying package information
198 // may be surprising.
199 //
200 // The recommended approach is to include a go_package option in every
201 // .proto source file specifying the full import path of the Go package
202 // associated with this file.
203 //
204 // option go_package = "github.com/golang/protobuf/ptypes/any";
205 //
206 // Build systems which want to exert full control over import paths may
207 // specify M<filename>=<import_path> flags.
208 //
209 // Other approaches are not recommend.
210 generatedFileNames := make(map[string]bool)
211 for _, name := range gen.Request.FileToGenerate {
212 generatedFileNames[name] = true
213 }
214 // We need to determine the import paths before the package names,
215 // because the Go package name for a file is sometimes derived from
216 // different file in the same package.
217 packageNameForImportPath := make(map[GoImportPath]GoPackageName)
218 for _, fdesc := range gen.Request.ProtoFile {
219 filename := fdesc.GetName()
220 packageName, importPath := goPackageOption(fdesc)
221 switch {
222 case importPaths[filename] != "":
223 // Command line: M=foo.proto=quux/bar
224 //
225 // Explicit mapping of source file to import path.
226 case generatedFileNames[filename] && packageImportPath != "":
227 // Command line: import_path=quux/bar
228 //
229 // The import_path flag sets the import path for every file that
230 // we generate code for.
231 importPaths[filename] = packageImportPath
232 case importPath != "":
233 // Source file: option go_package = "quux/bar";
234 //
235 // The go_package option sets the import path. Most users should use this.
236 importPaths[filename] = importPath
237 default:
238 // Source filename.
239 //
240 // Last resort when nothing else is available.
241 importPaths[filename] = GoImportPath(path.Dir(filename))
242 }
243 if packageName != "" {
244 packageNameForImportPath[importPaths[filename]] = packageName
245 }
246 }
247 for _, fdesc := range gen.Request.ProtoFile {
248 filename := fdesc.GetName()
249 packageName, _ := goPackageOption(fdesc)
250 defaultPackageName := packageNameForImportPath[importPaths[filename]]
251 switch {
252 case packageName != "":
253 // Source file: option go_package = "quux/bar";
254 packageNames[filename] = packageName
255 case defaultPackageName != "":
256 // A go_package option in another file in the same package.
257 //
258 // This is a poor choice in general, since every source file should
259 // contain a go_package option. Supported mainly for historical
260 // compatibility.
261 packageNames[filename] = defaultPackageName
262 case generatedFileNames[filename] && packageImportPath != "":
263 // Command line: import_path=quux/bar
264 packageNames[filename] = cleanPackageName(path.Base(string(packageImportPath)))
265 case fdesc.GetPackage() != "":
266 // Source file: package quux.bar;
267 packageNames[filename] = cleanPackageName(fdesc.GetPackage())
268 default:
269 // Source filename.
270 packageNames[filename] = cleanPackageName(baseName(filename))
271 }
272 }
273
274 // Consistency check: Every file with the same Go import path should have
275 // the same Go package name.
276 packageFiles := make(map[GoImportPath][]string)
277 for filename, importPath := range importPaths {
278 packageFiles[importPath] = append(packageFiles[importPath], filename)
279 }
280 for importPath, filenames := range packageFiles {
281 for i := 1; i < len(filenames); i++ {
282 if a, b := packageNames[filenames[0]], packageNames[filenames[i]]; a != b {
283 return nil, fmt.Errorf("Go package %v has inconsistent names %v (%v) and %v (%v)",
284 importPath, a, filenames[0], b, filenames[i])
285 }
Damien Neil220c2022018-08-15 11:24:18 -0700286 }
287 }
288
289 for _, fdesc := range gen.Request.ProtoFile {
Damien Neil082ce922018-09-06 10:23:53 -0700290 filename := fdesc.GetName()
291 if gen.filesByName[filename] != nil {
292 return nil, fmt.Errorf("duplicate file name: %q", filename)
293 }
294 f, err := newFile(gen, fdesc, packageNames[filename], importPaths[filename])
Damien Neilabc6fc12018-08-23 14:39:30 -0700295 if err != nil {
296 return nil, err
297 }
Damien Neil220c2022018-08-15 11:24:18 -0700298 gen.Files = append(gen.Files, f)
Damien Neil082ce922018-09-06 10:23:53 -0700299 gen.filesByName[filename] = f
Damien Neil220c2022018-08-15 11:24:18 -0700300 }
Damien Neil082ce922018-09-06 10:23:53 -0700301 for _, filename := range gen.Request.FileToGenerate {
302 f, ok := gen.FileByName(filename)
Damien Neil220c2022018-08-15 11:24:18 -0700303 if !ok {
Damien Neil082ce922018-09-06 10:23:53 -0700304 return nil, fmt.Errorf("no descriptor for generated file: %v", filename)
Damien Neil220c2022018-08-15 11:24:18 -0700305 }
306 f.Generate = true
307 }
308 return gen, nil
309}
310
311// Error records an error in code generation. The generator will report the
312// error back to protoc and will not produce output.
313func (gen *Plugin) Error(err error) {
314 if gen.err == nil {
315 gen.err = err
316 }
317}
318
319// Response returns the generator output.
320func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse {
321 resp := &pluginpb.CodeGeneratorResponse{}
322 if gen.err != nil {
323 resp.Error = proto.String(gen.err.Error())
324 return resp
325 }
326 for _, gf := range gen.genFiles {
Damien Neilc7d07d92018-08-22 13:46:02 -0700327 content, err := gf.Content()
328 if err != nil {
329 return &pluginpb.CodeGeneratorResponse{
330 Error: proto.String(err.Error()),
331 }
332 }
Damien Neil220c2022018-08-15 11:24:18 -0700333 resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
Damien Neild9016772018-08-23 14:39:30 -0700334 Name: proto.String(gf.filename),
Damien Neilc7d07d92018-08-22 13:46:02 -0700335 Content: proto.String(string(content)),
Damien Neil220c2022018-08-15 11:24:18 -0700336 })
337 }
338 return resp
339}
340
341// FileByName returns the file with the given name.
342func (gen *Plugin) FileByName(name string) (f *File, ok bool) {
343 f, ok = gen.filesByName[name]
344 return f, ok
345}
346
Damien Neilc7d07d92018-08-22 13:46:02 -0700347// A File describes a .proto source file.
Damien Neil220c2022018-08-15 11:24:18 -0700348type File struct {
Damien Neil7779e052018-09-07 14:14:06 -0700349 Desc protoreflect.FileDescriptor
350 Proto *descpb.FileDescriptorProto
Damien Neil220c2022018-08-15 11:24:18 -0700351
Damien Neil082ce922018-09-06 10:23:53 -0700352 GoPackageName GoPackageName // name of this file's Go package
353 GoImportPath GoImportPath // import path of this file's Go package
354 Messages []*Message // top-level message declarations
Damien Neil46abb572018-09-07 12:45:37 -0700355 Enums []*Enum // top-level enum declarations
Damien Neil993c04d2018-09-14 15:41:11 -0700356 Extensions []*Extension // top-level extension declarations
Damien Neil2dc67182018-09-21 15:03:34 -0700357 Services []*Service // top-level service declarations
Damien Neil082ce922018-09-06 10:23:53 -0700358 Generate bool // true if we should generate code for this file
359
360 // GeneratedFilenamePrefix is used to construct filenames for generated
361 // files associated with this source file.
362 //
363 // For example, the source file "dir/foo.proto" might have a filename prefix
364 // of "dir/foo". Appending ".pb.go" produces an output file of "dir/foo.pb.go".
365 GeneratedFilenamePrefix string
Damien Neil220c2022018-08-15 11:24:18 -0700366}
367
Damien Neil082ce922018-09-06 10:23:53 -0700368func newFile(gen *Plugin, p *descpb.FileDescriptorProto, packageName GoPackageName, importPath GoImportPath) (*File, error) {
Damien Neilabc6fc12018-08-23 14:39:30 -0700369 desc, err := prototype.NewFileFromDescriptorProto(p, gen.fileReg)
370 if err != nil {
371 return nil, fmt.Errorf("invalid FileDescriptorProto %q: %v", p.GetName(), err)
372 }
373 if err := gen.fileReg.Register(desc); err != nil {
374 return nil, fmt.Errorf("cannot register descriptor %q: %v", p.GetName(), err)
375 }
Damien Neilc7d07d92018-08-22 13:46:02 -0700376 f := &File{
Damien Neil082ce922018-09-06 10:23:53 -0700377 Desc: desc,
Damien Neil7779e052018-09-07 14:14:06 -0700378 Proto: p,
Damien Neil082ce922018-09-06 10:23:53 -0700379 GoPackageName: packageName,
380 GoImportPath: importPath,
Damien Neil220c2022018-08-15 11:24:18 -0700381 }
Damien Neil082ce922018-09-06 10:23:53 -0700382
383 // Determine the prefix for generated Go files.
384 prefix := p.GetName()
385 if ext := path.Ext(prefix); ext == ".proto" || ext == ".protodevel" {
386 prefix = prefix[:len(prefix)-len(ext)]
387 }
388 if gen.pathType == pathTypeImport {
389 // If paths=import (the default) and the file contains a go_package option
390 // with a full import path, the output filename is derived from the Go import
391 // path.
392 //
393 // Pass the paths=source_relative flag to always derive the output filename
394 // from the input filename instead.
395 if _, importPath := goPackageOption(p); importPath != "" {
396 prefix = path.Join(string(importPath), path.Base(prefix))
397 }
398 }
399 f.GeneratedFilenamePrefix = prefix
400
Damien Neilabc6fc12018-08-23 14:39:30 -0700401 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700402 f.Messages = append(f.Messages, newMessage(gen, f, nil, mdescs.Get(i)))
Damien Neilc7d07d92018-08-22 13:46:02 -0700403 }
Damien Neil46abb572018-09-07 12:45:37 -0700404 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
405 f.Enums = append(f.Enums, newEnum(gen, f, nil, edescs.Get(i)))
406 }
Damien Neil993c04d2018-09-14 15:41:11 -0700407 for i, extdescs := 0, desc.Extensions(); i < extdescs.Len(); i++ {
408 f.Extensions = append(f.Extensions, newField(gen, f, nil, extdescs.Get(i)))
409 }
Damien Neil2dc67182018-09-21 15:03:34 -0700410 for i, sdescs := 0, desc.Services(); i < sdescs.Len(); i++ {
411 f.Services = append(f.Services, newService(gen, f, sdescs.Get(i)))
412 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700413 for _, message := range f.Messages {
Damien Neil993c04d2018-09-14 15:41:11 -0700414 if err := message.init(gen); err != nil {
415 return nil, err
416 }
417 }
418 for _, extension := range f.Extensions {
419 if err := extension.init(gen); err != nil {
420 return nil, err
421 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700422 }
Damien Neil2dc67182018-09-21 15:03:34 -0700423 for _, service := range f.Services {
424 for _, method := range service.Methods {
425 if err := method.init(gen); err != nil {
426 return nil, err
427 }
428 }
429 }
Damien Neilabc6fc12018-08-23 14:39:30 -0700430 return f, nil
Damien Neilc7d07d92018-08-22 13:46:02 -0700431}
432
Damien Neil082ce922018-09-06 10:23:53 -0700433// goPackageOption interprets a file's go_package option.
434// If there is no go_package, it returns ("", "").
435// If there's a simple name, it returns (pkg, "").
436// If the option implies an import path, it returns (pkg, impPath).
437func goPackageOption(d *descpb.FileDescriptorProto) (pkg GoPackageName, impPath GoImportPath) {
438 opt := d.GetOptions().GetGoPackage()
439 if opt == "" {
440 return "", ""
441 }
442 // A semicolon-delimited suffix delimits the import path and package name.
443 if i := strings.Index(opt, ";"); i >= 0 {
444 return cleanPackageName(opt[i+1:]), GoImportPath(opt[:i])
445 }
446 // The presence of a slash implies there's an import path.
447 if i := strings.LastIndex(opt, "/"); i >= 0 {
448 return cleanPackageName(opt[i+1:]), GoImportPath(opt)
449 }
450 return cleanPackageName(opt), ""
451}
452
Damien Neilc7d07d92018-08-22 13:46:02 -0700453// A Message describes a message.
454type Message struct {
Damien Neilabc6fc12018-08-23 14:39:30 -0700455 Desc protoreflect.MessageDescriptor
Damien Neilc7d07d92018-08-22 13:46:02 -0700456
Damien Neil993c04d2018-09-14 15:41:11 -0700457 GoIdent GoIdent // name of the generated Go type
458 Fields []*Field // message field declarations
459 Oneofs []*Oneof // oneof declarations
460 Messages []*Message // nested message declarations
461 Enums []*Enum // nested enum declarations
462 Extensions []*Extension // nested extension declarations
463 Path []int32 // location path of this message
Damien Neilc7d07d92018-08-22 13:46:02 -0700464}
465
Damien Neil1fa78d82018-09-13 13:12:36 -0700466func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) *Message {
Damien Neilcab8dfe2018-09-06 14:51:28 -0700467 var path []int32
468 if parent != nil {
469 path = pathAppend(parent.Path, messageMessageField, int32(desc.Index()))
470 } else {
471 path = []int32{fileMessageField, int32(desc.Index())}
472 }
Damien Neil46abb572018-09-07 12:45:37 -0700473 message := &Message{
Damien Neilabc6fc12018-08-23 14:39:30 -0700474 Desc: desc,
475 GoIdent: newGoIdent(f, desc),
Damien Neilcab8dfe2018-09-06 14:51:28 -0700476 Path: path,
Damien Neilc7d07d92018-08-22 13:46:02 -0700477 }
Damien Neil658051b2018-09-10 12:26:21 -0700478 gen.messagesByName[desc.FullName()] = message
Damien Neilabc6fc12018-08-23 14:39:30 -0700479 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700480 message.Messages = append(message.Messages, newMessage(gen, f, message, mdescs.Get(i)))
Damien Neilc7d07d92018-08-22 13:46:02 -0700481 }
Damien Neil46abb572018-09-07 12:45:37 -0700482 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
483 message.Enums = append(message.Enums, newEnum(gen, f, message, edescs.Get(i)))
484 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700485 for i, odescs := 0, desc.Oneofs(); i < odescs.Len(); i++ {
486 message.Oneofs = append(message.Oneofs, newOneof(gen, f, message, odescs.Get(i)))
487 }
Damien Neil658051b2018-09-10 12:26:21 -0700488 for i, fdescs := 0, desc.Fields(); i < fdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700489 message.Fields = append(message.Fields, newField(gen, f, message, fdescs.Get(i)))
Damien Neil658051b2018-09-10 12:26:21 -0700490 }
Damien Neil993c04d2018-09-14 15:41:11 -0700491 for i, extdescs := 0, desc.Extensions(); i < extdescs.Len(); i++ {
492 message.Extensions = append(message.Extensions, newField(gen, f, message, extdescs.Get(i)))
493 }
Damien Neil658051b2018-09-10 12:26:21 -0700494
495 // Field name conflict resolution.
496 //
497 // We assume well-known method names that may be attached to a generated
498 // message type, as well as a 'Get*' method for each field. For each
499 // field in turn, we add _s to its name until there are no conflicts.
500 //
501 // Any change to the following set of method names is a potential
502 // incompatible API change because it may change generated field names.
503 //
504 // TODO: If we ever support a 'go_name' option to set the Go name of a
505 // field, we should consider dropping this entirely. The conflict
506 // resolution algorithm is subtle and surprising (changing the order
507 // in which fields appear in the .proto source file can change the
508 // names of fields in generated code), and does not adapt well to
509 // adding new per-field methods such as setters.
510 usedNames := map[string]bool{
511 "Reset": true,
512 "String": true,
513 "ProtoMessage": true,
514 "Marshal": true,
515 "Unmarshal": true,
516 "ExtensionRangeArray": true,
517 "ExtensionMap": true,
518 "Descriptor": true,
519 }
520 makeNameUnique := func(name string) string {
521 for usedNames[name] || usedNames["Get"+name] {
522 name += "_"
523 }
524 usedNames[name] = true
525 usedNames["Get"+name] = true
526 return name
527 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700528 seenOneofs := make(map[int]bool)
Damien Neil658051b2018-09-10 12:26:21 -0700529 for _, field := range message.Fields {
Damien Neil1fa78d82018-09-13 13:12:36 -0700530 field.GoName = makeNameUnique(field.GoName)
531 if field.OneofType != nil {
532 if !seenOneofs[field.OneofType.Desc.Index()] {
533 // If this is a field in a oneof that we haven't seen before,
534 // make the name for that oneof unique as well.
535 field.OneofType.GoName = makeNameUnique(field.OneofType.GoName)
536 seenOneofs[field.OneofType.Desc.Index()] = true
537 }
538 }
Damien Neil658051b2018-09-10 12:26:21 -0700539 }
540
Damien Neil1fa78d82018-09-13 13:12:36 -0700541 return message
Damien Neil658051b2018-09-10 12:26:21 -0700542}
543
Damien Neil0bd5a382018-09-13 15:07:10 -0700544func (message *Message) init(gen *Plugin) error {
545 for _, child := range message.Messages {
546 if err := child.init(gen); err != nil {
547 return err
548 }
549 }
550 for _, field := range message.Fields {
551 if err := field.init(gen); err != nil {
552 return err
553 }
554 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700555 for _, oneof := range message.Oneofs {
556 oneof.init(gen, message)
557 }
Damien Neil993c04d2018-09-14 15:41:11 -0700558 for _, extension := range message.Extensions {
559 if err := extension.init(gen); err != nil {
560 return err
561 }
562 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700563 return nil
564}
565
Damien Neil658051b2018-09-10 12:26:21 -0700566// A Field describes a message field.
567type Field struct {
568 Desc protoreflect.FieldDescriptor
569
Damien Neil1fa78d82018-09-13 13:12:36 -0700570 // GoName is the base name of this field's Go field and methods.
Damien Neil658051b2018-09-10 12:26:21 -0700571 // For code generated by protoc-gen-go, this means a field named
Damien Neil1fa78d82018-09-13 13:12:36 -0700572 // '{{GoName}}' and a getter method named 'Get{{GoName}}'.
573 GoName string
Damien Neil658051b2018-09-10 12:26:21 -0700574
Damien Neil993c04d2018-09-14 15:41:11 -0700575 ParentMessage *Message // message in which this field is defined; nil if top-level extension
576 ExtendedType *Message // extended message for extension fields; nil otherwise
577 MessageType *Message // type for message or group fields; nil otherwise
578 EnumType *Enum // type for enum fields; nil otherwise
579 OneofType *Oneof // containing oneof; nil if not part of a oneof
580 Path []int32 // location path of this field
Damien Neil658051b2018-09-10 12:26:21 -0700581}
582
Damien Neil1fa78d82018-09-13 13:12:36 -0700583func newField(gen *Plugin, f *File, message *Message, desc protoreflect.FieldDescriptor) *Field {
Damien Neil993c04d2018-09-14 15:41:11 -0700584 var path []int32
585 switch {
586 case desc.ExtendedType() != nil && message == nil:
587 path = []int32{fileExtensionField, int32(desc.Index())}
588 case desc.ExtendedType() != nil && message != nil:
589 path = pathAppend(message.Path, messageExtensionField, int32(desc.Index()))
590 default:
591 path = pathAppend(message.Path, messageFieldField, int32(desc.Index()))
592 }
Damien Neil658051b2018-09-10 12:26:21 -0700593 field := &Field{
Damien Neil993c04d2018-09-14 15:41:11 -0700594 Desc: desc,
595 GoName: camelCase(string(desc.Name())),
596 ParentMessage: message,
597 Path: path,
Damien Neil658051b2018-09-10 12:26:21 -0700598 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700599 if desc.OneofType() != nil {
600 field.OneofType = message.Oneofs[desc.OneofType().Index()]
601 }
602 return field
Damien Neil0bd5a382018-09-13 15:07:10 -0700603}
604
Damien Neil993c04d2018-09-14 15:41:11 -0700605// Extension is an alias of Field for documentation.
606type Extension = Field
607
Damien Neil0bd5a382018-09-13 15:07:10 -0700608func (field *Field) init(gen *Plugin) error {
609 desc := field.Desc
Damien Neil658051b2018-09-10 12:26:21 -0700610 switch desc.Kind() {
611 case protoreflect.MessageKind, protoreflect.GroupKind:
612 mname := desc.MessageType().FullName()
613 message, ok := gen.messagesByName[mname]
614 if !ok {
Damien Neil0bd5a382018-09-13 15:07:10 -0700615 return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), mname)
Damien Neil658051b2018-09-10 12:26:21 -0700616 }
617 field.MessageType = message
618 case protoreflect.EnumKind:
Damien Neil0bd5a382018-09-13 15:07:10 -0700619 ename := field.Desc.EnumType().FullName()
Damien Neil658051b2018-09-10 12:26:21 -0700620 enum, ok := gen.enumsByName[ename]
621 if !ok {
Damien Neil0bd5a382018-09-13 15:07:10 -0700622 return fmt.Errorf("field %v: no descriptor for enum %v", desc.FullName(), ename)
Damien Neil658051b2018-09-10 12:26:21 -0700623 }
624 field.EnumType = enum
625 }
Damien Neil993c04d2018-09-14 15:41:11 -0700626 if desc.ExtendedType() != nil {
627 mname := desc.ExtendedType().FullName()
628 message, ok := gen.messagesByName[mname]
629 if !ok {
630 return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), mname)
631 }
632 field.ExtendedType = message
633 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700634 return nil
Damien Neil46abb572018-09-07 12:45:37 -0700635}
636
Damien Neil1fa78d82018-09-13 13:12:36 -0700637// A Oneof describes a oneof field.
638type Oneof struct {
639 Desc protoreflect.OneofDescriptor
640
Damien Neil993c04d2018-09-14 15:41:11 -0700641 GoName string // Go field name of this oneof
642 ParentMessage *Message // message in which this oneof occurs
643 Fields []*Field // fields that are part of this oneof
644 Path []int32 // location path of this oneof
Damien Neil1fa78d82018-09-13 13:12:36 -0700645}
646
647func newOneof(gen *Plugin, f *File, message *Message, desc protoreflect.OneofDescriptor) *Oneof {
648 return &Oneof{
Damien Neil993c04d2018-09-14 15:41:11 -0700649 Desc: desc,
650 ParentMessage: message,
651 GoName: camelCase(string(desc.Name())),
652 Path: pathAppend(message.Path, messageOneofField, int32(desc.Index())),
Damien Neil1fa78d82018-09-13 13:12:36 -0700653 }
654}
655
656func (oneof *Oneof) init(gen *Plugin, parent *Message) {
657 for i, fdescs := 0, oneof.Desc.Fields(); i < fdescs.Len(); i++ {
658 oneof.Fields = append(oneof.Fields, parent.Fields[fdescs.Get(i).Index()])
659 }
660}
661
Damien Neil46abb572018-09-07 12:45:37 -0700662// An Enum describes an enum.
663type Enum struct {
664 Desc protoreflect.EnumDescriptor
665
666 GoIdent GoIdent // name of the generated Go type
667 Values []*EnumValue // enum values
668 Path []int32 // location path of this enum
669}
670
671func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum {
672 var path []int32
673 if parent != nil {
674 path = pathAppend(parent.Path, messageEnumField, int32(desc.Index()))
675 } else {
676 path = []int32{fileEnumField, int32(desc.Index())}
677 }
678 enum := &Enum{
679 Desc: desc,
680 GoIdent: newGoIdent(f, desc),
681 Path: path,
682 }
Damien Neil658051b2018-09-10 12:26:21 -0700683 gen.enumsByName[desc.FullName()] = enum
Damien Neil46abb572018-09-07 12:45:37 -0700684 for i, evdescs := 0, enum.Desc.Values(); i < evdescs.Len(); i++ {
685 enum.Values = append(enum.Values, newEnumValue(gen, f, parent, enum, evdescs.Get(i)))
686 }
687 return enum
688}
689
690// An EnumValue describes an enum value.
691type EnumValue struct {
692 Desc protoreflect.EnumValueDescriptor
693
694 GoIdent GoIdent // name of the generated Go type
695 Path []int32 // location path of this enum value
696}
697
698func newEnumValue(gen *Plugin, f *File, message *Message, enum *Enum, desc protoreflect.EnumValueDescriptor) *EnumValue {
699 // A top-level enum value's name is: EnumName_ValueName
700 // An enum value contained in a message is: MessageName_ValueName
701 //
702 // Enum value names are not camelcased.
703 parentIdent := enum.GoIdent
704 if message != nil {
705 parentIdent = message.GoIdent
706 }
707 name := parentIdent.GoName + "_" + string(desc.Name())
708 return &EnumValue{
709 Desc: desc,
710 GoIdent: GoIdent{
711 GoName: name,
712 GoImportPath: f.GoImportPath,
713 },
714 Path: pathAppend(enum.Path, enumValueField, int32(desc.Index())),
715 }
Damien Neil220c2022018-08-15 11:24:18 -0700716}
717
718// A GeneratedFile is a generated file.
719type GeneratedFile struct {
Damien Neil1fa8ab02018-09-27 15:51:05 -0700720 gen *Plugin
Damien Neild9016772018-08-23 14:39:30 -0700721 filename string
722 goImportPath GoImportPath
723 buf bytes.Buffer
724 packageNames map[GoImportPath]GoPackageName
725 usedPackageNames map[GoPackageName]bool
Damien Neil2e0c3da2018-09-19 12:51:36 -0700726 manualImports map[GoImportPath]bool
Damien Neil220c2022018-08-15 11:24:18 -0700727}
728
Damien Neild9016772018-08-23 14:39:30 -0700729// NewGeneratedFile creates a new generated file with the given filename
730// and import path.
731func (gen *Plugin) NewGeneratedFile(filename string, goImportPath GoImportPath) *GeneratedFile {
Damien Neil220c2022018-08-15 11:24:18 -0700732 g := &GeneratedFile{
Damien Neil1fa8ab02018-09-27 15:51:05 -0700733 gen: gen,
Damien Neild9016772018-08-23 14:39:30 -0700734 filename: filename,
735 goImportPath: goImportPath,
736 packageNames: make(map[GoImportPath]GoPackageName),
737 usedPackageNames: make(map[GoPackageName]bool),
Damien Neil2e0c3da2018-09-19 12:51:36 -0700738 manualImports: make(map[GoImportPath]bool),
Damien Neil220c2022018-08-15 11:24:18 -0700739 }
740 gen.genFiles = append(gen.genFiles, g)
741 return g
742}
743
Damien Neil2dc67182018-09-21 15:03:34 -0700744// A Service describes a service.
745type Service struct {
746 Desc protoreflect.ServiceDescriptor
747
748 GoName string
749 Path []int32 // location path of this service
750 Methods []*Method // service method definitions
751}
752
753func newService(gen *Plugin, f *File, desc protoreflect.ServiceDescriptor) *Service {
754 service := &Service{
755 Desc: desc,
756 GoName: camelCase(string(desc.Name())),
757 Path: []int32{fileServiceField, int32(desc.Index())},
758 }
759 for i, mdescs := 0, desc.Methods(); i < mdescs.Len(); i++ {
760 service.Methods = append(service.Methods, newMethod(gen, f, service, mdescs.Get(i)))
761 }
762 return service
763}
764
765// A Method describes a method in a service.
766type Method struct {
767 Desc protoreflect.MethodDescriptor
768
769 GoName string
770 ParentService *Service
771 Path []int32 // location path of this method
772 InputType *Message
773 OutputType *Message
774}
775
776func newMethod(gen *Plugin, f *File, service *Service, desc protoreflect.MethodDescriptor) *Method {
777 method := &Method{
778 Desc: desc,
779 GoName: camelCase(string(desc.Name())),
780 ParentService: service,
781 Path: pathAppend(service.Path, serviceMethodField, int32(desc.Index())),
782 }
783 return method
784}
785
786func (method *Method) init(gen *Plugin) error {
787 desc := method.Desc
788
789 inName := desc.InputType().FullName()
790 in, ok := gen.messagesByName[inName]
791 if !ok {
792 return fmt.Errorf("method %v: no descriptor for type %v", desc.FullName(), inName)
793 }
794 method.InputType = in
795
796 outName := desc.OutputType().FullName()
797 out, ok := gen.messagesByName[outName]
798 if !ok {
799 return fmt.Errorf("method %v: no descriptor for type %v", desc.FullName(), outName)
800 }
801 method.OutputType = out
802
803 return nil
804}
805
Damien Neil220c2022018-08-15 11:24:18 -0700806// P prints a line to the generated output. It converts each parameter to a
807// string following the same rules as fmt.Print. It never inserts spaces
808// between parameters.
809//
810// TODO: .meta file annotations.
811func (g *GeneratedFile) P(v ...interface{}) {
812 for _, x := range v {
Damien Neild9016772018-08-23 14:39:30 -0700813 switch x := x.(type) {
814 case GoIdent:
Damien Neil46abb572018-09-07 12:45:37 -0700815 fmt.Fprint(&g.buf, g.QualifiedGoIdent(x))
Damien Neild9016772018-08-23 14:39:30 -0700816 default:
817 fmt.Fprint(&g.buf, x)
818 }
Damien Neil220c2022018-08-15 11:24:18 -0700819 }
820 fmt.Fprintln(&g.buf)
821}
822
Damien Neil46abb572018-09-07 12:45:37 -0700823// QualifiedGoIdent returns the string to use for a Go identifier.
824//
825// If the identifier is from a different Go package than the generated file,
826// the returned name will be qualified (package.name) and an import statement
827// for the identifier's package will be included in the file.
828func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string {
829 if ident.GoImportPath == g.goImportPath {
830 return ident.GoName
831 }
832 if packageName, ok := g.packageNames[ident.GoImportPath]; ok {
833 return string(packageName) + "." + ident.GoName
834 }
835 packageName := cleanPackageName(baseName(string(ident.GoImportPath)))
Damien Neil87214662018-10-05 11:23:35 -0700836 for i, orig := 1, packageName; g.usedPackageNames[packageName] || isGoPredeclaredIdentifier[string(packageName)]; i++ {
Damien Neil46abb572018-09-07 12:45:37 -0700837 packageName = orig + GoPackageName(strconv.Itoa(i))
838 }
839 g.packageNames[ident.GoImportPath] = packageName
840 g.usedPackageNames[packageName] = true
841 return string(packageName) + "." + ident.GoName
842}
843
Damien Neil2e0c3da2018-09-19 12:51:36 -0700844// Import ensures a package is imported by the generated file.
845//
846// Packages referenced by QualifiedGoIdent are automatically imported.
847// Explicitly importing a package with Import is generally only necessary
848// when the import will be blank (import _ "package").
849func (g *GeneratedFile) Import(importPath GoImportPath) {
850 g.manualImports[importPath] = true
851}
852
Damien Neil220c2022018-08-15 11:24:18 -0700853// Write implements io.Writer.
854func (g *GeneratedFile) Write(p []byte) (n int, err error) {
855 return g.buf.Write(p)
856}
857
858// Content returns the contents of the generated file.
Damien Neilc7d07d92018-08-22 13:46:02 -0700859func (g *GeneratedFile) Content() ([]byte, error) {
Damien Neild9016772018-08-23 14:39:30 -0700860 if !strings.HasSuffix(g.filename, ".go") {
Damien Neilc7d07d92018-08-22 13:46:02 -0700861 return g.buf.Bytes(), nil
862 }
863
864 // Reformat generated code.
865 original := g.buf.Bytes()
866 fset := token.NewFileSet()
Damien Neil1ec33152018-09-13 13:12:36 -0700867 file, err := parser.ParseFile(fset, "", original, parser.ParseComments)
Damien Neilc7d07d92018-08-22 13:46:02 -0700868 if err != nil {
869 // Print out the bad code with line numbers.
870 // This should never happen in practice, but it can while changing generated code
871 // so consider this a debugging aid.
872 var src bytes.Buffer
873 s := bufio.NewScanner(bytes.NewReader(original))
874 for line := 1; s.Scan(); line++ {
875 fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
876 }
Damien Neild9016772018-08-23 14:39:30 -0700877 return nil, fmt.Errorf("%v: unparsable Go source: %v\n%v", g.filename, err, src.String())
Damien Neilc7d07d92018-08-22 13:46:02 -0700878 }
Damien Neild9016772018-08-23 14:39:30 -0700879
880 // Add imports.
881 var importPaths []string
882 for importPath := range g.packageNames {
883 importPaths = append(importPaths, string(importPath))
884 }
885 sort.Strings(importPaths)
Damien Neil1fa8ab02018-09-27 15:51:05 -0700886 rewriteImport := func(importPath string) string {
887 if f := g.gen.opts.ImportRewriteFunc; f != nil {
888 return string(f(GoImportPath(importPath)))
889 }
890 return importPath
891 }
Damien Neild9016772018-08-23 14:39:30 -0700892 for _, importPath := range importPaths {
Damien Neil1fa8ab02018-09-27 15:51:05 -0700893 astutil.AddNamedImport(fset, file, string(g.packageNames[GoImportPath(importPath)]), rewriteImport(importPath))
Damien Neild9016772018-08-23 14:39:30 -0700894 }
Damien Neil2e0c3da2018-09-19 12:51:36 -0700895 for importPath := range g.manualImports {
896 if _, ok := g.packageNames[importPath]; ok {
897 continue
898 }
Damien Neil329b75e2018-10-04 17:31:07 -0700899 astutil.AddNamedImport(fset, file, "_", rewriteImport(string(importPath)))
Damien Neil2e0c3da2018-09-19 12:51:36 -0700900 }
Damien Neil1ec33152018-09-13 13:12:36 -0700901 ast.SortImports(fset, file)
Damien Neild9016772018-08-23 14:39:30 -0700902
Damien Neilc7d07d92018-08-22 13:46:02 -0700903 var out bytes.Buffer
Damien Neil1ec33152018-09-13 13:12:36 -0700904 if err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(&out, fset, file); err != nil {
Damien Neild9016772018-08-23 14:39:30 -0700905 return nil, fmt.Errorf("%v: can not reformat Go source: %v", g.filename, err)
Damien Neilc7d07d92018-08-22 13:46:02 -0700906 }
Damien Neild9016772018-08-23 14:39:30 -0700907 // TODO: Annotations.
Damien Neilc7d07d92018-08-22 13:46:02 -0700908 return out.Bytes(), nil
909
Damien Neil220c2022018-08-15 11:24:18 -0700910}
Damien Neil082ce922018-09-06 10:23:53 -0700911
912type pathType int
913
914const (
915 pathTypeImport pathType = iota
916 pathTypeSourceRelative
917)
Damien Neilcab8dfe2018-09-06 14:51:28 -0700918
919// The SourceCodeInfo message describes the location of elements of a parsed
920// .proto file by way of a "path", which is a sequence of integers that
921// describe the route from a FileDescriptorProto to the relevant submessage.
922// The path alternates between a field number of a repeated field, and an index
923// into that repeated field. The constants below define the field numbers that
924// are used.
925//
926// See descriptor.proto for more information about this.
927const (
928 // field numbers in FileDescriptorProto
Damien Neil993c04d2018-09-14 15:41:11 -0700929 filePackageField = 2 // package
930 fileMessageField = 4 // message_type
931 fileEnumField = 5 // enum_type
Damien Neil2dc67182018-09-21 15:03:34 -0700932 fileServiceField = 6 // service
Damien Neil993c04d2018-09-14 15:41:11 -0700933 fileExtensionField = 7 // extension
Damien Neilcab8dfe2018-09-06 14:51:28 -0700934 // field numbers in DescriptorProto
Damien Neil993c04d2018-09-14 15:41:11 -0700935 messageFieldField = 2 // field
936 messageMessageField = 3 // nested_type
937 messageEnumField = 4 // enum_type
938 messageExtensionField = 6 // extension
939 messageOneofField = 8 // oneof_decl
Damien Neilcab8dfe2018-09-06 14:51:28 -0700940 // field numbers in EnumDescriptorProto
941 enumValueField = 2 // value
Damien Neil2dc67182018-09-21 15:03:34 -0700942 // field numbers in ServiceDescriptorProto
943 serviceMethodField = 2 // method
944 serviceStreamField = 4 // stream
Damien Neilcab8dfe2018-09-06 14:51:28 -0700945)
946
947// pathAppend appends elements to a location path.
948// It does not alias the original path.
949func pathAppend(path []int32, a ...int32) []int32 {
950 var n []int32
951 n = append(n, path...)
952 n = append(n, a...)
953 return n
954}