1 Star 0 Fork 0

idsutong/gqlgen

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
import.go 2.23 KB
一键复制 编辑 原始数据 按行查看 历史
Adam Scarr 提交于 2018-11-26 15:48 . Use new import handling code
package templates
import (
"fmt"
"go/build"
"strconv"
"github.com/99designs/gqlgen/internal/gopath"
)
type Import struct {
Name string
Path string
Alias string
}
type Imports struct {
imports []*Import
destDir string
}
func (i *Import) String() string {
if i.Alias == i.Name {
return strconv.Quote(i.Path)
}
return i.Alias + " " + strconv.Quote(i.Path)
}
func (s *Imports) String() string {
res := ""
for i, imp := range s.imports {
if i != 0 {
res += "\n"
}
res += imp.String()
}
return res
}
func (s *Imports) Reserve(path string, aliases ...string) string {
if path == "" {
panic("empty ambient import")
}
// if we are referencing our own package we dont need an import
if gopath.MustDir2Import(s.destDir) == path {
return ""
}
pkg, err := build.Default.Import(path, s.destDir, 0)
if err != nil {
panic(err)
}
var alias string
if len(aliases) != 1 {
alias = pkg.Name
} else {
alias = aliases[0]
}
if existing := s.findByPath(path); existing != nil {
panic("ambient import already exists")
}
if alias := s.findByAlias(alias); alias != nil {
panic("ambient import collides on an alias")
}
s.imports = append(s.imports, &Import{
Name: pkg.Name,
Path: path,
Alias: alias,
})
return ""
}
func (s *Imports) Lookup(path string) string {
if path == "" {
return ""
}
// if we are referencing our own package we dont need an import
if gopath.MustDir2Import(s.destDir) == path {
return ""
}
if existing := s.findByPath(path); existing != nil {
return existing.Alias
}
pkg, err := build.Default.Import(path, s.destDir, 0)
if err != nil {
panic(err)
}
imp := &Import{
Name: pkg.Name,
Path: path,
}
s.imports = append(s.imports, imp)
alias := imp.Name
i := 1
for s.findByAlias(alias) != nil {
alias = imp.Name + strconv.Itoa(i)
i++
if i > 10 {
panic(fmt.Errorf("too many collisions, last attempt was %s", alias))
}
}
imp.Alias = alias
return imp.Alias
}
func (s Imports) findByPath(importPath string) *Import {
for _, imp := range s.imports {
if imp.Path == importPath {
return imp
}
}
return nil
}
func (s Imports) findByAlias(alias string) *Import {
for _, imp := range s.imports {
if imp.Alias == alias {
return imp
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/idsutong/gqlgen.git
git@gitee.com:idsutong/gqlgen.git
idsutong
gqlgen
gqlgen
v0.7.0

搜索帮助