Skip to content

Commit

Permalink
copy old imports through before gofmt prunes
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 3, 2020
1 parent 6ec3650 commit e725558
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
38 changes: 37 additions & 1 deletion internal/rewrite/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"go/ast"
"go/token"
"io/ioutil"
"path/filepath"
"strconv"

"golang.org/x/tools/go/packages"
)
Expand Down Expand Up @@ -62,7 +64,7 @@ func (r *Rewriter) GetMethodBody(structname string, methodname string) string {
if d.Name.Name != methodname {
continue
}
if d.Recv.List == nil {
if d.Recv == nil || d.Recv.List == nil {
continue
}
recv := d.Recv.List[0].Type
Expand All @@ -85,3 +87,37 @@ func (r *Rewriter) GetMethodBody(structname string, methodname string) string {

return ""
}

func (r *Rewriter) ExistingImports(filename string) []Import {
filename, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
for _, f := range r.pkg.Syntax {
pos := r.pkg.Fset.Position(f.Pos())

if filename != pos.Filename {
continue
}

var imps []Import
for _, i := range f.Imports {
name := ""
if i.Name != nil {
name = i.Name.Name
}
path, err := strconv.Unquote(i.Path.Value)
if err != nil {
panic(err)
}
imps = append(imps, Import{name, path})
}
return imps
}
return nil
}

type Import struct {
Alias string
ImportPath string
}
14 changes: 14 additions & 0 deletions internal/rewrite/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rewrite
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -19,4 +20,17 @@ func TestRewriter(t *testing.T) {
// trailing comment
`, body)

imps := r.ExistingImports("testdata/example.go")
require.Len(t, imps, 2)
assert.Equal(t, []Import{
{
Alias: "",
ImportPath: "fmt",
},
{
Alias: "lol",
ImportPath: "bytes",
},
}, imps)
}
10 changes: 10 additions & 0 deletions internal/rewrite/testdata/example.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package testdata

import "fmt"

import lol "bytes"

type Foo struct {
Field int
}
Expand All @@ -12,3 +16,9 @@ func (m *Foo) Method(arg int) {

// trailing comment
}

func (m *Foo) String() string {
var buf lol.Buffer
buf.WriteString(fmt.Sprintf("%d", m.Field))
return buf.String()
}
16 changes: 15 additions & 1 deletion plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
if o.HasResolvers() {
fn := gqlToResolverName(data.Config.Resolver.Dir(), o.Position.Src.Name)
if files[fn] == nil {
files[fn] = &File{}
files[fn] = &File{
imports: rewriter.ExistingImports(fn),
}
}

files[fn].Objects = append(files[fn].Objects, o)
Expand Down Expand Up @@ -166,6 +168,18 @@ type File struct {
//resolver method implementations, for example when extending a type in a different graphql schema file
Objects []*codegen.Object
Resolvers []*Resolver
imports []rewrite.Import
}

func (f *File) Imports() string {
for _, imp := range f.imports {
if imp.Alias == "" {
templates.CurrentImports.Reserve(imp.ImportPath)
} else {
templates.CurrentImports.Reserve(imp.ImportPath, imp.Alias)
}
}
return ""
}

type Resolver struct {
Expand Down
2 changes: 2 additions & 0 deletions plugin/resolvergen/resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
{{ reserveImport "github.com/99designs/gqlgen/graphql" }}
{{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }}

{{ .Imports }}

{{ if .HasRoot }}
type {{.ResolverType}} struct {}
{{ end }}
Expand Down

0 comments on commit e725558

Please sign in to comment.