-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start moving import management to templates
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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) 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package templates | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestImports(t *testing.T) { | ||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
aBar := "github.com/99designs/gqlgen/codegen/templates/testdata/a/bar" | ||
bBar := "github.com/99designs/gqlgen/codegen/templates/testdata/b/bar" | ||
mismatch := "github.com/99designs/gqlgen/codegen/templates/testdata/pkg_mismatch" | ||
|
||
t.Run("multiple lookups is ok", func(t *testing.T) { | ||
a := Imports{destDir: wd} | ||
|
||
require.Equal(t, "bar", a.Lookup(aBar)) | ||
require.Equal(t, "bar", a.Lookup(aBar)) | ||
}) | ||
|
||
t.Run("duplicates are decollisioned", func(t *testing.T) { | ||
a := Imports{destDir: wd} | ||
|
||
require.Equal(t, "bar", a.Lookup(aBar)) | ||
require.Equal(t, "bar1", a.Lookup(bBar)) | ||
|
||
t.Run("additionial calls get decollisioned name", func(t *testing.T) { | ||
require.Equal(t, "bar1", a.Lookup(bBar)) | ||
}) | ||
}) | ||
|
||
t.Run("package name defined in code will be used", func(t *testing.T) { | ||
a := Imports{destDir: wd} | ||
|
||
require.Equal(t, "turtles", a.Lookup(mismatch)) | ||
}) | ||
|
||
t.Run("string printing for import block", func(t *testing.T) { | ||
a := Imports{destDir: wd} | ||
a.Lookup(aBar) | ||
a.Lookup(bBar) | ||
a.Lookup(mismatch) | ||
|
||
require.Equal( | ||
t, | ||
`"github.com/99designs/gqlgen/codegen/templates/testdata/a/bar" | ||
bar1 "github.com/99designs/gqlgen/codegen/templates/testdata/b/bar" | ||
"github.com/99designs/gqlgen/codegen/templates/testdata/pkg_mismatch"`, | ||
a.String(), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package turtles |