Skip to content

Commit

Permalink
Refactor TypeIdentifier to avoid circular imports (#2682)
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
StevenACoffman authored Jun 19, 2023
1 parent 44376e5 commit ee6add4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
42 changes: 40 additions & 2 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
}
}
42 changes: 2 additions & 40 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())

Expand Down

0 comments on commit ee6add4

Please sign in to comment.