encoding/protobuf: remove dependency on load

Also fixes openapi test

Change-Id: I4ca1dda9a2f9a32fc9a4220f1c95421e22f5ad4e
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5221
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/load/config.go b/cue/load/config.go
index 2b35577..51b1faf 100644
--- a/cue/load/config.go
+++ b/cue/load/config.go
@@ -25,6 +25,7 @@
 	"cuelang.org/go/cue/build"
 	"cuelang.org/go/cue/errors"
 	"cuelang.org/go/cue/token"
+	"cuelang.org/go/internal"
 )
 
 const (
@@ -113,12 +114,7 @@
 // GenPath reports the directory in which to store generated
 // files.
 func GenPath(root string) string {
-	info, err := os.Stat(filepath.Join(root, modDir))
-	if err == nil && info.IsDir() {
-		// TODO(legacy): support legacy cue.mod file.
-		return filepath.Join(root, modDir, "gen")
-	}
-	return filepath.Join(root, "pkg")
+	return internal.GenPath(root)
 }
 
 // A Config configures load behavior.
diff --git a/encoding/openapi/openapi_test.go b/encoding/openapi/openapi_test.go
index 336fac6..dfc0fc9 100644
--- a/encoding/openapi/openapi_test.go
+++ b/encoding/openapi/openapi_test.go
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package openapi
+package openapi_test
 
 import (
 	"bytes"
@@ -29,21 +29,22 @@
 	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/cue/errors"
 	"cuelang.org/go/cue/load"
+	"cuelang.org/go/encoding/openapi"
 )
 
 var update *bool = flag.Bool("update", false, "update the test output")
 
 func TestParseDefinitions(t *testing.T) {
-	info := *(*OrderedMap)(ast.NewStruct(
+	info := *(*openapi.OrderedMap)(ast.NewStruct(
 		"title", ast.NewString("test"),
 		"version", ast.NewString("v1"),
 	))
-	defaultConfig := &Config{}
-	resolveRefs := &Config{Info: info, ExpandReferences: true}
+	defaultConfig := &openapi.Config{}
+	resolveRefs := &openapi.Config{Info: info, ExpandReferences: true}
 
 	testCases := []struct {
 		in, out string
-		config  *Config
+		config  *openapi.Config
 	}{{
 		"structural.cue",
 		"structural.json",
@@ -55,7 +56,7 @@
 	}, {
 		"simple.cue",
 		"simple-filter.json",
-		&Config{Info: info, FieldFilter: "min.*|max.*"},
+		&openapi.Config{Info: info, FieldFilter: "min.*|max.*"},
 	}, {
 		"array.cue",
 		"array.json",
@@ -95,7 +96,7 @@
 	}, {
 		"oneof.cue",
 		"oneof-funcs.json",
-		&Generator{
+		&openapi.Config{
 			Info: info,
 			ReferenceFunc: func(inst *cue.Instance, path []string) string {
 				return strings.ToUpper(strings.Join(path, "_"))
@@ -107,7 +108,7 @@
 	}, {
 		"refs.cue",
 		"refs.json",
-		&Generator{
+		&openapi.Config{
 			Info: info,
 			ReferenceFunc: func(inst *cue.Instance, path []string) string {
 				switch {
@@ -120,7 +121,7 @@
 	}, {
 		"issue131.cue",
 		"issue131.json",
-		&Generator{Info: info, SelfContained: true},
+		&openapi.Config{Info: info, SelfContained: true},
 	}}
 	for _, tc := range testCases {
 		t.Run(tc.out, func(t *testing.T) {
@@ -133,7 +134,7 @@
 				t.Fatal(errors.Details(inst.Err, nil))
 			}
 
-			b, err := Gen(inst, tc.config)
+			b, err := openapi.Gen(inst, tc.config)
 			if err != nil {
 				t.Fatal(err)
 			}
@@ -170,7 +171,7 @@
 		t.Fatal(err)
 	}
 
-	b, err := Gen(inst, &Config{
+	b, err := openapi.Gen(inst, &openapi.Config{
 		ExpandReferences: true,
 	})
 	if err != nil {
diff --git a/encoding/protobuf/protobuf.go b/encoding/protobuf/protobuf.go
index 5821c66..d176b77 100644
--- a/encoding/protobuf/protobuf.go
+++ b/encoding/protobuf/protobuf.go
@@ -97,9 +97,9 @@
 	"cuelang.org/go/cue/build"
 	"cuelang.org/go/cue/errors"
 	"cuelang.org/go/cue/format"
-	"cuelang.org/go/cue/load"
 	"cuelang.org/go/cue/parser"
 	"cuelang.org/go/cue/token"
+	"cuelang.org/go/internal"
 )
 
 // Config specifies the environment into which to parse a proto definition file.
@@ -323,7 +323,7 @@
 	dir := b.root
 	path := importPath
 	if !strings.HasPrefix(path, b.module) {
-		dir = filepath.Join(load.GenPath(dir), path)
+		dir = filepath.Join(internal.GenPath(dir), path)
 	} else {
 		dir = filepath.Join(dir, path[len(b.module)+1:])
 		want := filepath.Dir(p.file.Filename)
diff --git a/internal/internal.go b/internal/internal.go
index 8c9b33c..2e73e88 100644
--- a/internal/internal.go
+++ b/internal/internal.go
@@ -21,6 +21,8 @@
 
 import (
 	"bufio"
+	"os"
+	"path/filepath"
 	"strings"
 
 	"github.com/cockroachdb/apd/v2"
@@ -161,3 +163,13 @@
 	}
 	return i.Name == "string" || i.Name == "_"
 }
+
+// GenPath reports the directory in which to store generated files.
+func GenPath(root string) string {
+	info, err := os.Stat(filepath.Join(root, "cue.mod"))
+	if err == nil && info.IsDir() {
+		// TODO(legacy): support legacy cue.mod file.
+		return filepath.Join(root, "cue.mod", "gen")
+	}
+	return filepath.Join(root, "pkg")
+}