Skip to content

Commit

Permalink
deduplicate package load between AutoBind and Binder
Browse files Browse the repository at this point in the history
  • Loading branch information
vikstrous committed Jan 9, 2020
1 parent 28c032d commit 0f2413b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
7 changes: 1 addition & 6 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ type Binder struct {
SawInvalid bool
}

func (c *Config) NewBinder(s *ast.Schema) (*Binder, error) {
pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, c.Models.ReferencedPackages()...)
if err != nil {
return nil, err
}

func (c *Config) NewBinder(s *ast.Schema, pkgs []*packages.Package) (*Binder, error) {
mp := map[string]*packages.Package{}
var pkgErrs PkgErrors
for _, p := range pkgs {
Expand Down
8 changes: 7 additions & 1 deletion codegen/config/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/packages"
)

func TestBindingToInvalid(t *testing.T) {
Expand Down Expand Up @@ -58,7 +59,12 @@ func createBinder(cfg Config) (*Binder, *ast.Schema) {
}
`})

b, err := cfg.NewBinder(s)
ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, cfg.Models.ReferencedPackages()...)
if err != nil {
panic(err)
}

b, err := cfg.NewBinder(s, ps)
if err != nil {
panic(err)
}
Expand Down
27 changes: 21 additions & 6 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,22 +395,34 @@ func (c *Config) normalize() error {

return nil
}

func (c *Config) Autobind(s *ast.Schema) error {
func (c *Config) isAutobind(pkg *packages.Package) bool {
for _, ab := range c.AutoBind {
if strings.HasSuffix(ab, "/...") {
abPrefix := strings.TrimSuffix(ab, "/...")
if strings.HasPrefix(pkg.PkgPath, abPrefix) {
return true
}
}
if pkg.PkgPath == ab {
return true
}
}
return false
}
func (c *Config) Autobind(s *ast.Schema, ps []*packages.Package) error {
if len(c.AutoBind) == 0 {
return nil
}
ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes}, c.AutoBind...)
if err != nil {
return err
}

for _, t := range s.Types {
if c.Models.UserDefined(t.Name) {
continue
}

for _, p := range ps {
if !c.isAutobind(p) {
continue
}
if t := p.Types.Scope().Lookup(t.Name); t != nil {
c.Models.Add(t.Name(), t.Pkg().Path()+"."+t.Name())
break
Expand All @@ -428,6 +440,9 @@ func (c *Config) Autobind(s *ast.Schema) error {
}

for _, p := range ps {
if !c.isAutobind(p) {
continue
}
if p.Name != pkg {
continue
}
Expand Down
6 changes: 5 additions & 1 deletion codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/packages"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -132,7 +133,10 @@ func TestAutobinding(t *testing.T) {
type Message { id: ID }
`})

require.NoError(t, cfg.Autobind(s))
ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes}, cfg.AutoBind...)
require.NoError(t, err)

require.NoError(t, cfg.Autobind(s, ps))

require.Equal(t, "github.com/99designs/gqlgen/example/scalars/model.Banned", cfg.Models["Banned"].Model[0])
require.Equal(t, "github.com/99designs/gqlgen/example/chat.Message", cfg.Models["Message"].Model[0])
Expand Down
18 changes: 12 additions & 6 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/formatter"
"golang.org/x/tools/go/packages"
)

// Data is a unified model of the code to be generated. Plugins may modify this structure to do things like implement
Expand Down Expand Up @@ -57,11 +58,6 @@ func BuildData(cfg *config.Config, plugins []SchemaMutator) (*Data, error) {
return nil, err
}

err = cfg.Autobind(b.Schema)
if err != nil {
return nil, err
}

cfg.InjectBuiltins(b.Schema)

for _, p := range plugins {
Expand All @@ -71,7 +67,17 @@ func BuildData(cfg *config.Config, plugins []SchemaMutator) (*Data, error) {
}
}

b.Binder, err = b.Config.NewBinder(b.Schema)
ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, append(cfg.Models.ReferencedPackages(), cfg.AutoBind...)...)
if err != nil {
return nil, err
}

err = cfg.Autobind(b.Schema, ps)
if err != nil {
return nil, err
}

b.Binder, err = b.Config.NewBinder(b.Schema, ps)
if err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,19 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
return err
}

err = cfg.Autobind(schema)
cfg.InjectBuiltins(schema)

ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, append(cfg.Models.ReferencedPackages(), cfg.AutoBind...)...)
if err != nil {
return err
}

cfg.InjectBuiltins(schema)
err = cfg.Autobind(schema, ps)
if err != nil {
return err
}

binder, err := cfg.NewBinder(schema)
binder, err := cfg.NewBinder(schema, ps)
if err != nil {
return err
}
Expand Down

0 comments on commit 0f2413b

Please sign in to comment.