forked from sensu/sensu-go
/
interface.go
146 lines (136 loc) · 3.88 KB
/
interface.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
package generator
import (
"github.com/dave/jennifer/jen"
"github.com/graphql-go/graphql/language/ast"
)
//
// Generates description for scalar type
//
// == Example input SDL
//
// "Pets are the bestest family members"
// interface Pet {
// "name of this fine beast."
// name: String!
// }
//
// == Example output
//
// // PetType - Pets are the bestest family members
// var PetType = graphql.NewType("Pet", graphql.InterfaceKind)
//
// // RegisterPet registers Pet interface type with given service.
// func RegisterPet(svc graphql.Service, impl graphql.InterfaceTypeResolver) {
// svc.RegisterInterface(_InterfaceTypePetDesc, impl)
// }
//
// // define configuration thunk
// func _InterfaceTypePetConfigFn() graphql.InterfaceConfig {
// return graphql.InterfaceConfig{
// Name: "Pet",
// Description: "Pets are the bestest family members",
// Fields: // ...
// ResolveType: func (_ ResolveTypeParams) string {
// panic("Unimplemented; see PetResolver.")
// },
// }
// }
//
// // describe pet's configuration; kept private to avoid unintentional
// // tampering at runtime.
// var _InterfaceTypePetDesc = graphql.InterfaceDesc{
// Config: _InterfaceTypePetConfigFn,
// }
//
func genInterface(node *ast.InterfaceDefinition) jen.Code {
code := newGroup()
name := node.GetName().Value
// Type description
desc := getDescription(node)
// Ids
publicRefName := name + "Type"
publicRefComment := genTypeComment(publicRefName, desc)
privateConfigName := mkPrivateID(node, "Desc")
privateConfigThunkName := mkPrivateID(node, "ConfigFn")
//
// Generate public reference to type
//
// == Example output
//
// // PetType - Pets are the bestest family members
// var PetType = graphql.NewType("Pet", graphql.InterfaceKind)
//
code.Comment(publicRefComment)
code.
Var().Id(publicRefName).Op("=").
Qual(servicePkg, "NewType").
Call(jen.Lit(name), jen.Qual(servicePkg, "InterfaceKind"))
//
// Generate public func to register type with service
//
// == Example output
//
// // RegisterPet registers Pet interface type with given service.
// func RegisterPet(svc graphql.Service, impl graphql.InterfaceTypeResolver) {
// svc.RegisterInterface(_InterfaceTypePetDesc, impl)
// }
//
code.Add(
genRegisterFn(node, jen.Qual(servicePkg, "InterfaceTypeResolver")),
)
//
// Generates type config thunk
//
// == Example output
//
// // define configuration thunk
// func _InterfaceTypePetConfigFn() graphql.InterfaceConfig {
// return graphql.InterfaceConfig{
// Name: "Pet",
// Description: "Pets are the bestest family members",
// Fields: // ...
// ResolveType: func (_ ResolveTypeParams) string {
// panic("Unimplemented; see PetResolver.")
// },
// }
// }
//
code.
Func().Id(privateConfigThunkName).
Params().Qual(defsPkg, "InterfaceConfig").
Block(
jen.Return(jen.Qual(defsPkg, "InterfaceConfig").Values(jen.Dict{
jen.Id("Name"): jen.Lit(name),
jen.Id("Description"): jen.Lit(desc),
jen.Id("Fields"): genFields(node.Fields),
jen.Id("ResolveType"): jen.Func().
Params(jen.Id("_").Qual(defsPkg, "ResolveTypeParams")).
Op("*").Qual(defsPkg, "Object").
Block(
jen.Comment(missingResolverNote),
jen.Panic(jen.Lit("Unimplemented; see InterfaceTypeResolver.")),
),
})),
)
//
// Generate type description
//
// == Example output
//
// // ...
// var _InterfaceTypePetDesc = graphql.InterfaceDesc{
// Config: _InterfaceTypePetConfigFn,
// }
//
code.Commentf(
`describe %s's configuration; kept private to avoid unintentional tampering of configuration at runtime.`,
name,
)
code.
Var().Id(privateConfigName).Op("=").
Qual(servicePkg, "InterfaceDesc").
Values(jen.Dict{
jen.Id("Config"): jen.Id(privateConfigThunkName),
})
return code
}