From 0f2413b23cba8ce0ee2493d4d3a962d7c977cc5b Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Tue, 26 Nov 2019 21:22:36 +0300 Subject: [PATCH] deduplicate package load between AutoBind and Binder --- codegen/config/binder.go | 7 +------ codegen/config/binder_test.go | 8 +++++++- codegen/config/config.go | 27 +++++++++++++++++++++------ codegen/config/config_test.go | 6 +++++- codegen/data.go | 18 ++++++++++++------ plugin/modelgen/models.go | 11 ++++++++--- 6 files changed, 54 insertions(+), 23 deletions(-) diff --git a/codegen/config/binder.go b/codegen/config/binder.go index 7ac9bff01e..0ebf415735 100644 --- a/codegen/config/binder.go +++ b/codegen/config/binder.go @@ -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 { diff --git a/codegen/config/binder_test.go b/codegen/config/binder_test.go index 6a842a12da..fa0ac15c71 100644 --- a/codegen/config/binder_test.go +++ b/codegen/config/binder_test.go @@ -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) { @@ -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) } diff --git a/codegen/config/config.go b/codegen/config/config.go index 477de0fc07..7c258b6529 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -395,15 +395,24 @@ 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) { @@ -411,6 +420,9 @@ func (c *Config) Autobind(s *ast.Schema) error { } 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 @@ -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 } diff --git a/codegen/config/config_test.go b/codegen/config/config_test.go index 7551a11543..9d5fdc74d9 100644 --- a/codegen/config/config_test.go +++ b/codegen/config/config_test.go @@ -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" @@ -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]) diff --git a/codegen/data.go b/codegen/data.go index ef92ec8b48..4073663f4d 100644 --- a/codegen/data.go +++ b/codegen/data.go @@ -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 @@ -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 { @@ -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 } diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index 3458b42bdf..0f8b0ec113 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -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 }