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>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index cc1991c..2a200a6 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -234,7 +234,7 @@
 }
 
 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
 	}
diff --git a/cmd/protoc-gen-go/internal_gengo/reflect.go b/cmd/protoc-gen-go/internal_gengo/reflect.go
index c453e73..63bbbd0 100644
--- a/cmd/protoc-gen-go/internal_gengo/reflect.go
+++ b/cmd/protoc-gen-go/internal_gengo/reflect.go
@@ -153,7 +153,7 @@
 	// 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
 		}
diff --git a/compiler/protogen/protogen.go b/compiler/protogen/protogen.go
index 0f05e22..32e0e43 100644
--- a/compiler/protogen/protogen.go
+++ b/compiler/protogen/protogen.go
@@ -100,7 +100,7 @@
 	// 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 @@
 	}
 	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 @@
 
 	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 @@
 			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 @@
 	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
diff --git a/compiler/protogen/protogen_test.go b/compiler/protogen/protogen_test.go
index 4604ed1..51dbd25 100644
--- a/compiler/protogen/protogen_test.go
+++ b/compiler/protogen/protogen_test.go
@@ -186,7 +186,7 @@
 			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 @@
 	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)