Skip to content

Commit

Permalink
allow more than 10 different import sources with types
Browse files Browse the repository at this point in the history
  • Loading branch information
simborgs committed Apr 28, 2021
1 parent 5ad012e commit e82b401
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion codegen/templates/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *Imports) Lookup(path string) string {
for s.findByAlias(alias) != nil {
alias = imp.Name + strconv.Itoa(i)
i++
if i > 10 {
if i > 1000 {
panic(fmt.Errorf("too many collisions, last attempt was %s", alias))
}
}
Expand Down
14 changes: 14 additions & 0 deletions codegen/templates/import_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package templates

import (
"fmt"
"go/types"
"os"
"testing"
Expand Down Expand Up @@ -45,6 +46,19 @@ func TestImports(t *testing.T) {
})
})

t.Run("duplicates above 10 are decollisioned", func(t *testing.T) {
a := Imports{destDir: wd, packages: &code.Packages{}}
for i := 0; i < 100; i++ {
cBar := fmt.Sprintf("github.com/99designs/gqlgen/codegen/templates/testdata/%d/bar", i)
if i > 0 {
require.Equal(t, fmt.Sprintf("bar%d", i), a.Lookup(cBar))
} else {
require.Equal(t, "bar", a.Lookup(cBar))

}
}
})

t.Run("package name defined in code will be used", func(t *testing.T) {
a := Imports{destDir: wd, packages: &code.Packages{}}

Expand Down

0 comments on commit e82b401

Please sign in to comment.