Skip to content

Commit

Permalink
Merge pull request #710 from 99designs/slice-pointers
Browse files Browse the repository at this point in the history
Use pointers to structs inside slices
  • Loading branch information
vektah authored May 15, 2019
2 parents e669d47 + 315141d commit f3c8406
Show file tree
Hide file tree
Showing 31 changed files with 584 additions and 436 deletions.
6 changes: 5 additions & 1 deletion codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,11 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret

func (b *Binder) CopyModifiersFromAst(t *ast.Type, base types.Type) types.Type {
if t.Elem != nil {
return types.NewSlice(b.CopyModifiersFromAst(t.Elem, base))
child := b.CopyModifiersFromAst(t.Elem, base)
if _, isStruct := child.Underlying().(*types.Struct); isStruct {
child = types.NewPointer(child)
}
return types.NewSlice(child)
}

var isInterface bool
Expand Down
32 changes: 16 additions & 16 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions codegen/testserver/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestMiddleware(t *testing.T) {
return &User{ID: 1}, nil
}

resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) (users []User, e error) {
return []User{{ID: 1}}, nil
resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) (users []*User, e error) {
return []*User{{ID: 1}}, nil
}

areMethods := []bool{}
Expand Down Expand Up @@ -57,10 +57,10 @@ func TestMiddleware(t *testing.T) {
}

called := false
resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) ([]User, error) {
resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) ([]*User, error) {
assert.Equal(t, []int{1, 2, 1, 2}, ctx.Value("path"))
called = true
return []User{}, nil
return []*User{}, nil
}

err := c.Post(`query { user(id: 1) { id, friends { id } } }`, &resp)
Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,6 @@ func (r *subscriptionResolver) InitPayload(ctx context.Context) (<-chan string,

type userResolver struct{ *Resolver }

func (r *userResolver) Friends(ctx context.Context, obj *User) ([]User, error) {
func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error) {
panic("not implemented")
}
4 changes: 2 additions & 2 deletions codegen/testserver/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions codegen/testserver/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestTracer(t *testing.T) {
}

called := false
resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) ([]User, error) {
resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) ([]*User, error) {
assert.Equal(t, []string{
"op:p:start:1", "op:p:start:2",
"op:v:start:1", "op:v:start:2",
Expand All @@ -76,7 +76,7 @@ func TestTracer(t *testing.T) {
"field'b:e:start:1:[user friends]", "field'b:e:start:2:[user friends]",
}, ctx.Value("tracer"))
called = true
return []User{}, nil
return []*User{}, nil
}

err := c.Post(`query { user(id: 1) { id, friends { id } } }`, &resp)
Expand Down Expand Up @@ -175,9 +175,9 @@ func TestTracer(t *testing.T) {
}

called := false
resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) ([]User, error) {
resolvers.UserResolver.Friends = func(ctx context.Context, obj *User) ([]*User, error) {
called = true
return []User{}, nil
return []*User{}, nil
}

err := c.Post(`query { user(id: 1) { id, friends { id } } }`, &resp)
Expand Down
10 changes: 5 additions & 5 deletions example/config/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions example/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func New() Config {
c := Config{
Resolvers: &Resolver{
todos: []Todo{
todos: []*Todo{
{DatabaseID: 1, Description: "A todo not to forget", Done: false},
{DatabaseID: 2, Description: "This is the most important", Done: false},
{DatabaseID: 3, Description: "Please do this or else", Done: false},
Expand All @@ -22,7 +22,7 @@ func New() Config {
}

type Resolver struct {
todos []Todo
todos []*Todo
nextID int
}

Expand All @@ -42,19 +42,19 @@ func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (*Todo
newID := r.nextID
r.nextID++

newTodo := Todo{
newTodo := &Todo{
DatabaseID: newID,
Description: input.Text,
}

r.todos = append(r.todos, newTodo)

return &newTodo, nil
return newTodo, nil
}

type queryResolver struct{ *Resolver }

func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) {
func (r *queryResolver) Todos(ctx context.Context) ([]*Todo, error) {
return r.todos, nil
}

Expand Down
Loading

0 comments on commit f3c8406

Please sign in to comment.