blob: 8951ca0f91605a54b930a8d9e153f689e47d95b2 [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"
Damien Neilba1159f2018-10-17 12:53:18 -070016 "encoding/binary"
Damien Neil220c2022018-08-15 11:24:18 -070017 "fmt"
Damien Neil1ec33152018-09-13 13:12:36 -070018 "go/ast"
Damien Neilc7d07d92018-08-22 13:46:02 -070019 "go/parser"
20 "go/printer"
21 "go/token"
Damien Neil220c2022018-08-15 11:24:18 -070022 "io/ioutil"
23 "os"
Damien Neil082ce922018-09-06 10:23:53 -070024 "path"
Damien Neil220c2022018-08-15 11:24:18 -070025 "path/filepath"
Damien Neild9016772018-08-23 14:39:30 -070026 "sort"
27 "strconv"
Damien Neil220c2022018-08-15 11:24:18 -070028 "strings"
29
30 "github.com/golang/protobuf/proto"
31 descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
32 pluginpb "github.com/golang/protobuf/protoc-gen-go/plugin"
Joe Tsai009e0672018-11-27 18:45:07 -080033 "github.com/golang/protobuf/v2/internal/scalar"
Joe Tsai01ab2962018-09-21 17:44:00 -070034 "github.com/golang/protobuf/v2/reflect/protoreflect"
35 "github.com/golang/protobuf/v2/reflect/protoregistry"
36 "github.com/golang/protobuf/v2/reflect/prototype"
Damien Neild9016772018-08-23 14:39:30 -070037 "golang.org/x/tools/go/ast/astutil"
Damien Neil220c2022018-08-15 11:24:18 -070038)
39
40// Run executes a function as a protoc plugin.
41//
42// It reads a CodeGeneratorRequest message from os.Stdin, invokes the plugin
43// function, and writes a CodeGeneratorResponse message to os.Stdout.
44//
45// If a failure occurs while reading or writing, Run prints an error to
46// os.Stderr and calls os.Exit(1).
Damien Neil3cf6e622018-09-11 13:53:14 -070047//
48// Passing a nil options is equivalent to passing a zero-valued one.
49func Run(opts *Options, f func(*Plugin) error) {
50 if err := run(opts, f); err != nil {
Damien Neil220c2022018-08-15 11:24:18 -070051 fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err)
52 os.Exit(1)
53 }
54}
55
Damien Neil3cf6e622018-09-11 13:53:14 -070056func run(opts *Options, f func(*Plugin) error) error {
Damien Neild277b522018-10-04 15:30:51 -070057 if len(os.Args) > 1 {
58 return fmt.Errorf("unknown argument %q (this program should be run by protoc, not directly)", os.Args[1])
59 }
Damien Neil220c2022018-08-15 11:24:18 -070060 in, err := ioutil.ReadAll(os.Stdin)
61 if err != nil {
62 return err
63 }
64 req := &pluginpb.CodeGeneratorRequest{}
65 if err := proto.Unmarshal(in, req); err != nil {
66 return err
67 }
Damien Neil3cf6e622018-09-11 13:53:14 -070068 gen, err := New(req, opts)
Damien Neil220c2022018-08-15 11:24:18 -070069 if err != nil {
70 return err
71 }
72 if err := f(gen); err != nil {
73 // Errors from the plugin function are reported by setting the
74 // error field in the CodeGeneratorResponse.
75 //
76 // In contrast, errors that indicate a problem in protoc
77 // itself (unparsable input, I/O errors, etc.) are reported
78 // to stderr.
79 gen.Error(err)
80 }
81 resp := gen.Response()
82 out, err := proto.Marshal(resp)
83 if err != nil {
84 return err
85 }
86 if _, err := os.Stdout.Write(out); err != nil {
87 return err
88 }
89 return nil
90}
91
92// A Plugin is a protoc plugin invocation.
93type Plugin struct {
94 // Request is the CodeGeneratorRequest provided by protoc.
95 Request *pluginpb.CodeGeneratorRequest
96
97 // Files is the set of files to generate and everything they import.
98 // Files appear in topological order, so each file appears before any
99 // file that imports it.
100 Files []*File
101 filesByName map[string]*File
102
Damien Neil658051b2018-09-10 12:26:21 -0700103 fileReg *protoregistry.Files
104 messagesByName map[protoreflect.FullName]*Message
105 enumsByName map[protoreflect.FullName]*Enum
Damien Neil162c1272018-10-04 12:42:37 -0700106 annotateCode bool
Damien Neil658051b2018-09-10 12:26:21 -0700107 pathType pathType
108 genFiles []*GeneratedFile
Damien Neil1fa8ab02018-09-27 15:51:05 -0700109 opts *Options
Damien Neil658051b2018-09-10 12:26:21 -0700110 err error
Damien Neil220c2022018-08-15 11:24:18 -0700111}
112
Damien Neil3cf6e622018-09-11 13:53:14 -0700113// Options are optional parameters to New.
114type Options struct {
115 // If ParamFunc is non-nil, it will be called with each unknown
116 // generator parameter.
117 //
118 // Plugins for protoc can accept parameters from the command line,
119 // passed in the --<lang>_out protoc, separated from the output
120 // directory with a colon; e.g.,
121 //
122 // --go_out=<param1>=<value1>,<param2>=<value2>:<output_directory>
123 //
124 // Parameters passed in this fashion as a comma-separated list of
125 // key=value pairs will be passed to the ParamFunc.
126 //
127 // The (flag.FlagSet).Set method matches this function signature,
128 // so parameters can be converted into flags as in the following:
129 //
130 // var flags flag.FlagSet
131 // value := flags.Bool("param", false, "")
132 // opts := &protogen.Options{
133 // ParamFunc: flags.Set,
134 // }
135 // protogen.Run(opts, func(p *protogen.Plugin) error {
136 // if *value { ... }
137 // })
138 ParamFunc func(name, value string) error
Damien Neil1fa8ab02018-09-27 15:51:05 -0700139
140 // ImportRewriteFunc is called with the import path of each package
141 // imported by a generated file. It returns the import path to use
142 // for this package.
143 ImportRewriteFunc func(GoImportPath) GoImportPath
Damien Neil3cf6e622018-09-11 13:53:14 -0700144}
145
Damien Neil220c2022018-08-15 11:24:18 -0700146// New returns a new Plugin.
Damien Neil3cf6e622018-09-11 13:53:14 -0700147//
148// Passing a nil Options is equivalent to passing a zero-valued one.
149func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
150 if opts == nil {
151 opts = &Options{}
152 }
Damien Neil220c2022018-08-15 11:24:18 -0700153 gen := &Plugin{
Damien Neil658051b2018-09-10 12:26:21 -0700154 Request: req,
155 filesByName: make(map[string]*File),
156 fileReg: protoregistry.NewFiles(),
157 messagesByName: make(map[protoreflect.FullName]*Message),
158 enumsByName: make(map[protoreflect.FullName]*Enum),
Damien Neil1fa8ab02018-09-27 15:51:05 -0700159 opts: opts,
Damien Neil220c2022018-08-15 11:24:18 -0700160 }
161
Damien Neil082ce922018-09-06 10:23:53 -0700162 packageNames := make(map[string]GoPackageName) // filename -> package name
163 importPaths := make(map[string]GoImportPath) // filename -> import path
164 var packageImportPath GoImportPath
Damien Neil220c2022018-08-15 11:24:18 -0700165 for _, param := range strings.Split(req.GetParameter(), ",") {
166 var value string
167 if i := strings.Index(param, "="); i >= 0 {
168 value = param[i+1:]
169 param = param[0:i]
170 }
171 switch param {
172 case "":
173 // Ignore.
Damien Neil220c2022018-08-15 11:24:18 -0700174 case "import_path":
Damien Neil082ce922018-09-06 10:23:53 -0700175 packageImportPath = GoImportPath(value)
Damien Neil220c2022018-08-15 11:24:18 -0700176 case "paths":
Damien Neil082ce922018-09-06 10:23:53 -0700177 switch value {
178 case "import":
179 gen.pathType = pathTypeImport
180 case "source_relative":
181 gen.pathType = pathTypeSourceRelative
182 default:
183 return nil, fmt.Errorf(`unknown path type %q: want "import" or "source_relative"`, value)
184 }
Damien Neil220c2022018-08-15 11:24:18 -0700185 case "annotate_code":
Damien Neil162c1272018-10-04 12:42:37 -0700186 switch value {
187 case "true", "":
188 gen.annotateCode = true
189 case "false":
190 default:
191 return nil, fmt.Errorf(`bad value for parameter %q: want "true" or "false"`, param)
192 }
Damien Neil220c2022018-08-15 11:24:18 -0700193 default:
Damien Neil3cf6e622018-09-11 13:53:14 -0700194 if param[0] == 'M' {
195 importPaths[param[1:]] = GoImportPath(value)
196 continue
Damien Neil220c2022018-08-15 11:24:18 -0700197 }
Damien Neil3cf6e622018-09-11 13:53:14 -0700198 if opts.ParamFunc != nil {
199 if err := opts.ParamFunc(param, value); err != nil {
200 return nil, err
201 }
202 }
Damien Neil082ce922018-09-06 10:23:53 -0700203 }
204 }
205
206 // Figure out the import path and package name for each file.
207 //
208 // The rules here are complicated and have grown organically over time.
209 // Interactions between different ways of specifying package information
210 // may be surprising.
211 //
212 // The recommended approach is to include a go_package option in every
213 // .proto source file specifying the full import path of the Go package
214 // associated with this file.
215 //
216 // option go_package = "github.com/golang/protobuf/ptypes/any";
217 //
218 // Build systems which want to exert full control over import paths may
219 // specify M<filename>=<import_path> flags.
220 //
221 // Other approaches are not recommend.
222 generatedFileNames := make(map[string]bool)
223 for _, name := range gen.Request.FileToGenerate {
224 generatedFileNames[name] = true
225 }
226 // We need to determine the import paths before the package names,
227 // because the Go package name for a file is sometimes derived from
228 // different file in the same package.
229 packageNameForImportPath := make(map[GoImportPath]GoPackageName)
230 for _, fdesc := range gen.Request.ProtoFile {
231 filename := fdesc.GetName()
232 packageName, importPath := goPackageOption(fdesc)
233 switch {
234 case importPaths[filename] != "":
235 // Command line: M=foo.proto=quux/bar
236 //
237 // Explicit mapping of source file to import path.
238 case generatedFileNames[filename] && packageImportPath != "":
239 // Command line: import_path=quux/bar
240 //
241 // The import_path flag sets the import path for every file that
242 // we generate code for.
243 importPaths[filename] = packageImportPath
244 case importPath != "":
245 // Source file: option go_package = "quux/bar";
246 //
247 // The go_package option sets the import path. Most users should use this.
248 importPaths[filename] = importPath
249 default:
250 // Source filename.
251 //
252 // Last resort when nothing else is available.
253 importPaths[filename] = GoImportPath(path.Dir(filename))
254 }
255 if packageName != "" {
256 packageNameForImportPath[importPaths[filename]] = packageName
257 }
258 }
259 for _, fdesc := range gen.Request.ProtoFile {
260 filename := fdesc.GetName()
261 packageName, _ := goPackageOption(fdesc)
262 defaultPackageName := packageNameForImportPath[importPaths[filename]]
263 switch {
264 case packageName != "":
265 // Source file: option go_package = "quux/bar";
266 packageNames[filename] = packageName
267 case defaultPackageName != "":
268 // A go_package option in another file in the same package.
269 //
270 // This is a poor choice in general, since every source file should
271 // contain a go_package option. Supported mainly for historical
272 // compatibility.
273 packageNames[filename] = defaultPackageName
274 case generatedFileNames[filename] && packageImportPath != "":
275 // Command line: import_path=quux/bar
276 packageNames[filename] = cleanPackageName(path.Base(string(packageImportPath)))
277 case fdesc.GetPackage() != "":
278 // Source file: package quux.bar;
279 packageNames[filename] = cleanPackageName(fdesc.GetPackage())
280 default:
281 // Source filename.
282 packageNames[filename] = cleanPackageName(baseName(filename))
283 }
284 }
285
286 // Consistency check: Every file with the same Go import path should have
287 // the same Go package name.
288 packageFiles := make(map[GoImportPath][]string)
289 for filename, importPath := range importPaths {
Damien Neilbbbd38f2018-10-08 16:36:49 -0700290 if _, ok := packageNames[filename]; !ok {
291 // Skip files mentioned in a M<file>=<import_path> parameter
292 // but which do not appear in the CodeGeneratorRequest.
293 continue
294 }
Damien Neil082ce922018-09-06 10:23:53 -0700295 packageFiles[importPath] = append(packageFiles[importPath], filename)
296 }
297 for importPath, filenames := range packageFiles {
298 for i := 1; i < len(filenames); i++ {
299 if a, b := packageNames[filenames[0]], packageNames[filenames[i]]; a != b {
300 return nil, fmt.Errorf("Go package %v has inconsistent names %v (%v) and %v (%v)",
301 importPath, a, filenames[0], b, filenames[i])
302 }
Damien Neil220c2022018-08-15 11:24:18 -0700303 }
304 }
305
306 for _, fdesc := range gen.Request.ProtoFile {
Damien Neil082ce922018-09-06 10:23:53 -0700307 filename := fdesc.GetName()
308 if gen.filesByName[filename] != nil {
309 return nil, fmt.Errorf("duplicate file name: %q", filename)
310 }
311 f, err := newFile(gen, fdesc, packageNames[filename], importPaths[filename])
Damien Neilabc6fc12018-08-23 14:39:30 -0700312 if err != nil {
313 return nil, err
314 }
Damien Neil220c2022018-08-15 11:24:18 -0700315 gen.Files = append(gen.Files, f)
Damien Neil082ce922018-09-06 10:23:53 -0700316 gen.filesByName[filename] = f
Damien Neil220c2022018-08-15 11:24:18 -0700317 }
Damien Neil082ce922018-09-06 10:23:53 -0700318 for _, filename := range gen.Request.FileToGenerate {
319 f, ok := gen.FileByName(filename)
Damien Neil220c2022018-08-15 11:24:18 -0700320 if !ok {
Damien Neil082ce922018-09-06 10:23:53 -0700321 return nil, fmt.Errorf("no descriptor for generated file: %v", filename)
Damien Neil220c2022018-08-15 11:24:18 -0700322 }
323 f.Generate = true
324 }
325 return gen, nil
326}
327
328// Error records an error in code generation. The generator will report the
329// error back to protoc and will not produce output.
330func (gen *Plugin) Error(err error) {
331 if gen.err == nil {
332 gen.err = err
333 }
334}
335
336// Response returns the generator output.
337func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse {
338 resp := &pluginpb.CodeGeneratorResponse{}
339 if gen.err != nil {
Joe Tsai009e0672018-11-27 18:45:07 -0800340 resp.Error = scalar.String(gen.err.Error())
Damien Neil220c2022018-08-15 11:24:18 -0700341 return resp
342 }
Damien Neil162c1272018-10-04 12:42:37 -0700343 for _, g := range gen.genFiles {
344 content, err := g.content()
Damien Neilc7d07d92018-08-22 13:46:02 -0700345 if err != nil {
346 return &pluginpb.CodeGeneratorResponse{
Joe Tsai009e0672018-11-27 18:45:07 -0800347 Error: scalar.String(err.Error()),
Damien Neilc7d07d92018-08-22 13:46:02 -0700348 }
349 }
Damien Neil220c2022018-08-15 11:24:18 -0700350 resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
Joe Tsai009e0672018-11-27 18:45:07 -0800351 Name: scalar.String(g.filename),
352 Content: scalar.String(string(content)),
Damien Neil220c2022018-08-15 11:24:18 -0700353 })
Damien Neil162c1272018-10-04 12:42:37 -0700354 if gen.annotateCode && strings.HasSuffix(g.filename, ".go") {
355 meta, err := g.metaFile(content)
356 if err != nil {
357 return &pluginpb.CodeGeneratorResponse{
Joe Tsai009e0672018-11-27 18:45:07 -0800358 Error: scalar.String(err.Error()),
Damien Neil162c1272018-10-04 12:42:37 -0700359 }
360 }
361 resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
Joe Tsai009e0672018-11-27 18:45:07 -0800362 Name: scalar.String(g.filename + ".meta"),
363 Content: scalar.String(meta),
Damien Neil162c1272018-10-04 12:42:37 -0700364 })
365 }
Damien Neil220c2022018-08-15 11:24:18 -0700366 }
367 return resp
368}
369
370// FileByName returns the file with the given name.
371func (gen *Plugin) FileByName(name string) (f *File, ok bool) {
372 f, ok = gen.filesByName[name]
373 return f, ok
374}
375
Damien Neilc7d07d92018-08-22 13:46:02 -0700376// A File describes a .proto source file.
Damien Neil220c2022018-08-15 11:24:18 -0700377type File struct {
Damien Neil7779e052018-09-07 14:14:06 -0700378 Desc protoreflect.FileDescriptor
379 Proto *descpb.FileDescriptorProto
Damien Neil220c2022018-08-15 11:24:18 -0700380
Damien Neil082ce922018-09-06 10:23:53 -0700381 GoPackageName GoPackageName // name of this file's Go package
382 GoImportPath GoImportPath // import path of this file's Go package
383 Messages []*Message // top-level message declarations
Damien Neil46abb572018-09-07 12:45:37 -0700384 Enums []*Enum // top-level enum declarations
Damien Neil993c04d2018-09-14 15:41:11 -0700385 Extensions []*Extension // top-level extension declarations
Damien Neil2dc67182018-09-21 15:03:34 -0700386 Services []*Service // top-level service declarations
Damien Neil082ce922018-09-06 10:23:53 -0700387 Generate bool // true if we should generate code for this file
388
389 // GeneratedFilenamePrefix is used to construct filenames for generated
390 // files associated with this source file.
391 //
392 // For example, the source file "dir/foo.proto" might have a filename prefix
393 // of "dir/foo". Appending ".pb.go" produces an output file of "dir/foo.pb.go".
394 GeneratedFilenamePrefix string
Damien Neilba1159f2018-10-17 12:53:18 -0700395
396 sourceInfo map[pathKey][]*descpb.SourceCodeInfo_Location
Damien Neil220c2022018-08-15 11:24:18 -0700397}
398
Damien Neil082ce922018-09-06 10:23:53 -0700399func newFile(gen *Plugin, p *descpb.FileDescriptorProto, packageName GoPackageName, importPath GoImportPath) (*File, error) {
Damien Neilabc6fc12018-08-23 14:39:30 -0700400 desc, err := prototype.NewFileFromDescriptorProto(p, gen.fileReg)
401 if err != nil {
402 return nil, fmt.Errorf("invalid FileDescriptorProto %q: %v", p.GetName(), err)
403 }
404 if err := gen.fileReg.Register(desc); err != nil {
405 return nil, fmt.Errorf("cannot register descriptor %q: %v", p.GetName(), err)
406 }
Damien Neilc7d07d92018-08-22 13:46:02 -0700407 f := &File{
Damien Neil082ce922018-09-06 10:23:53 -0700408 Desc: desc,
Damien Neil7779e052018-09-07 14:14:06 -0700409 Proto: p,
Damien Neil082ce922018-09-06 10:23:53 -0700410 GoPackageName: packageName,
411 GoImportPath: importPath,
Damien Neilba1159f2018-10-17 12:53:18 -0700412 sourceInfo: make(map[pathKey][]*descpb.SourceCodeInfo_Location),
Damien Neil220c2022018-08-15 11:24:18 -0700413 }
Damien Neil082ce922018-09-06 10:23:53 -0700414
415 // Determine the prefix for generated Go files.
416 prefix := p.GetName()
417 if ext := path.Ext(prefix); ext == ".proto" || ext == ".protodevel" {
418 prefix = prefix[:len(prefix)-len(ext)]
419 }
420 if gen.pathType == pathTypeImport {
421 // If paths=import (the default) and the file contains a go_package option
422 // with a full import path, the output filename is derived from the Go import
423 // path.
424 //
425 // Pass the paths=source_relative flag to always derive the output filename
426 // from the input filename instead.
427 if _, importPath := goPackageOption(p); importPath != "" {
428 prefix = path.Join(string(importPath), path.Base(prefix))
429 }
430 }
431 f.GeneratedFilenamePrefix = prefix
432
Damien Neilba1159f2018-10-17 12:53:18 -0700433 for _, loc := range p.GetSourceCodeInfo().GetLocation() {
434 key := newPathKey(loc.Path)
435 f.sourceInfo[key] = append(f.sourceInfo[key], loc)
436 }
Damien Neilabc6fc12018-08-23 14:39:30 -0700437 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700438 f.Messages = append(f.Messages, newMessage(gen, f, nil, mdescs.Get(i)))
Damien Neilc7d07d92018-08-22 13:46:02 -0700439 }
Damien Neil46abb572018-09-07 12:45:37 -0700440 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
441 f.Enums = append(f.Enums, newEnum(gen, f, nil, edescs.Get(i)))
442 }
Damien Neil993c04d2018-09-14 15:41:11 -0700443 for i, extdescs := 0, desc.Extensions(); i < extdescs.Len(); i++ {
444 f.Extensions = append(f.Extensions, newField(gen, f, nil, extdescs.Get(i)))
445 }
Damien Neil2dc67182018-09-21 15:03:34 -0700446 for i, sdescs := 0, desc.Services(); i < sdescs.Len(); i++ {
447 f.Services = append(f.Services, newService(gen, f, sdescs.Get(i)))
448 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700449 for _, message := range f.Messages {
Damien Neil993c04d2018-09-14 15:41:11 -0700450 if err := message.init(gen); err != nil {
451 return nil, err
452 }
453 }
454 for _, extension := range f.Extensions {
455 if err := extension.init(gen); err != nil {
456 return nil, err
457 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700458 }
Damien Neil2dc67182018-09-21 15:03:34 -0700459 for _, service := range f.Services {
460 for _, method := range service.Methods {
461 if err := method.init(gen); err != nil {
462 return nil, err
463 }
464 }
465 }
Damien Neilabc6fc12018-08-23 14:39:30 -0700466 return f, nil
Damien Neilc7d07d92018-08-22 13:46:02 -0700467}
468
Damien Neil162c1272018-10-04 12:42:37 -0700469func (f *File) location(path ...int32) Location {
470 return Location{
471 SourceFile: f.Desc.Path(),
472 Path: path,
473 }
474}
475
Damien Neil082ce922018-09-06 10:23:53 -0700476// goPackageOption interprets a file's go_package option.
477// If there is no go_package, it returns ("", "").
478// If there's a simple name, it returns (pkg, "").
479// If the option implies an import path, it returns (pkg, impPath).
480func goPackageOption(d *descpb.FileDescriptorProto) (pkg GoPackageName, impPath GoImportPath) {
481 opt := d.GetOptions().GetGoPackage()
482 if opt == "" {
483 return "", ""
484 }
485 // A semicolon-delimited suffix delimits the import path and package name.
486 if i := strings.Index(opt, ";"); i >= 0 {
487 return cleanPackageName(opt[i+1:]), GoImportPath(opt[:i])
488 }
489 // The presence of a slash implies there's an import path.
490 if i := strings.LastIndex(opt, "/"); i >= 0 {
491 return cleanPackageName(opt[i+1:]), GoImportPath(opt)
492 }
493 return cleanPackageName(opt), ""
494}
495
Damien Neilc7d07d92018-08-22 13:46:02 -0700496// A Message describes a message.
497type Message struct {
Damien Neilabc6fc12018-08-23 14:39:30 -0700498 Desc protoreflect.MessageDescriptor
Damien Neilc7d07d92018-08-22 13:46:02 -0700499
Damien Neil993c04d2018-09-14 15:41:11 -0700500 GoIdent GoIdent // name of the generated Go type
501 Fields []*Field // message field declarations
502 Oneofs []*Oneof // oneof declarations
503 Messages []*Message // nested message declarations
504 Enums []*Enum // nested enum declarations
505 Extensions []*Extension // nested extension declarations
Damien Neil162c1272018-10-04 12:42:37 -0700506 Location Location // location of this message
Damien Neilc7d07d92018-08-22 13:46:02 -0700507}
508
Damien Neil1fa78d82018-09-13 13:12:36 -0700509func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) *Message {
Damien Neil162c1272018-10-04 12:42:37 -0700510 var loc Location
Damien Neilcab8dfe2018-09-06 14:51:28 -0700511 if parent != nil {
Damien Neil162c1272018-10-04 12:42:37 -0700512 loc = parent.Location.appendPath(messageMessageField, int32(desc.Index()))
Damien Neilcab8dfe2018-09-06 14:51:28 -0700513 } else {
Damien Neil162c1272018-10-04 12:42:37 -0700514 loc = f.location(fileMessageField, int32(desc.Index()))
Damien Neilcab8dfe2018-09-06 14:51:28 -0700515 }
Damien Neil46abb572018-09-07 12:45:37 -0700516 message := &Message{
Damien Neil162c1272018-10-04 12:42:37 -0700517 Desc: desc,
518 GoIdent: newGoIdent(f, desc),
519 Location: loc,
Damien Neilc7d07d92018-08-22 13:46:02 -0700520 }
Damien Neil658051b2018-09-10 12:26:21 -0700521 gen.messagesByName[desc.FullName()] = message
Damien Neilabc6fc12018-08-23 14:39:30 -0700522 for i, mdescs := 0, desc.Messages(); i < mdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700523 message.Messages = append(message.Messages, newMessage(gen, f, message, mdescs.Get(i)))
Damien Neilc7d07d92018-08-22 13:46:02 -0700524 }
Damien Neil46abb572018-09-07 12:45:37 -0700525 for i, edescs := 0, desc.Enums(); i < edescs.Len(); i++ {
526 message.Enums = append(message.Enums, newEnum(gen, f, message, edescs.Get(i)))
527 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700528 for i, odescs := 0, desc.Oneofs(); i < odescs.Len(); i++ {
529 message.Oneofs = append(message.Oneofs, newOneof(gen, f, message, odescs.Get(i)))
530 }
Damien Neil658051b2018-09-10 12:26:21 -0700531 for i, fdescs := 0, desc.Fields(); i < fdescs.Len(); i++ {
Damien Neil1fa78d82018-09-13 13:12:36 -0700532 message.Fields = append(message.Fields, newField(gen, f, message, fdescs.Get(i)))
Damien Neil658051b2018-09-10 12:26:21 -0700533 }
Damien Neil993c04d2018-09-14 15:41:11 -0700534 for i, extdescs := 0, desc.Extensions(); i < extdescs.Len(); i++ {
535 message.Extensions = append(message.Extensions, newField(gen, f, message, extdescs.Get(i)))
536 }
Damien Neil658051b2018-09-10 12:26:21 -0700537
538 // Field name conflict resolution.
539 //
540 // We assume well-known method names that may be attached to a generated
541 // message type, as well as a 'Get*' method for each field. For each
542 // field in turn, we add _s to its name until there are no conflicts.
543 //
544 // Any change to the following set of method names is a potential
545 // incompatible API change because it may change generated field names.
546 //
547 // TODO: If we ever support a 'go_name' option to set the Go name of a
548 // field, we should consider dropping this entirely. The conflict
549 // resolution algorithm is subtle and surprising (changing the order
550 // in which fields appear in the .proto source file can change the
551 // names of fields in generated code), and does not adapt well to
552 // adding new per-field methods such as setters.
553 usedNames := map[string]bool{
554 "Reset": true,
555 "String": true,
556 "ProtoMessage": true,
557 "Marshal": true,
558 "Unmarshal": true,
559 "ExtensionRangeArray": true,
560 "ExtensionMap": true,
561 "Descriptor": true,
562 }
563 makeNameUnique := func(name string) string {
564 for usedNames[name] || usedNames["Get"+name] {
565 name += "_"
566 }
567 usedNames[name] = true
568 usedNames["Get"+name] = true
569 return name
570 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700571 seenOneofs := make(map[int]bool)
Damien Neil658051b2018-09-10 12:26:21 -0700572 for _, field := range message.Fields {
Damien Neil1fa78d82018-09-13 13:12:36 -0700573 field.GoName = makeNameUnique(field.GoName)
574 if field.OneofType != nil {
575 if !seenOneofs[field.OneofType.Desc.Index()] {
576 // If this is a field in a oneof that we haven't seen before,
577 // make the name for that oneof unique as well.
578 field.OneofType.GoName = makeNameUnique(field.OneofType.GoName)
579 seenOneofs[field.OneofType.Desc.Index()] = true
580 }
581 }
Damien Neil658051b2018-09-10 12:26:21 -0700582 }
583
Damien Neil1fa78d82018-09-13 13:12:36 -0700584 return message
Damien Neil658051b2018-09-10 12:26:21 -0700585}
586
Damien Neil0bd5a382018-09-13 15:07:10 -0700587func (message *Message) init(gen *Plugin) error {
588 for _, child := range message.Messages {
589 if err := child.init(gen); err != nil {
590 return err
591 }
592 }
593 for _, field := range message.Fields {
594 if err := field.init(gen); err != nil {
595 return err
596 }
597 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700598 for _, oneof := range message.Oneofs {
599 oneof.init(gen, message)
600 }
Damien Neil993c04d2018-09-14 15:41:11 -0700601 for _, extension := range message.Extensions {
602 if err := extension.init(gen); err != nil {
603 return err
604 }
605 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700606 return nil
607}
608
Damien Neil658051b2018-09-10 12:26:21 -0700609// A Field describes a message field.
610type Field struct {
611 Desc protoreflect.FieldDescriptor
612
Damien Neil1fa78d82018-09-13 13:12:36 -0700613 // GoName is the base name of this field's Go field and methods.
Damien Neil658051b2018-09-10 12:26:21 -0700614 // For code generated by protoc-gen-go, this means a field named
Damien Neil1fa78d82018-09-13 13:12:36 -0700615 // '{{GoName}}' and a getter method named 'Get{{GoName}}'.
616 GoName string
Damien Neil658051b2018-09-10 12:26:21 -0700617
Damien Neil993c04d2018-09-14 15:41:11 -0700618 ParentMessage *Message // message in which this field is defined; nil if top-level extension
619 ExtendedType *Message // extended message for extension fields; nil otherwise
620 MessageType *Message // type for message or group fields; nil otherwise
621 EnumType *Enum // type for enum fields; nil otherwise
622 OneofType *Oneof // containing oneof; nil if not part of a oneof
Damien Neil162c1272018-10-04 12:42:37 -0700623 Location Location // location of this field
Damien Neil658051b2018-09-10 12:26:21 -0700624}
625
Damien Neil1fa78d82018-09-13 13:12:36 -0700626func newField(gen *Plugin, f *File, message *Message, desc protoreflect.FieldDescriptor) *Field {
Damien Neil162c1272018-10-04 12:42:37 -0700627 var loc Location
Damien Neil993c04d2018-09-14 15:41:11 -0700628 switch {
629 case desc.ExtendedType() != nil && message == nil:
Damien Neil162c1272018-10-04 12:42:37 -0700630 loc = f.location(fileExtensionField, int32(desc.Index()))
Damien Neil993c04d2018-09-14 15:41:11 -0700631 case desc.ExtendedType() != nil && message != nil:
Damien Neil162c1272018-10-04 12:42:37 -0700632 loc = message.Location.appendPath(messageExtensionField, int32(desc.Index()))
Damien Neil993c04d2018-09-14 15:41:11 -0700633 default:
Damien Neil162c1272018-10-04 12:42:37 -0700634 loc = message.Location.appendPath(messageFieldField, int32(desc.Index()))
Damien Neil993c04d2018-09-14 15:41:11 -0700635 }
Damien Neil658051b2018-09-10 12:26:21 -0700636 field := &Field{
Damien Neil993c04d2018-09-14 15:41:11 -0700637 Desc: desc,
638 GoName: camelCase(string(desc.Name())),
639 ParentMessage: message,
Damien Neil162c1272018-10-04 12:42:37 -0700640 Location: loc,
Damien Neil658051b2018-09-10 12:26:21 -0700641 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700642 if desc.OneofType() != nil {
643 field.OneofType = message.Oneofs[desc.OneofType().Index()]
644 }
645 return field
Damien Neil0bd5a382018-09-13 15:07:10 -0700646}
647
Damien Neil993c04d2018-09-14 15:41:11 -0700648// Extension is an alias of Field for documentation.
649type Extension = Field
650
Damien Neil0bd5a382018-09-13 15:07:10 -0700651func (field *Field) init(gen *Plugin) error {
652 desc := field.Desc
Damien Neil658051b2018-09-10 12:26:21 -0700653 switch desc.Kind() {
654 case protoreflect.MessageKind, protoreflect.GroupKind:
655 mname := desc.MessageType().FullName()
656 message, ok := gen.messagesByName[mname]
657 if !ok {
Damien Neil0bd5a382018-09-13 15:07:10 -0700658 return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), mname)
Damien Neil658051b2018-09-10 12:26:21 -0700659 }
660 field.MessageType = message
661 case protoreflect.EnumKind:
Damien Neil0bd5a382018-09-13 15:07:10 -0700662 ename := field.Desc.EnumType().FullName()
Damien Neil658051b2018-09-10 12:26:21 -0700663 enum, ok := gen.enumsByName[ename]
664 if !ok {
Damien Neil0bd5a382018-09-13 15:07:10 -0700665 return fmt.Errorf("field %v: no descriptor for enum %v", desc.FullName(), ename)
Damien Neil658051b2018-09-10 12:26:21 -0700666 }
667 field.EnumType = enum
668 }
Damien Neil993c04d2018-09-14 15:41:11 -0700669 if desc.ExtendedType() != nil {
670 mname := desc.ExtendedType().FullName()
671 message, ok := gen.messagesByName[mname]
672 if !ok {
673 return fmt.Errorf("field %v: no descriptor for type %v", desc.FullName(), mname)
674 }
675 field.ExtendedType = message
676 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700677 return nil
Damien Neil46abb572018-09-07 12:45:37 -0700678}
679
Damien Neil1fa78d82018-09-13 13:12:36 -0700680// A Oneof describes a oneof field.
681type Oneof struct {
682 Desc protoreflect.OneofDescriptor
683
Damien Neil993c04d2018-09-14 15:41:11 -0700684 GoName string // Go field name of this oneof
685 ParentMessage *Message // message in which this oneof occurs
686 Fields []*Field // fields that are part of this oneof
Damien Neil162c1272018-10-04 12:42:37 -0700687 Location Location // location of this oneof
Damien Neil1fa78d82018-09-13 13:12:36 -0700688}
689
690func newOneof(gen *Plugin, f *File, message *Message, desc protoreflect.OneofDescriptor) *Oneof {
691 return &Oneof{
Damien Neil993c04d2018-09-14 15:41:11 -0700692 Desc: desc,
693 ParentMessage: message,
694 GoName: camelCase(string(desc.Name())),
Damien Neil162c1272018-10-04 12:42:37 -0700695 Location: message.Location.appendPath(messageOneofField, int32(desc.Index())),
Damien Neil1fa78d82018-09-13 13:12:36 -0700696 }
697}
698
699func (oneof *Oneof) init(gen *Plugin, parent *Message) {
700 for i, fdescs := 0, oneof.Desc.Fields(); i < fdescs.Len(); i++ {
701 oneof.Fields = append(oneof.Fields, parent.Fields[fdescs.Get(i).Index()])
702 }
703}
704
Damien Neil46abb572018-09-07 12:45:37 -0700705// An Enum describes an enum.
706type Enum struct {
707 Desc protoreflect.EnumDescriptor
708
Damien Neil162c1272018-10-04 12:42:37 -0700709 GoIdent GoIdent // name of the generated Go type
710 Values []*EnumValue // enum values
711 Location Location // location of this enum
Damien Neil46abb572018-09-07 12:45:37 -0700712}
713
714func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum {
Damien Neil162c1272018-10-04 12:42:37 -0700715 var loc Location
Damien Neil46abb572018-09-07 12:45:37 -0700716 if parent != nil {
Damien Neil162c1272018-10-04 12:42:37 -0700717 loc = parent.Location.appendPath(messageEnumField, int32(desc.Index()))
Damien Neil46abb572018-09-07 12:45:37 -0700718 } else {
Damien Neil162c1272018-10-04 12:42:37 -0700719 loc = f.location(fileEnumField, int32(desc.Index()))
Damien Neil46abb572018-09-07 12:45:37 -0700720 }
721 enum := &Enum{
Damien Neil162c1272018-10-04 12:42:37 -0700722 Desc: desc,
723 GoIdent: newGoIdent(f, desc),
724 Location: loc,
Damien Neil46abb572018-09-07 12:45:37 -0700725 }
Damien Neil658051b2018-09-10 12:26:21 -0700726 gen.enumsByName[desc.FullName()] = enum
Damien Neil46abb572018-09-07 12:45:37 -0700727 for i, evdescs := 0, enum.Desc.Values(); i < evdescs.Len(); i++ {
728 enum.Values = append(enum.Values, newEnumValue(gen, f, parent, enum, evdescs.Get(i)))
729 }
730 return enum
731}
732
733// An EnumValue describes an enum value.
734type EnumValue struct {
735 Desc protoreflect.EnumValueDescriptor
736
Damien Neil162c1272018-10-04 12:42:37 -0700737 GoIdent GoIdent // name of the generated Go type
738 Location Location // location of this enum value
Damien Neil46abb572018-09-07 12:45:37 -0700739}
740
741func newEnumValue(gen *Plugin, f *File, message *Message, enum *Enum, desc protoreflect.EnumValueDescriptor) *EnumValue {
742 // A top-level enum value's name is: EnumName_ValueName
743 // An enum value contained in a message is: MessageName_ValueName
744 //
745 // Enum value names are not camelcased.
746 parentIdent := enum.GoIdent
747 if message != nil {
748 parentIdent = message.GoIdent
749 }
750 name := parentIdent.GoName + "_" + string(desc.Name())
751 return &EnumValue{
Joe Tsaic1c17aa2018-11-16 11:14:14 -0800752 Desc: desc,
753 GoIdent: f.GoImportPath.Ident(name),
Damien Neil162c1272018-10-04 12:42:37 -0700754 Location: enum.Location.appendPath(enumValueField, int32(desc.Index())),
Damien Neil46abb572018-09-07 12:45:37 -0700755 }
Damien Neil220c2022018-08-15 11:24:18 -0700756}
757
758// A GeneratedFile is a generated file.
759type GeneratedFile struct {
Damien Neil1fa8ab02018-09-27 15:51:05 -0700760 gen *Plugin
Damien Neild9016772018-08-23 14:39:30 -0700761 filename string
762 goImportPath GoImportPath
763 buf bytes.Buffer
764 packageNames map[GoImportPath]GoPackageName
765 usedPackageNames map[GoPackageName]bool
Damien Neil2e0c3da2018-09-19 12:51:36 -0700766 manualImports map[GoImportPath]bool
Damien Neil162c1272018-10-04 12:42:37 -0700767 annotations map[string][]Location
Damien Neil220c2022018-08-15 11:24:18 -0700768}
769
Damien Neild9016772018-08-23 14:39:30 -0700770// NewGeneratedFile creates a new generated file with the given filename
771// and import path.
772func (gen *Plugin) NewGeneratedFile(filename string, goImportPath GoImportPath) *GeneratedFile {
Damien Neil220c2022018-08-15 11:24:18 -0700773 g := &GeneratedFile{
Damien Neil1fa8ab02018-09-27 15:51:05 -0700774 gen: gen,
Damien Neild9016772018-08-23 14:39:30 -0700775 filename: filename,
776 goImportPath: goImportPath,
777 packageNames: make(map[GoImportPath]GoPackageName),
778 usedPackageNames: make(map[GoPackageName]bool),
Damien Neil2e0c3da2018-09-19 12:51:36 -0700779 manualImports: make(map[GoImportPath]bool),
Damien Neil162c1272018-10-04 12:42:37 -0700780 annotations: make(map[string][]Location),
Damien Neil220c2022018-08-15 11:24:18 -0700781 }
782 gen.genFiles = append(gen.genFiles, g)
783 return g
784}
785
Damien Neil2dc67182018-09-21 15:03:34 -0700786// A Service describes a service.
787type Service struct {
788 Desc protoreflect.ServiceDescriptor
789
Damien Neil162c1272018-10-04 12:42:37 -0700790 GoName string
791 Location Location // location of this service
792 Methods []*Method // service method definitions
Damien Neil2dc67182018-09-21 15:03:34 -0700793}
794
795func newService(gen *Plugin, f *File, desc protoreflect.ServiceDescriptor) *Service {
796 service := &Service{
Damien Neil162c1272018-10-04 12:42:37 -0700797 Desc: desc,
798 GoName: camelCase(string(desc.Name())),
799 Location: f.location(fileServiceField, int32(desc.Index())),
Damien Neil2dc67182018-09-21 15:03:34 -0700800 }
801 for i, mdescs := 0, desc.Methods(); i < mdescs.Len(); i++ {
802 service.Methods = append(service.Methods, newMethod(gen, f, service, mdescs.Get(i)))
803 }
804 return service
805}
806
807// A Method describes a method in a service.
808type Method struct {
809 Desc protoreflect.MethodDescriptor
810
811 GoName string
812 ParentService *Service
Damien Neil162c1272018-10-04 12:42:37 -0700813 Location Location // location of this method
Damien Neil2dc67182018-09-21 15:03:34 -0700814 InputType *Message
815 OutputType *Message
816}
817
818func newMethod(gen *Plugin, f *File, service *Service, desc protoreflect.MethodDescriptor) *Method {
819 method := &Method{
820 Desc: desc,
821 GoName: camelCase(string(desc.Name())),
822 ParentService: service,
Damien Neil162c1272018-10-04 12:42:37 -0700823 Location: service.Location.appendPath(serviceMethodField, int32(desc.Index())),
Damien Neil2dc67182018-09-21 15:03:34 -0700824 }
825 return method
826}
827
828func (method *Method) init(gen *Plugin) error {
829 desc := method.Desc
830
831 inName := desc.InputType().FullName()
832 in, ok := gen.messagesByName[inName]
833 if !ok {
834 return fmt.Errorf("method %v: no descriptor for type %v", desc.FullName(), inName)
835 }
836 method.InputType = in
837
838 outName := desc.OutputType().FullName()
839 out, ok := gen.messagesByName[outName]
840 if !ok {
841 return fmt.Errorf("method %v: no descriptor for type %v", desc.FullName(), outName)
842 }
843 method.OutputType = out
844
845 return nil
846}
847
Damien Neil220c2022018-08-15 11:24:18 -0700848// P prints a line to the generated output. It converts each parameter to a
849// string following the same rules as fmt.Print. It never inserts spaces
850// between parameters.
Damien Neil220c2022018-08-15 11:24:18 -0700851func (g *GeneratedFile) P(v ...interface{}) {
852 for _, x := range v {
Damien Neild9016772018-08-23 14:39:30 -0700853 switch x := x.(type) {
854 case GoIdent:
Damien Neil46abb572018-09-07 12:45:37 -0700855 fmt.Fprint(&g.buf, g.QualifiedGoIdent(x))
Damien Neild9016772018-08-23 14:39:30 -0700856 default:
857 fmt.Fprint(&g.buf, x)
858 }
Damien Neil220c2022018-08-15 11:24:18 -0700859 }
860 fmt.Fprintln(&g.buf)
861}
862
Damien Neilba1159f2018-10-17 12:53:18 -0700863// PrintLeadingComments writes the comment appearing before a location in
864// the .proto source to the generated file.
865//
866// It returns true if a comment was present at the location.
867func (g *GeneratedFile) PrintLeadingComments(loc Location) (hasComment bool) {
868 f := g.gen.filesByName[loc.SourceFile]
869 if f == nil {
870 return false
871 }
872 for _, infoLoc := range f.sourceInfo[newPathKey(loc.Path)] {
873 if infoLoc.LeadingComments == nil {
874 continue
875 }
876 for _, line := range strings.Split(strings.TrimSuffix(infoLoc.GetLeadingComments(), "\n"), "\n") {
877 g.buf.WriteString("//")
878 g.buf.WriteString(line)
879 g.buf.WriteString("\n")
880 }
881 return true
882 }
883 return false
884}
885
Damien Neil46abb572018-09-07 12:45:37 -0700886// QualifiedGoIdent returns the string to use for a Go identifier.
887//
888// If the identifier is from a different Go package than the generated file,
889// the returned name will be qualified (package.name) and an import statement
890// for the identifier's package will be included in the file.
891func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string {
892 if ident.GoImportPath == g.goImportPath {
893 return ident.GoName
894 }
895 if packageName, ok := g.packageNames[ident.GoImportPath]; ok {
896 return string(packageName) + "." + ident.GoName
897 }
898 packageName := cleanPackageName(baseName(string(ident.GoImportPath)))
Damien Neil87214662018-10-05 11:23:35 -0700899 for i, orig := 1, packageName; g.usedPackageNames[packageName] || isGoPredeclaredIdentifier[string(packageName)]; i++ {
Damien Neil46abb572018-09-07 12:45:37 -0700900 packageName = orig + GoPackageName(strconv.Itoa(i))
901 }
902 g.packageNames[ident.GoImportPath] = packageName
903 g.usedPackageNames[packageName] = true
904 return string(packageName) + "." + ident.GoName
905}
906
Damien Neil2e0c3da2018-09-19 12:51:36 -0700907// Import ensures a package is imported by the generated file.
908//
909// Packages referenced by QualifiedGoIdent are automatically imported.
910// Explicitly importing a package with Import is generally only necessary
911// when the import will be blank (import _ "package").
912func (g *GeneratedFile) Import(importPath GoImportPath) {
913 g.manualImports[importPath] = true
914}
915
Damien Neil220c2022018-08-15 11:24:18 -0700916// Write implements io.Writer.
917func (g *GeneratedFile) Write(p []byte) (n int, err error) {
918 return g.buf.Write(p)
919}
920
Damien Neil162c1272018-10-04 12:42:37 -0700921// Annotate associates a symbol in a generated Go file with a location in a
922// source .proto file.
923//
924// The symbol may refer to a type, constant, variable, function, method, or
925// struct field. The "T.sel" syntax is used to identify the method or field
926// 'sel' on type 'T'.
927func (g *GeneratedFile) Annotate(symbol string, loc Location) {
928 g.annotations[symbol] = append(g.annotations[symbol], loc)
929}
930
931// content returns the contents of the generated file.
932func (g *GeneratedFile) content() ([]byte, error) {
Damien Neild9016772018-08-23 14:39:30 -0700933 if !strings.HasSuffix(g.filename, ".go") {
Damien Neilc7d07d92018-08-22 13:46:02 -0700934 return g.buf.Bytes(), nil
935 }
936
937 // Reformat generated code.
938 original := g.buf.Bytes()
939 fset := token.NewFileSet()
Damien Neil1ec33152018-09-13 13:12:36 -0700940 file, err := parser.ParseFile(fset, "", original, parser.ParseComments)
Damien Neilc7d07d92018-08-22 13:46:02 -0700941 if err != nil {
942 // Print out the bad code with line numbers.
943 // This should never happen in practice, but it can while changing generated code
944 // so consider this a debugging aid.
945 var src bytes.Buffer
946 s := bufio.NewScanner(bytes.NewReader(original))
947 for line := 1; s.Scan(); line++ {
948 fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
949 }
Damien Neild9016772018-08-23 14:39:30 -0700950 return nil, fmt.Errorf("%v: unparsable Go source: %v\n%v", g.filename, err, src.String())
Damien Neilc7d07d92018-08-22 13:46:02 -0700951 }
Damien Neild9016772018-08-23 14:39:30 -0700952
953 // Add imports.
954 var importPaths []string
955 for importPath := range g.packageNames {
956 importPaths = append(importPaths, string(importPath))
957 }
958 sort.Strings(importPaths)
Damien Neil1fa8ab02018-09-27 15:51:05 -0700959 rewriteImport := func(importPath string) string {
960 if f := g.gen.opts.ImportRewriteFunc; f != nil {
961 return string(f(GoImportPath(importPath)))
962 }
963 return importPath
964 }
Damien Neild9016772018-08-23 14:39:30 -0700965 for _, importPath := range importPaths {
Damien Neil1fa8ab02018-09-27 15:51:05 -0700966 astutil.AddNamedImport(fset, file, string(g.packageNames[GoImportPath(importPath)]), rewriteImport(importPath))
Damien Neild9016772018-08-23 14:39:30 -0700967 }
Damien Neil2e0c3da2018-09-19 12:51:36 -0700968 for importPath := range g.manualImports {
969 if _, ok := g.packageNames[importPath]; ok {
970 continue
971 }
Damien Neil329b75e2018-10-04 17:31:07 -0700972 astutil.AddNamedImport(fset, file, "_", rewriteImport(string(importPath)))
Damien Neil2e0c3da2018-09-19 12:51:36 -0700973 }
Damien Neil1ec33152018-09-13 13:12:36 -0700974 ast.SortImports(fset, file)
Damien Neild9016772018-08-23 14:39:30 -0700975
Damien Neilc7d07d92018-08-22 13:46:02 -0700976 var out bytes.Buffer
Damien Neil1ec33152018-09-13 13:12:36 -0700977 if err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(&out, fset, file); err != nil {
Damien Neild9016772018-08-23 14:39:30 -0700978 return nil, fmt.Errorf("%v: can not reformat Go source: %v", g.filename, err)
Damien Neilc7d07d92018-08-22 13:46:02 -0700979 }
Damien Neilc7d07d92018-08-22 13:46:02 -0700980 return out.Bytes(), nil
Damien Neil162c1272018-10-04 12:42:37 -0700981}
Damien Neilc7d07d92018-08-22 13:46:02 -0700982
Damien Neil162c1272018-10-04 12:42:37 -0700983// metaFile returns the contents of the file's metadata file, which is a
984// text formatted string of the google.protobuf.GeneratedCodeInfo.
985func (g *GeneratedFile) metaFile(content []byte) (string, error) {
986 fset := token.NewFileSet()
987 astFile, err := parser.ParseFile(fset, "", content, 0)
988 if err != nil {
989 return "", err
990 }
991 info := &descpb.GeneratedCodeInfo{}
992
993 seenAnnotations := make(map[string]bool)
994 annotate := func(s string, ident *ast.Ident) {
995 seenAnnotations[s] = true
996 for _, loc := range g.annotations[s] {
997 info.Annotation = append(info.Annotation, &descpb.GeneratedCodeInfo_Annotation{
Joe Tsai009e0672018-11-27 18:45:07 -0800998 SourceFile: scalar.String(loc.SourceFile),
Damien Neil162c1272018-10-04 12:42:37 -0700999 Path: loc.Path,
Joe Tsai009e0672018-11-27 18:45:07 -08001000 Begin: scalar.Int32(int32(fset.Position(ident.Pos()).Offset)),
1001 End: scalar.Int32(int32(fset.Position(ident.End()).Offset)),
Damien Neil162c1272018-10-04 12:42:37 -07001002 })
1003 }
1004 }
1005 for _, decl := range astFile.Decls {
1006 switch decl := decl.(type) {
1007 case *ast.GenDecl:
1008 for _, spec := range decl.Specs {
1009 switch spec := spec.(type) {
1010 case *ast.TypeSpec:
1011 annotate(spec.Name.Name, spec.Name)
1012 if st, ok := spec.Type.(*ast.StructType); ok {
1013 for _, field := range st.Fields.List {
1014 for _, name := range field.Names {
1015 annotate(spec.Name.Name+"."+name.Name, name)
1016 }
1017 }
1018 }
1019 case *ast.ValueSpec:
1020 for _, name := range spec.Names {
1021 annotate(name.Name, name)
1022 }
1023 }
1024 }
1025 case *ast.FuncDecl:
1026 if decl.Recv == nil {
1027 annotate(decl.Name.Name, decl.Name)
1028 } else {
1029 recv := decl.Recv.List[0].Type
1030 if s, ok := recv.(*ast.StarExpr); ok {
1031 recv = s.X
1032 }
1033 if id, ok := recv.(*ast.Ident); ok {
1034 annotate(id.Name+"."+decl.Name.Name, decl.Name)
1035 }
1036 }
1037 }
1038 }
1039 for a := range g.annotations {
1040 if !seenAnnotations[a] {
1041 return "", fmt.Errorf("%v: no symbol matching annotation %q", g.filename, a)
1042 }
1043 }
1044
1045 return proto.CompactTextString(info), nil
Damien Neil220c2022018-08-15 11:24:18 -07001046}
Damien Neil082ce922018-09-06 10:23:53 -07001047
1048type pathType int
1049
1050const (
1051 pathTypeImport pathType = iota
1052 pathTypeSourceRelative
1053)
Damien Neilcab8dfe2018-09-06 14:51:28 -07001054
1055// The SourceCodeInfo message describes the location of elements of a parsed
1056// .proto file by way of a "path", which is a sequence of integers that
1057// describe the route from a FileDescriptorProto to the relevant submessage.
1058// The path alternates between a field number of a repeated field, and an index
1059// into that repeated field. The constants below define the field numbers that
1060// are used.
1061//
1062// See descriptor.proto for more information about this.
1063const (
1064 // field numbers in FileDescriptorProto
Damien Neil993c04d2018-09-14 15:41:11 -07001065 filePackageField = 2 // package
1066 fileMessageField = 4 // message_type
1067 fileEnumField = 5 // enum_type
Damien Neil2dc67182018-09-21 15:03:34 -07001068 fileServiceField = 6 // service
Damien Neil993c04d2018-09-14 15:41:11 -07001069 fileExtensionField = 7 // extension
Damien Neilcab8dfe2018-09-06 14:51:28 -07001070 // field numbers in DescriptorProto
Damien Neil993c04d2018-09-14 15:41:11 -07001071 messageFieldField = 2 // field
1072 messageMessageField = 3 // nested_type
1073 messageEnumField = 4 // enum_type
1074 messageExtensionField = 6 // extension
1075 messageOneofField = 8 // oneof_decl
Damien Neilcab8dfe2018-09-06 14:51:28 -07001076 // field numbers in EnumDescriptorProto
1077 enumValueField = 2 // value
Damien Neil2dc67182018-09-21 15:03:34 -07001078 // field numbers in ServiceDescriptorProto
1079 serviceMethodField = 2 // method
1080 serviceStreamField = 4 // stream
Damien Neilcab8dfe2018-09-06 14:51:28 -07001081)
1082
Damien Neil162c1272018-10-04 12:42:37 -07001083// A Location is a location in a .proto source file.
1084//
1085// See the google.protobuf.SourceCodeInfo documentation in descriptor.proto
1086// for details.
1087type Location struct {
1088 SourceFile string
1089 Path []int32
1090}
1091
1092// appendPath add elements to a Location's path, returning a new Location.
1093func (loc Location) appendPath(a ...int32) Location {
Damien Neilcab8dfe2018-09-06 14:51:28 -07001094 var n []int32
Damien Neil162c1272018-10-04 12:42:37 -07001095 n = append(n, loc.Path...)
Damien Neilcab8dfe2018-09-06 14:51:28 -07001096 n = append(n, a...)
Damien Neil162c1272018-10-04 12:42:37 -07001097 return Location{
1098 SourceFile: loc.SourceFile,
1099 Path: n,
1100 }
Damien Neilcab8dfe2018-09-06 14:51:28 -07001101}
Damien Neilba1159f2018-10-17 12:53:18 -07001102
1103// A pathKey is a representation of a location path suitable for use as a map key.
1104type pathKey struct {
1105 s string
1106}
1107
1108// newPathKey converts a location path to a pathKey.
1109func newPathKey(path []int32) pathKey {
1110 buf := make([]byte, 4*len(path))
1111 for i, x := range path {
1112 binary.LittleEndian.PutUint32(buf[i*4:], uint32(x))
1113 }
1114 return pathKey{string(buf)}
1115}