/
entities.go
289 lines (250 loc) · 6.45 KB
/
entities.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package codegen
import (
"fmt"
"strconv"
"github.com/vektah/gqlparser/ast"
)
//TODO: get rid of this global variable
var enumTypes map[string]bool
func init() {
enumTypes = make(map[string]bool)
}
type Definition struct {
Fields Fields
Name string
IsEntity bool
CacheSkipDBLookup bool
ParallelSetterSteps map[int][]*ParallelStep
ParallelAggregatorSteps map[int][]*ParallelStep
}
type Enum struct {
Name string
Fields []string
}
type Field struct {
Name string
Type string
GoType string
GoInit string
Init string
Nullable bool
Array bool
Derived bool
Hidden bool
PostgresType string
PostgresIndex string
ParallelStep *ParallelStep
}
type ParallelStep struct {
Step int
Type string
Field *Field
}
type Fields []*Field
type Enums []*Enum
func (fs Fields) VisibleOnly() (out []*Field) {
for _, f := range fs {
if !f.Derived && !f.Hidden {
out = append(out, f)
}
}
return
}
func (fs Fields) InitOnly() (out []*Field) {
for _, f := range fs {
if f.GoInit != "" && !f.Derived && !f.Hidden {
out = append(out, f)
}
}
return
}
func ParseEnum(def *ast.Definition) *Enum {
if def.Kind != "ENUM" {
return nil
}
vals := make([]string, 0, len(def.EnumValues))
for _, val := range def.EnumValues {
vals = append(vals, val.Name)
}
return &Enum{
Name: def.Name,
Fields: vals,
}
}
func parseObject(def *ast.Definition) (*Definition, error) {
if def.Kind != "OBJECT" {
return nil, nil
}
fields := make([]*Field, 0, len(def.Fields))
for _, field := range def.Fields {
fieldDef, err := ParseFieldDefinition(field)
if err != nil {
return nil, fmt.Errorf("entity %q: %w", def.Name, err)
}
fields = append(fields, fieldDef)
}
out := &Definition{
Fields: fields,
Name: def.Name,
}
for _, dir := range def.Directives {
if dir.Name == "entity" {
out.IsEntity = true
}
}
if !out.IsEntity {
return out, nil
}
// this is only applied for entities
for _, dir := range def.Directives {
if dir.Name == "cache" {
for _, arg := range dir.Arguments {
switch arg.Name {
case "skip_db_lookup":
if arg.Value == nil {
return nil, fmt.Errorf("'skip_db_lookup' argument to @cache directive requires a boolean parameter")
}
val, err := strconv.ParseBool(arg.Value.Raw)
if err != nil {
return nil, fmt.Errorf("invalid bool value for 'skip_db_lookup' argument to @cache directive: %w", err)
}
if val {
out.CacheSkipDBLookup = true
}
}
}
}
}
return out, nil
}
func ParseFieldDefinition(field *ast.FieldDefinition) (*Field, error) {
f := &Field{
Name: field.Name,
Type: field.Type.Name(),
Array: bool(field.Type.Elem != nil),
Hidden: field.Name == "id", // defined in entity.Base, so not needed in our codegen
}
if field.Type.Elem != nil {
f.Nullable = !field.Type.Elem.NonNull
} else {
f.Nullable = !field.Type.NonNull
}
goType, err := f.getGoType()
if err != nil {
return nil, fmt.Errorf("invalid type %q for field %q: %w", field.Type.Name(), field.Name, err)
}
f.GoType = goType
f.GoInit = f.getGoInit()
f.PostgresType = f.getPostgresType()
f.PostgresIndex = f.getPostgresIndex()
for _, directive := range field.Directives {
if directive.Name == "sql" {
for _, arg := range directive.Arguments {
switch arg.Name {
case "index":
if arg.Value == nil {
return nil, fmt.Errorf("'index' argument to @sql directive requires a boolean param on field %s", f.Name)
}
val, err := strconv.ParseBool(arg.Value.Raw)
if err != nil {
return nil, fmt.Errorf("invalid bool value for 'index' argument to @sql directive for field %s: %w", f.Name, err)
}
if !val {
f.PostgresIndex = "" // remove sql index
}
default:
return nil, fmt.Errorf("invalid argument %q for directive @%q on field %s", arg.Name, directive.Name, f.Name)
}
}
}
if directive.Name == "derivedFrom" {
f.Derived = true
}
if directive.Name == "parallel" {
// compute any missing parallel notations, and error out if some are missing.
step, err := toParallelStep(f, directive)
if err != nil {
return nil, err
}
f.ParallelStep = step
}
}
return f, nil
}
func toParallelStep(field *Field, directive *ast.Directive) (*ParallelStep, error) {
step := &ParallelStep{
Field: field,
}
for _, arg := range directive.Arguments {
switch arg.Name {
case "step":
if arg.Value == nil {
return nil, fmt.Errorf("'step' argument to @parallel directive requires a numeric parameter in field %q", field.Name)
}
val, err := strconv.ParseInt(arg.Value.Raw, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid integer value for 'step' argument to @parallel directive: %w", err)
}
step.Step = int(val)
case "type":
if arg.Value == nil {
return nil, fmt.Errorf("'type' argument to @parallel directive requires a parameter in field %q, supported values: SUM", field.Name)
}
if arg.Value.Raw != "SUM" {
return nil, fmt.Errorf("invalid type %q for @parallel directive in field %q", arg.Value.Raw, field.Name)
}
step.Type = arg.Value.Raw
default:
return nil, fmt.Errorf("invalid @parallel directive argument %q, in field %q", arg.Name, field.Name)
}
}
return step, nil
}
func (f *Field) getGoType() (string, error) {
var dt string
if t, ok := map[string]string{
"ID": "string",
"String": "string",
"Int": "int64",
"Float": "float64",
"Boolean": "entity.Bool",
"BigInt": "entity.Int",
"BigDecimal": "entity.Float",
"Bytes": "entity.Bytes",
}[f.Type]; ok {
dt = t
} else if _, ok = enumTypes[f.Type]; ok {
dt = "entity.Enum"
}
res := ""
if f.Array {
res += "[]"
}
neverPointers := map[string]bool{
"Bytes": true,
}
if _, ok := neverPointers[f.Type]; !ok && f.Nullable && !f.Array {
res += "*"
}
if dt == "" {
if res == "[]" {
return "entity.LocalStringArray", nil
}
// FIXME: this allows us to define types like BigFloat or BigWhatever which aren't
// defined as enties.
// We should catch that at a later step, and fail when types are not part of the basic types nor defined in the GraphQL schema.
res += "string"
//return "", fmt.Errorf("%q is not a supported type in sparkle", f.Type)
} else {
res += dt
}
return res, nil
}
func (f *Field) getGoInit() string {
initFunc := map[string]string{
"entity.Int": "IL(0)",
"entity.Float": "FL(0)",
"entity.Bytes": "[]byte{}",
}[f.GoType]
return initFunc
}