-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
364 lines (280 loc) · 6.64 KB
/
object.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package gq
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/aacebo/gq/query"
)
type Object[T any] struct {
Name string `json:"name"` // must be unique
Description string `json:"description,omitempty"`
Use []Middleware `json:"-"`
Fields Fields `json:"fields,omitempty"`
}
func (self Object[T]) Key() string {
return self.Name
}
func (self Object[T]) Do(params *DoParams) Result {
parser := query.Parser([]byte(params.Query))
query, err := parser.Parse()
if err != nil {
return Result{Error: err}
}
return self.Resolve(&ResolveParams{
Query: query,
Key: self.Name,
Value: params.Value,
Context: params.Context,
})
}
func (self Object[T]) Resolve(params *ResolveParams) Result {
res := Result{Meta: Meta{}}
routes := []Middleware{}
if self.Use != nil {
for _, route := range self.Use {
routes = append(routes, route)
}
}
routes = append(routes, self.resolve)
var next Resolver
i := -1
next = func(params *ResolveParams) Result {
i++
if i > (len(routes) - 1) {
return Result{}
}
return routes[i](params, next)
}
result := next(&ResolveParams{
Query: params.Query,
Parent: params.Parent,
Key: "fields",
Value: params.Value,
Context: params.Context,
})
if result.Error != nil {
res.Error = NewEmptyError(params.Key).Add(result.Error)
return res
}
res.Meta = result.Meta
res.Data = result.Data
return res
}
func (self Object[T]) Extend(schema Object[T]) Object[T] {
fields := Fields{}
for key, value := range self.Fields {
fields[key] = value
}
if schema.Fields != nil {
for key, value := range schema.Fields {
fields[key] = value
}
}
middleware := []Middleware{}
if self.Use != nil {
for _, use := range self.Use {
middleware = append(middleware, use)
}
}
if schema.Use != nil {
for _, use := range schema.Use {
middleware = append(middleware, use)
}
}
return Object[T]{
Name: schema.Name,
Description: schema.Description,
Use: middleware,
Fields: fields,
}
}
func (self Object[T]) String() string {
b, _ := json.Marshal(self)
return string(b)
}
func (self Object[T]) resolve(params *ResolveParams, _ Resolver) Result {
res := Result{}
if params.Value == nil || self.Fields == nil {
return res
}
err := NewEmptyError(params.Key)
object := reflect.Indirect(reflect.New(reflect.TypeFor[T]()))
if object.Kind() == reflect.Pointer {
object = reflect.New(object.Type().Elem())
}
visited := map[string]bool{}
var resolve func(key string) error
resolve = func(key string) error {
if visited[key] {
return nil
}
query := params.Query.Fields[key]
field, exists := self.Fields[key]
if !exists {
return NewError(key, "field not found")
}
if field.DependsOn != nil {
for _, dependsOn := range field.DependsOn {
e := resolve(dependsOn)
if e != nil {
return e
}
}
}
result := field.Resolve(&ResolveParams{
Query: query,
Parent: object.Interface(),
Key: key,
Value: self.getKey(key, reflect.ValueOf(params.Value)),
Context: params.Context,
})
if result.Error != nil {
return result.Error
}
if e := self.setKey(key, result.Data, object); e != nil {
return e
}
if result.Meta != nil && !result.Meta.Empty() {
res.SetMeta(key, result.Meta)
}
visited[key] = true
return nil
}
for key := range params.Query.Fields {
e := resolve(key)
if e != nil {
err = err.Add(e)
}
}
if len(err.Errors) > 0 {
res.Error = err
return res
}
if res.Meta != nil && res.Meta.Empty() {
res.Meta = nil
}
res.Data = object.Interface()
return res
}
func (self Object[T]) getKey(key string, value reflect.Value) any {
for value.Kind() == reflect.Pointer {
value = value.Elem()
}
if !value.IsValid() {
return nil
}
if value.Kind() == reflect.Map {
return self.getMapKey(key, value)
}
return self.getStructKey(key, value)
}
func (self Object[T]) setKey(key string, val any, value reflect.Value) error {
for value.Kind() == reflect.Pointer {
value = value.Elem()
}
if !value.IsValid() {
return nil
}
if value.Kind() == reflect.Interface {
value = value.Elem()
}
if value.Kind() == reflect.Map {
return self.setMapKey(key, val, value)
}
return self.setStructKey(key, val, value)
}
func (self Object[T]) getMapKey(key string, object reflect.Value) any {
value := reflect.Indirect(object.MapIndex(reflect.ValueOf(key)))
if value.Kind() == reflect.Interface {
value = value.Elem()
}
if value.IsValid() && value.CanInterface() {
return value.Interface()
}
return nil
}
func (self Object[T]) setMapKey(key string, val any, object reflect.Value) error {
value := reflect.ValueOf(val)
if object.CanSet() && object.IsNil() {
object.Set(reflect.MakeMapWithSize(reflect.TypeFor[T](), 0))
}
if object.Type().Elem() != value.Type() && value.CanConvert(object.Type().Elem()) {
value = value.Convert(object.Type().Elem())
}
if object.Type().Elem() != value.Type() {
return NewError(
key,
fmt.Sprintf(
"expected type '%s', received '%s'",
object.Type().Elem().String(),
value.Type().String(),
),
)
}
object.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(val))
return nil
}
func (self Object[T]) getStructKey(key string, object reflect.Value) any {
name, exists := self.getStructFieldByName(key, object)
if !exists {
return nil
}
value := object.FieldByName(name)
if value.Kind() == reflect.Interface {
value = value.Elem()
}
if value.IsValid() && value.CanInterface() {
return value.Interface()
}
return nil
}
func (self Object[T]) setStructKey(key string, val any, object reflect.Value) error {
name, exists := self.getStructFieldByName(key, object)
if !exists {
return NewError(key, "struct field not found")
}
value := object.FieldByName(name)
if !reflect.ValueOf(val).IsValid() {
if value.CanSet() {
value.Set(reflect.New(value.Type()).Elem())
}
return nil
}
if value.Type() != reflect.ValueOf(val).Type() {
return NewError(
key,
fmt.Sprintf(
"expected type '%s', received '%s'",
value.Type().String(),
reflect.ValueOf(val).Type().String(),
),
)
}
if value.CanSet() {
value.Set(reflect.ValueOf(val))
}
return nil
}
func (self Object[T]) getStructFieldByName(name string, object reflect.Value) (string, bool) {
if !object.IsValid() {
return "", false
}
for i := 0; i < object.Type().NumField(); i++ {
field := object.Type().Field(i)
tag := field.Tag.Get("json")
if tag == "" {
tag = field.Name
}
if i := strings.Index(tag, ","); i > -1 {
tag = tag[:i]
}
if tag == "" || tag == "-" {
continue
}
if tag == name {
return field.Name, true
}
}
return "", false
}