encoding/protobuf: allow specifying package

Change-Id: I27642c3c47ff5d3131a7cb3f8b4f397ce1c59c87
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5185
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go
index c5c8c14..fa35d06 100644
--- a/encoding/protobuf/parse.go
+++ b/encoding/protobuf/parse.go
@@ -210,6 +210,9 @@
 }
 
 func (p *protoConverter) shortName() string {
+	if p.state.pkgName != "" {
+		return p.state.pkgName
+	}
 	if p.shortPkgName == "" && p.protoPkg != "" {
 		split := strings.Split(p.protoPkg, ".")
 		p.shortPkgName = split[len(split)-1]
diff --git a/encoding/protobuf/protobuf.go b/encoding/protobuf/protobuf.go
index aa5189e..5821c66 100644
--- a/encoding/protobuf/protobuf.go
+++ b/encoding/protobuf/protobuf.go
@@ -114,11 +114,16 @@
 	Root string
 
 	// Module is the Go package import path of the module root. It is the value
-	// as after "module" in a go.mod file, if a module file is present.
+	// as after "module" in a cue.mod/modules.cue file, if a module file is
+	// present.
 	Module string // TODO: determine automatically if unspecified.
 
 	// Paths defines the include directory in which to search for imports.
 	Paths []string
+
+	// PkgName specifies the package name for a generated CUE file. A value
+	// will be derived from the Go package name if undefined.
+	PkgName string
 }
 
 // An Extractor converts a collection of proto files, typically belonging to one
@@ -132,10 +137,11 @@
 // according to their Go package import path.
 //
 type Extractor struct {
-	root   string
-	cwd    string
-	module string
-	paths  []string
+	root    string
+	cwd     string
+	module  string
+	paths   []string
+	pkgName string
 
 	fileCache map[string]result
 	imports   map[string]*build.Instance
@@ -158,6 +164,7 @@
 		root:      c.Root,
 		cwd:       cwd,
 		paths:     c.Paths,
+		pkgName:   c.PkgName,
 		module:    c.Module,
 		fileCache: map[string]result{},
 		imports:   map[string]*build.Instance{},