aboutsummaryrefslogblamecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/interface.go
blob: f59e8ed07154ec21afef1fe0ce613a759370bfc4 (plain) (tree)
1
2
3
4
5
6
7
8

               




                                         
 


                               
                                           
                         


                                  











































                                                                             
 
                                                               
 
package codegen

import (
	"go/types"

	"github.com/vektah/gqlparser/ast"
)

type Interface struct {
	*ast.Definition
	Type         types.Type
	Implementors []InterfaceImplementor
	InTypemap    bool
}

type InterfaceImplementor struct {
	*ast.Definition

	Interface *Interface
	Type      types.Type
}

func (b *builder) buildInterface(typ *ast.Definition) *Interface {
	obj, err := b.Binder.DefaultUserObject(typ.Name)
	if err != nil {
		panic(err)
	}

	i := &Interface{
		Definition: typ,
		Type:       obj,
		InTypemap:  b.Config.Models.UserDefined(typ.Name),
	}

	for _, implementor := range b.Schema.GetPossibleTypes(typ) {
		obj, err := b.Binder.DefaultUserObject(implementor.Name)
		if err != nil {
			panic(err)
		}

		i.Implementors = append(i.Implementors, InterfaceImplementor{
			Definition: implementor,
			Type:       obj,
			Interface:  i,
		})
	}

	return i
}

func (i *InterfaceImplementor) ValueReceiver() bool {
	interfaceType, err := findGoInterface(i.Interface.Type)
	if interfaceType == nil || err != nil {
		return true
	}

	implementorType, err := findGoNamedType(i.Type)
	if implementorType == nil || err != nil {
		return true
	}

	return types.Implements(implementorType, interfaceType)
}