From ee6add4bddd7ee05e31a37815f281c320eb44e26 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Mon, 19 Jun 2023 14:00:22 -0400 Subject: [PATCH] Refactor TypeIdentifier to avoid circular imports (#2682) Signed-off-by: Steve Coffman --- codegen/config/binder.go | 42 ++++++++++++++++++++++++++++++++-- codegen/templates/templates.go | 42 ++-------------------------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/codegen/config/binder.go b/codegen/config/binder.go index e479417aa5a..0483afdbdfa 100644 --- a/codegen/config/binder.go +++ b/codegen/config/binder.go @@ -5,10 +5,10 @@ import ( "fmt" "go/token" "go/types" + "strings" "golang.org/x/tools/go/packages" - "github.com/99designs/gqlgen/codegen/templates" "github.com/99designs/gqlgen/internal/code" "github.com/vektah/gqlparser/v2/ast" ) @@ -285,7 +285,7 @@ func (ref *TypeReference) UniquenessKey() string { // Fix for #896 elemNullability = "ᚄ" } - return nullability + ref.Definition.Name + "2" + templates.TypeIdentifier(ref.GO) + elemNullability + return nullability + ref.Definition.Name + "2" + TypeIdentifier(ref.GO) + elemNullability } func (ref *TypeReference) MarshalFunc() string { @@ -540,3 +540,41 @@ func basicUnderlying(it types.Type) *types.Basic { return nil } + +var pkgReplacer = strings.NewReplacer( + "/", "ᚋ", + ".", "ᚗ", + "-", "ᚑ", + "~", "א", +) + +func TypeIdentifier(t types.Type) string { + res := "" + for { + switch it := t.(type) { + case *types.Pointer: + t.Underlying() + res += "ᚖ" + t = it.Elem() + case *types.Slice: + res += "ᚕ" + t = it.Elem() + case *types.Named: + res += pkgReplacer.Replace(it.Obj().Pkg().Path()) + res += "ᚐ" + res += it.Obj().Name() + return res + case *types.Basic: + res += it.Name() + return res + case *types.Map: + res += "map" + return res + case *types.Interface: + res += "interface" + return res + default: + panic(fmt.Errorf("unexpected type %T", it)) + } + } +} diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index a3b3be0a17c..6d536d3c17e 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -17,8 +17,8 @@ import ( "text/template" "unicode" + "github.com/99designs/gqlgen/codegen/config" "github.com/99designs/gqlgen/internal/code" - "github.com/99designs/gqlgen/internal/imports" ) @@ -202,7 +202,7 @@ func Funcs() template.FuncMap { "rawQuote": rawQuote, "dump": Dump, "ref": ref, - "ts": TypeIdentifier, + "ts": config.TypeIdentifier, "call": Call, "prefixLines": prefixLines, "notNil": notNil, @@ -248,44 +248,6 @@ func ref(p types.Type) string { return CurrentImports.LookupType(p) } -var pkgReplacer = strings.NewReplacer( - "/", "ᚋ", - ".", "ᚗ", - "-", "ᚑ", - "~", "א", -) - -func TypeIdentifier(t types.Type) string { - res := "" - for { - switch it := t.(type) { - case *types.Pointer: - t.Underlying() - res += "ᚖ" - t = it.Elem() - case *types.Slice: - res += "ᚕ" - t = it.Elem() - case *types.Named: - res += pkgReplacer.Replace(it.Obj().Pkg().Path()) - res += "ᚐ" - res += it.Obj().Name() - return res - case *types.Basic: - res += it.Name() - return res - case *types.Map: - res += "map" - return res - case *types.Interface: - res += "interface" - return res - default: - panic(fmt.Errorf("unexpected type %T", it)) - } - } -} - func Call(p *types.Func) string { pkg := CurrentImports.Lookup(p.Pkg().Path())