forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_build.go
71 lines (56 loc) · 1.79 KB
/
interface_build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package codegen
import (
"fmt"
"go/types"
"os"
"sort"
"strings"
"github.com/vektah/gqlparser/ast"
"golang.org/x/tools/go/loader"
)
func (cfg *Config) buildInterfaces(types NamedTypes, prog *loader.Program) []*Interface {
var interfaces []*Interface
for _, typ := range cfg.schema.Types {
if typ.Kind == ast.Union || typ.Kind == ast.Interface {
interfaces = append(interfaces, cfg.buildInterface(types, typ, prog))
}
}
sort.Slice(interfaces, func(i, j int) bool {
return strings.Compare(interfaces[i].GQLType, interfaces[j].GQLType) == -1
})
return interfaces
}
func (cfg *Config) buildInterface(types NamedTypes, typ *ast.Definition, prog *loader.Program) *Interface {
i := &Interface{NamedType: types[typ.Name]}
for _, implementor := range cfg.schema.GetPossibleTypes(typ) {
t := types[implementor.Name]
i.Implementors = append(i.Implementors, InterfaceImplementor{
NamedType: t,
ValueReceiver: cfg.isValueReceiver(types[typ.Name], t, prog),
})
}
return i
}
func (cfg *Config) isValueReceiver(intf *NamedType, implementor *NamedType, prog *loader.Program) bool {
interfaceType, err := findGoInterface(prog, intf.Package, intf.GoType)
if interfaceType == nil || err != nil {
return true
}
implementorType, err := findGoNamedType(prog, implementor.Package, implementor.GoType)
if implementorType == nil || err != nil {
return true
}
for i := 0; i < interfaceType.NumMethods(); i++ {
intfMethod := interfaceType.Method(i)
implMethod := findMethod(implementorType, intfMethod.Name())
if implMethod == nil {
fmt.Fprintf(os.Stderr, "missing method %s on %s\n", intfMethod.Name(), implementor.GoType)
return false
}
sig := implMethod.Type().(*types.Signature)
if _, isPtr := sig.Recv().Type().(*types.Pointer); isPtr {
return false
}
}
return true
}