From 26b5343e2160de172969e6834074cf8482ceb845 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Tue, 9 Apr 2019 21:45:24 +0200 Subject: Update Gopkg.* --- .../99designs/gqlgen/internal/code/imports.go | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 vendor/github.com/99designs/gqlgen/internal/code/imports.go (limited to 'vendor/github.com/99designs/gqlgen/internal/code/imports.go') 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 +} -- cgit