From e82b401ddd97fe807a5066d3702b391ec317a358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Borgstr=C3=B6m?= Date: Wed, 28 Apr 2021 15:11:00 +0200 Subject: [PATCH] allow more than 10 different import sources with types --- codegen/templates/import.go | 2 +- codegen/templates/import_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/codegen/templates/import.go b/codegen/templates/import.go index 17bd96ab2e..00a82ea5ef 100644 --- a/codegen/templates/import.go +++ b/codegen/templates/import.go @@ -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)) } } diff --git a/codegen/templates/import_test.go b/codegen/templates/import_test.go index 2e8dd5a89a..a91d2a8ccd 100644 --- a/codegen/templates/import_test.go +++ b/codegen/templates/import_test.go @@ -1,6 +1,7 @@ package templates import ( + "fmt" "go/types" "os" "testing" @@ -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{}}