compiler/protogen: export Plugin.FilesByPath

The Plugin.FileByName method seems like an unnecessary indirection that
provides little benefit over just exposing the map.
If the intention to provide some form of immutability, there are already
many leakages in the abstraction given that most things returned by
protogen is by pointer.

Change-Id: I0c793a8d7dab7632b92968a74f8c1ba5accb242f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191039
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2019-08-20 20:14:19 -07:00
parent 7762ec2098
commit 2cec484ed7
4 changed files with 9 additions and 15 deletions

View File

@ -234,7 +234,7 @@ func genGeneratedHeader(gen *protogen.Plugin, g *protogen.GeneratedFile, f *file
}
func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp protoreflect.FileImport) {
impFile, ok := gen.FileByName(imp.Path())
impFile, ok := gen.FilesByPath[imp.Path()]
if !ok {
return
}

View File

@ -153,7 +153,7 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f
// package run in the correct order: Call the init funcs for every .proto file
// imported by this one that is in the same Go package.
for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {
impFile, _ := gen.FileByName(imps.Get(i).Path())
impFile := gen.FilesByPath[imps.Get(i).Path()]
if impFile.GoImportPath != f.GoImportPath {
continue
}

View File

@ -100,7 +100,7 @@ type Plugin struct {
// Files appear in topological order, so each file appears before any
// file that imports it.
Files []*File
filesByName map[string]*File
FilesByPath map[string]*File
fileReg *protoregistry.Files
enumsByName map[protoreflect.FullName]*Enum
@ -154,7 +154,7 @@ func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
}
gen := &Plugin{
Request: req,
filesByName: make(map[string]*File),
FilesByPath: make(map[string]*File),
fileReg: protoregistry.NewFiles(),
enumsByName: make(map[protoreflect.FullName]*Enum),
messagesByName: make(map[protoreflect.FullName]*Message),
@ -307,7 +307,7 @@ func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
for _, fdesc := range gen.Request.ProtoFile {
filename := fdesc.GetName()
if gen.filesByName[filename] != nil {
if gen.FilesByPath[filename] != nil {
return nil, fmt.Errorf("duplicate file name: %q", filename)
}
f, err := newFile(gen, fdesc, packageNames[filename], importPaths[filename])
@ -315,10 +315,10 @@ func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
return nil, err
}
gen.Files = append(gen.Files, f)
gen.filesByName[filename] = f
gen.FilesByPath[filename] = f
}
for _, filename := range gen.Request.FileToGenerate {
f, ok := gen.FileByName(filename)
f, ok := gen.FilesByPath[filename]
if !ok {
return nil, fmt.Errorf("no descriptor for generated file: %v", filename)
}
@ -372,12 +372,6 @@ func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse {
return resp
}
// FileByName returns the file with the given name.
func (gen *Plugin) FileByName(name string) (f *File, ok bool) {
f, ok = gen.filesByName[name]
return f, ok
}
// A File describes a .proto source file.
type File struct {
Desc protoreflect.FileDescriptor

View File

@ -186,7 +186,7 @@ TEST: %v
t.Errorf("%vNew(req) = %v", context, err)
continue
}
gotFile, ok := gen.FileByName(filename)
gotFile, ok := gen.FilesByPath[filename]
if !ok {
t.Errorf("%v%v: missing file info", context, filename)
continue
@ -223,7 +223,7 @@ func TestPackageNameInference(t *testing.T) {
if err != nil {
t.Fatalf("New(req) = %v", err)
}
if f1, ok := gen.FileByName("dir/file1.proto"); !ok {
if f1, ok := gen.FilesByPath["dir/file1.proto"]; !ok {
t.Errorf("missing file info for dir/file1.proto")
} else if f1.GoPackageName != "foo" {
t.Errorf("dir/file1.proto: GoPackageName=%v, want foo; package name should be derived from dir/file2.proto", f1.GoPackageName)