Skip to content

Commit

Permalink
copy through any unknown data
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 3, 2020
1 parent e725558 commit dbaf355
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
1 change: 0 additions & 1 deletion example/config/todo.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ input NewTodo {
text: String!
userId: String!
}

73 changes: 69 additions & 4 deletions internal/rewrite/rewriter.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package rewrite

import (
"bytes"
"fmt"
"go/ast"
"go/token"
"io/ioutil"
"path/filepath"
"strconv"
"strings"

"golang.org/x/tools/go/packages"
)

type Rewriter struct {
pkg *packages.Package
files map[string]string
pkg *packages.Package
files map[string]string
copied map[ast.Decl]bool
}

func New(importPath string) (*Rewriter, error) {
Expand All @@ -25,8 +28,9 @@ func New(importPath string) (*Rewriter, error) {
}

return &Rewriter{
pkg: pkgs[0],
files: map[string]string{},
pkg: pkgs[0],
files: map[string]string{},
copied: map[ast.Decl]bool{},
}, nil
}

Expand Down Expand Up @@ -80,6 +84,8 @@ func (r *Rewriter) GetMethodBody(structname string, methodname string) string {
continue
}

r.copied[d] = true

return r.getSource(d.Body.Pos()+1, d.Body.End()-1)
}
}
Expand All @@ -88,6 +94,30 @@ func (r *Rewriter) GetMethodBody(structname string, methodname string) string {
return ""
}

func (r *Rewriter) MarkStructCopied(name string) {
for _, f := range r.pkg.Syntax {
for _, d := range f.Decls {
switch d := d.(type) {
case *ast.GenDecl:
if d.Tok != token.TYPE || len(d.Specs) == 0 {
continue
}

spec, isTypeSpec := d.Specs[0].(*ast.TypeSpec)
if !isTypeSpec {
continue
}

if spec.Name.Name != name {
continue
}

r.copied[d] = true
}
}
}
}

func (r *Rewriter) ExistingImports(filename string) []Import {
filename, err := filepath.Abs(filename)
if err != nil {
Expand Down Expand Up @@ -117,6 +147,41 @@ func (r *Rewriter) ExistingImports(filename string) []Import {
return nil
}

func (r *Rewriter) RemainingSource(filename string) string {
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 buf bytes.Buffer

for _, d := range f.Decls {
if r.copied[d] {
continue
}
switch d := d.(type) {
case *ast.GenDecl:
if d.Tok == token.IMPORT {
continue
}
}
fmt.Printf("%T\n", d)

buf.WriteString(r.getSource(d.Pos(), d.End()))
buf.WriteString("\n")
}

return strings.TrimSpace(buf.String())
}
return ""
}

type Import struct {
Alias string
ImportPath string
Expand Down
15 changes: 12 additions & 3 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resolvergen

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -94,6 +95,9 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
}
}

rewriter.MarkStructCopied(templates.LcFirst(o.Name) + templates.UcFirst(data.Config.Resolver.Type))
rewriter.GetMethodBody(data.Config.Resolver.Type, o.Name)
fmt.Println(data.Config.Resolver.Type, o.Name)
files[fn].Objects = append(files[fn].Objects, o)
}
for _, f := range o.Fields {
Expand All @@ -117,6 +121,10 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
}
}

for filename, file := range files {
file.RemainingSource = rewriter.RemainingSource(filename)
}

for filename, file := range files {
resolverBuild := &ResolverBuild{
File: file,
Expand Down Expand Up @@ -166,9 +174,10 @@ type ResolverBuild struct {
type File struct {
// These are separated because the type definition of the resolver object may live in a different file from the
//resolver method implementations, for example when extending a type in a different graphql schema file
Objects []*codegen.Object
Resolvers []*Resolver
imports []rewrite.Import
Objects []*codegen.Object
Resolvers []*Resolver
imports []rewrite.Import
RemainingSource string
}

func (f *File) Imports() string {
Expand Down
9 changes: 9 additions & 0 deletions plugin/resolvergen/resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@
type {{lcFirst $object.Name}}{{ucFirst $.ResolverType}} struct { *{{$.ResolverType}} }
{{ end }}

{{ if (ne .RemainingSource "") }}
// !!! WARNING !!!
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
// one last chance to move it out of harms way if you want. There are two reasons this happens:
// - When renaming or deleting a resolver the old code will be put in here. You can safely delete
// it when you're done.
// - You have helper methods in this file. Move them out to keep these resolver files clean.
{{ .RemainingSource }}
{{ end }}

0 comments on commit dbaf355

Please sign in to comment.