-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy existing resolver bodies when regenerating new resolvers
- Loading branch information
Showing
10 changed files
with
217 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This file will be automatically regenerated based on the schema, any resolver implementations | ||
// will be copied through when generating and any unknown code will be moved to the end. | ||
package config | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (*Todo, error) { | ||
newID := r.nextID | ||
r.nextID++ | ||
|
||
newTodo := &Todo{ | ||
DatabaseID: newID, | ||
Description: input.Text, | ||
} | ||
|
||
r.todos = append(r.todos, newTodo) | ||
|
||
return newTodo, nil | ||
} | ||
func (r *queryResolver) Todos(ctx context.Context) ([]*Todo, error) { | ||
return r.todos, nil | ||
} | ||
|
||
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} } | ||
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} } | ||
|
||
type mutationResolver struct{ *Resolver } | ||
type queryResolver struct{ *Resolver } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// This file will be automatically regenerated based on the schema, any resolver implementations | ||
// will be copied through when generating and any unknown code will be moved to the end. | ||
package config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func (r *todoResolver) ID(ctx context.Context, obj *Todo) (string, error) { | ||
if obj.ID != "" { | ||
return obj.ID, nil | ||
} | ||
|
||
obj.ID = fmt.Sprintf("TODO:%d", obj.DatabaseID) | ||
|
||
return obj.ID, nil | ||
} | ||
|
||
func (r *Resolver) Todo() TodoResolver { return &todoResolver{r} } | ||
|
||
type todoResolver struct{ *Resolver } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package rewrite | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
"io/ioutil" | ||
|
||
"golang.org/x/tools/go/packages" | ||
) | ||
|
||
type Rewriter struct { | ||
pkg *packages.Package | ||
files map[string]string | ||
} | ||
|
||
func New(importPath string) (*Rewriter, error) { | ||
pkgs, err := packages.Load(&packages.Config{ | ||
Mode: packages.NeedSyntax | packages.NeedTypes, | ||
}, importPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Rewriter{ | ||
pkg: pkgs[0], | ||
files: map[string]string{}, | ||
}, nil | ||
} | ||
|
||
func (r *Rewriter) getSource(start, end token.Pos) string { | ||
startPos := r.pkg.Fset.Position(start) | ||
endPos := r.pkg.Fset.Position(end) | ||
|
||
if startPos.Filename != endPos.Filename { | ||
panic("cant get source spanning multiple files") | ||
} | ||
|
||
file := r.getFile(startPos.Filename) | ||
return file[startPos.Offset:endPos.Offset] | ||
} | ||
|
||
func (r *Rewriter) getFile(filename string) string { | ||
if _, ok := r.files[filename]; !ok { | ||
b, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
panic(fmt.Errorf("unable to load file, already exists: %s", err.Error())) | ||
} | ||
|
||
r.files[filename] = string(b) | ||
|
||
} | ||
|
||
return r.files[filename] | ||
} | ||
|
||
func (r *Rewriter) GetMethodBody(structname string, methodname string) string { | ||
for _, f := range r.pkg.Syntax { | ||
for _, d := range f.Decls { | ||
switch d := d.(type) { | ||
case *ast.FuncDecl: | ||
if d.Name.Name != methodname { | ||
continue | ||
} | ||
if d.Recv.List == nil { | ||
continue | ||
} | ||
recv := d.Recv.List[0].Type | ||
if star, isStar := d.Recv.List[0].Type.(*ast.StarExpr); isStar { | ||
recv = star.X | ||
} | ||
ident, ok := recv.(*ast.Ident) | ||
if !ok { | ||
continue | ||
} | ||
|
||
if ident.Name != structname { | ||
continue | ||
} | ||
|
||
return r.getSource(d.Body.Pos()+1, d.Body.End()-1) | ||
} | ||
} | ||
} | ||
|
||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package rewrite | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRewriter(t *testing.T) { | ||
r, err := New("github.com/99designs/gqlgen/internal/rewrite/testdata") | ||
require.NoError(t, err) | ||
|
||
body := r.GetMethodBody("Foo", "Method") | ||
require.Equal(t, ` | ||
// leading comment | ||
// field comment | ||
m.Field++ | ||
// trailing comment | ||
`, body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package testdata | ||
|
||
type Foo struct { | ||
Field int | ||
} | ||
|
||
func (m *Foo) Method(arg int) { | ||
// leading comment | ||
|
||
// field comment | ||
m.Field++ | ||
|
||
// trailing comment | ||
} |
Oops, something went wrong.