Skip to content

Commit

Permalink
Merge pull request #633 from 99designs/fix-union-pointers
Browse files Browse the repository at this point in the history
Fix Having Pointers to Union Types
  • Loading branch information
Mathew Byrne committed Mar 18, 2019
2 parents 5df0938 + f02dabb commit fc05501
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 8 deletions.
13 changes: 9 additions & 4 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
ref.GO = obj.Type()
}

ref.GO = b.CopyModifiersFromAst(schemaType, def.Kind != ast.Interface, ref.GO)
ref.GO = b.CopyModifiersFromAst(schemaType, ref.GO)

if bindTarget != nil {
if err = code.CompatibleTypes(ref.GO, bindTarget); err != nil {
Expand All @@ -414,12 +414,17 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
return nil, fmt.Errorf("%s has type compatible with %s", schemaType.Name(), bindTarget.String())
}

func (b *Binder) CopyModifiersFromAst(t *ast.Type, usePtr bool, base types.Type) types.Type {
func (b *Binder) CopyModifiersFromAst(t *ast.Type, base types.Type) types.Type {
if t.Elem != nil {
return types.NewSlice(b.CopyModifiersFromAst(t.Elem, usePtr, base))
return types.NewSlice(b.CopyModifiersFromAst(t.Elem, base))
}

if !t.NonNull && usePtr {
var isInterface bool
if named, ok := base.(*types.Named); ok {
_, isInterface = named.Underlying().(*types.Interface)
}

if !isInterface && !t.NonNull {
return types.NewPointer(base)
}

Expand Down
209 changes: 209 additions & 0 deletions codegen/testserver/generated.go

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

16 changes: 16 additions & 0 deletions codegen/testserver/models-gen.go

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

3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (r *queryResolver) DefaultScalar(ctx context.Context, arg string) (string,
func (r *queryResolver) Slices(ctx context.Context) (*Slices, error) {
panic("not implemented")
}
func (r *queryResolver) OptionalUnion(ctx context.Context) (TestUnion, error) {
panic("not implemented")
}
func (r *queryResolver) ValidType(ctx context.Context) (*ValidType, error) {
panic("not implemented")
}
Expand Down
Loading

0 comments on commit fc05501

Please sign in to comment.