forked from sensu/sensu-go
/
common.go
167 lines (151 loc) · 3.51 KB
/
common.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package generator
import (
"fmt"
"github.com/dave/jennifer/jen"
"github.com/graphql-go/graphql/language/ast"
)
const (
MissingNamedDirectiveErr = `extend keyword must be followed by @named directive`
)
func getNodeName(def ast.Node) string {
switch d := def.(type) {
case *ast.EnumDefinition:
return d.Name.Value
case *ast.InputObjectDefinition:
return d.Name.Value
case *ast.InterfaceDefinition:
return d.Name.Value
case *ast.ObjectDefinition:
return d.Name.Value
case *ast.ScalarDefinition:
return d.Name.Value
case *ast.UnionDefinition:
return d.Name.Value
case *ast.TypeExtensionDefinition:
objDef := d.Definition
suffix := mustExtractSuffix(objDef)
return objDef.Name.Value + "Extension" + suffix
default:
return ""
}
}
func getTypeStr(node ast.Node) string {
switch node.(type) {
case *ast.ScalarDefinition:
return "Scalar"
case *ast.ObjectDefinition:
return "Object"
case *ast.InputObjectDefinition:
return "Input"
case *ast.InterfaceDefinition:
return "Interface"
case *ast.UnionDefinition:
return "Union"
case *ast.EnumDefinition:
return "Enum"
case *ast.TypeExtensionDefinition:
return "ObjectExtension"
default:
fmt.Printf("node: %#v", node)
panic("unknown node")
}
}
func mkPrivatePrefix(node ast.Node) string {
name := getNodeName(node)
typeStr := getTypeStr(node)
return fmt.Sprintf("_%sType%s", typeStr, name)
}
func mkPrivateID(node ast.Node, id string) string {
return mkPrivatePrefix(node) + id
}
//
// Generate public func to register type with service
//
// == Example output
//
// // RegisterDog registers Dog type with given service.
// func RegisterDog(svc graphql.Service, impl DogFieldResolvers) {
// svc.RegisterObject(_ObjTypeDogDesc, impl)
// }
//
func genRegisterFn(node ast.Node, resolverImpl jen.Code) jen.Code {
name := getNodeName(node)
typeStr := getTypeStr(node)
registerFnName := "Register" + name
privateConfigName := mkPrivateID(node, "Desc")
code := newGroup()
code.Commentf(
"%s registers %s object type with given service.",
registerFnName,
name,
)
code.
Func().Id(registerFnName).
ParamsFunc(func(g *jen.Group) {
g.Id("svc").Op("*").Qual(servicePkg, "Service")
if resolverImpl != nil {
g.Id("impl").Add(resolverImpl)
}
}).
Block(
jen.
Id("svc.Register" + typeStr).
CallFunc(func(g *jen.Group) {
g.Id(privateConfigName)
if resolverImpl != nil {
g.Id("impl")
}
}),
)
return code
}
func isNonNullableEnum(tt ast.Type, i info) bool {
t, ok := tt.(*ast.NonNull)
if !ok {
return false
}
return isEnum(t.Type, i)
}
func isEnum(tt ast.Type, i info) bool {
t, ok := tt.(*ast.Named)
if !ok {
return false
}
def, ok := i.definitions[t.Name.Value]
if !ok {
return false
}
_, isEnum := def.(*ast.EnumDefinition)
return isEnum
}
func mustExtractSuffix(obj *ast.ObjectDefinition) string {
namedDir := findDirectiveNamed(obj.Directives, "named")
if namedDir == nil {
logger.Fatal(MissingNamedDirectiveErr)
}
suffixArg := findArgumentNamed(namedDir.Arguments, "suffix")
if suffixArg == nil {
logger.Fatal(MissingNamedDirectiveErr)
}
suffix, ok := suffixArg.Value.GetValue().(string)
if !ok {
logger.Fatal(MissingNamedDirectiveErr)
}
return suffix
}
func findDirectiveNamed(ds []*ast.Directive, name string) *ast.Directive {
for _, d := range ds {
if d.Name.Value == name {
return d
}
}
return nil
}
func findArgumentNamed(as []*ast.Argument, name string) *ast.Argument {
for _, a := range as {
if a.Name.Value == name {
return a
}
}
return nil
}