aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/internal/code/imports.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/internal/code/imports.go')
-rw-r--r--vendor/github.com/99designs/gqlgen/internal/code/imports.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/github.com/99designs/gqlgen/internal/code/imports.go b/vendor/github.com/99designs/gqlgen/internal/code/imports.go
new file mode 100644
index 00000000..2384e87d
--- /dev/null
+++ b/vendor/github.com/99designs/gqlgen/internal/code/imports.go
@@ -0,0 +1,60 @@
+package code
+
+import (
+ "errors"
+ "path/filepath"
+ "sync"
+
+ "golang.org/x/tools/go/packages"
+)
+
+var pathForDirCache = sync.Map{}
+
+// ImportPathFromDir takes an *absolute* path and returns a golang import path for the package, and returns an error if it isn't on the gopath
+func ImportPathForDir(dir string) string {
+ if v, ok := pathForDirCache.Load(dir); ok {
+ return v.(string)
+ }
+
+ p, _ := packages.Load(&packages.Config{
+ Dir: dir,
+ }, ".")
+
+ // If the dir dosent exist yet, keep walking up the directory tree trying to find a match
+ if len(p) != 1 {
+ parent, err := filepath.Abs(filepath.Join(dir, ".."))
+ if err != nil {
+ panic(err)
+ }
+ // Walked all the way to the root and didnt find anything :'(
+ if parent == dir {
+ return ""
+ }
+ return ImportPathForDir(parent) + "/" + filepath.Base(dir)
+ }
+
+ pathForDirCache.Store(dir, p[0].PkgPath)
+
+ return p[0].PkgPath
+}
+
+var nameForPackageCache = sync.Map{}
+
+func NameForPackage(importPath string) string {
+ if importPath == "" {
+ panic(errors.New("import path can not be empty"))
+ }
+ if v, ok := nameForPackageCache.Load(importPath); ok {
+ return v.(string)
+ }
+ importPath = QualifyPackagePath(importPath)
+ p, _ := packages.Load(nil, importPath)
+
+ if len(p) != 1 || p[0].Name == "" {
+ return SanitizePackageName(filepath.Base(importPath))
+ }
+
+ nameForPackageCache.Store(importPath, p[0].Name)
+
+ return p[0].Name
+}