-
Notifications
You must be signed in to change notification settings - Fork 31
/
util.go
453 lines (381 loc) · 12.1 KB
/
util.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (c) 2022 MindStand Technologies, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gogm
import (
"errors"
"fmt"
"reflect"
"strings"
"sync"
dsl "github.com/mindstand/go-cypherdsl"
)
// checks if integer is in slice
func int64SliceContains(s []int64, e int64) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func ptrToBool(b bool) *bool {
return &b
}
// sets uuid for stuct if uuid field is empty
func handleNodeState(pkStrat *PrimaryKeyStrategy, val *reflect.Value) (isNew bool, id int64, relConfig map[string]*RelationConfig, err error) {
if val == nil {
return false, -1, nil, errors.New("value can not be nil")
}
if pkStrat == nil {
return false, -1, nil, errors.New("pk strategy can not be nil")
}
if reflect.TypeOf(*val).Kind() == reflect.Ptr {
*val = val.Elem()
}
loadVal := reflect.Indirect(*val).FieldByName("LoadMap")
iConf := loadVal.Interface()
var loadMap map[string]*RelationConfig
var ok bool
if iConf != nil && loadVal.Len() != 0 {
loadMap, ok = iConf.(map[string]*RelationConfig)
if !ok {
return false, -1, nil, fmt.Errorf("unable to cast conf to [map[string]*RelationConfig], %w", ErrInternal)
}
}
// handle the id
if val.IsNil() {
isNew = true
} else {
idVal := reflect.Indirect(*val).FieldByName(DefaultPrimaryKeyStrategy.FieldName)
// idVal is a pointer
idVal = idVal.Elem()
if idVal.IsValid() {
id = idVal.Int()
isNew = false
} else {
isNew = true
}
}
// using a pk strategy on top of default graph ids
if pkStrat.StrategyName != DefaultPrimaryKeyStrategy.StrategyName {
checkId := reflect.Indirect(*val).FieldByName(pkStrat.FieldName)
if !checkId.IsZero() && !isNew {
return false, id, loadMap, nil
} else {
// if id was not set by user, gen new id and set it
if checkId.IsZero() {
reflect.Indirect(*val).FieldByName(pkStrat.FieldName).Set(reflect.ValueOf(pkStrat.GenIDFunc()))
}
return true, -1, loadMap, nil
}
} else {
return isNew, id, loadMap, nil
}
}
// gets the type name from reflect type
func getTypeName(val reflect.Type) (string, error) {
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() == reflect.Slice {
val = val.Elem()
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
}
if val.Kind() == reflect.Struct {
return val.Name(), nil
} else {
return "", fmt.Errorf("can not take name from kind {%s)", val.Kind().String())
}
}
// converts struct fields to map that cypher can use
func toCypherParamsMap(gogm *Gogm, val reflect.Value, config structDecoratorConfig) (map[string]interface{}, error) {
var err error
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
if val.Type().Kind() == reflect.Interface || val.Type().Kind() == reflect.Ptr {
val = val.Elem()
}
ret := map[string]interface{}{}
for _, conf := range config.Fields {
if conf.Relationship != "" || conf.Name == "id" || conf.Ignore {
continue
}
field := val.FieldByName(conf.FieldName)
if conf.Properties {
//check if field is a map
if conf.Type.Kind() == reflect.Map && field.Kind() == reflect.Map {
for _, e := range field.MapKeys() {
v := field.MapIndex(e)
es := e.Interface().(string)
ret[conf.Name+"."+es] = v.Interface()
}
} else if conf.Type.Kind() == reflect.Slice && field.Kind() == reflect.Slice {
ret[conf.Name] = field.Interface()
} else {
return nil, fmt.Errorf("properties type is not a map or slice, %T", field.Interface())
}
} else {
var val interface{}
//check if field is type aliased
if conf.IsTypeDef {
val = field.Convert(conf.TypedefActual).Interface()
} else {
val = field.Interface()
}
if conf.PrimaryKey != "" {
if conf.PrimaryKey == DefaultPrimaryKeyStrategy.StrategyName {
// we dont want to write the id to the params map
continue
}
ret[gogm.pkStrategy.DBName] = val
} else {
ret[conf.Name] = val
}
}
}
return ret, err
}
type relationConfigs struct {
// [type-relationship][fieldType][]decoratorConfig
configs map[string]map[string][]decoratorConfig
mutex sync.Mutex
}
func (r *relationConfigs) getKey(nodeType, relationship string) string {
return fmt.Sprintf("%s-%s", nodeType, relationship)
}
func (r *relationConfigs) Add(nodeType, relationship, fieldType string, dec decoratorConfig) {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.configs == nil {
r.configs = map[string]map[string][]decoratorConfig{}
}
key := r.getKey(nodeType, relationship)
if _, ok := r.configs[key]; !ok {
r.configs[key] = map[string][]decoratorConfig{}
}
if _, ok := r.configs[key][fieldType]; !ok {
r.configs[key][fieldType] = []decoratorConfig{}
}
//log.Debugf("mapped relations [%s][%s][%v]", key, fieldType, len(r.configs[key][fieldType]))
r.configs[key][fieldType] = append(r.configs[key][fieldType], dec)
}
func (r *relationConfigs) GetConfigs(startNodeType, startNodeFieldType, endNodeType, endNodeFieldType, relationship string) (start, end *decoratorConfig, err error) {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.configs == nil {
return nil, nil, errors.New("no configs provided")
}
start, err = r.getConfig(startNodeType, relationship, startNodeFieldType, dsl.DirectionOutgoing)
if err != nil {
return nil, nil, err
}
end, err = r.getConfig(endNodeType, relationship, endNodeFieldType, dsl.DirectionIncoming)
if err != nil {
return nil, nil, err
}
return start, end, nil
}
func (r *relationConfigs) getConfig(nodeType, relationship, fieldType string, direction dsl.Direction) (*decoratorConfig, error) {
if r.configs == nil {
return nil, errors.New("no configs provided")
}
key := r.getKey(nodeType, relationship)
if _, ok := r.configs[key]; !ok {
return nil, fmt.Errorf("no configs for key [%s]", key)
}
var ok bool
var confs []decoratorConfig
if confs, ok = r.configs[key][fieldType]; !ok {
return nil, fmt.Errorf("no configs for key [%s] and field type [%s]", key, fieldType)
}
if len(confs) == 1 {
return &confs[0], nil
} else if len(confs) > 1 {
for _, c := range confs {
if c.Direction == direction {
return &c, nil
}
}
return nil, errors.New("relation with correct direction not found")
} else {
return nil, fmt.Errorf("config not found, %w", ErrInternal)
}
}
type validation struct {
Incoming []string
Outgoing []string
None []string
Both []string
BothSelf []string
}
func (r *relationConfigs) Validate() error {
r.mutex.Lock()
defer r.mutex.Unlock()
checkMap := map[string]*validation{}
for title, confMap := range r.configs {
parts := strings.Split(title, "-")
if len(parts) != 2 {
return fmt.Errorf("invalid length for parts [%v] should be 2. Rel is [%s], %w", len(parts), title, ErrValidation)
}
// vType := parts[0]
relType := parts[1]
for field, configs := range confMap {
for _, config := range configs {
if _, ok := checkMap[relType]; !ok {
checkMap[relType] = &validation{
Incoming: []string{},
Outgoing: []string{},
None: []string{},
Both: []string{},
BothSelf: []string{},
}
}
validate := checkMap[relType]
switch config.Direction {
case dsl.DirectionIncoming:
validate.Incoming = append(validate.Incoming, field)
case dsl.DirectionOutgoing:
validate.Outgoing = append(validate.Outgoing, field)
case dsl.DirectionNone:
validate.None = append(validate.None, field)
case dsl.DirectionBoth:
if field == config.ParentType.Name() {
validate.BothSelf = append(validate.BothSelf, field)
} else {
validate.Both = append(validate.Both, field)
}
default:
return fmt.Errorf("unrecognized direction [%s], %w", config.Direction.ToString(), ErrValidation)
}
}
}
}
for relType, validateConfig := range checkMap {
//check normal
if len(validateConfig.Outgoing) != len(validateConfig.Incoming) {
return fmt.Errorf("invalid directional configuration on relationship [%s], %w", relType, ErrValidation)
}
//check both direction
if len(validateConfig.Both) != 0 {
if len(validateConfig.Both)%2 != 0 {
return fmt.Errorf("invalid length for 'both' validation on relationship [%s], %w", relType, ErrValidation)
}
}
//check none direction
if len(validateConfig.None) != 0 {
if len(validateConfig.None)%2 != 0 {
return fmt.Errorf("invalid length for 'none' validation on relationship [%s], %w", relType, ErrValidation)
}
}
}
return nil
}
//isDifferentType, differentType, error
func getActualTypeIfAliased(iType reflect.Type) (bool, reflect.Type, error) {
if iType == nil {
return false, nil, errors.New("iType can not be nil")
}
if iType.Kind() == reflect.Ptr {
iType = iType.Elem()
}
//check if its a struct or an interface, we can skip that
if iType.Kind() == reflect.Struct || iType.Kind() == reflect.Interface || iType.Kind() == reflect.Slice || iType.Kind() == reflect.Map {
return false, nil, nil
}
//type is the same as the kind
if iType.Kind().String() == iType.Name() {
return false, nil, nil
}
actualType, err := getPrimitiveType(iType.Kind())
if err != nil {
return false, nil, err
}
return true, actualType, nil
}
func getPrimitiveType(k reflect.Kind) (reflect.Type, error) {
switch k {
case reflect.Int:
return reflect.TypeOf(0), nil
case reflect.Int64:
return reflect.TypeOf(int64(0)), nil
case reflect.Int32:
return reflect.TypeOf(int32(0)), nil
case reflect.Int16:
return reflect.TypeOf(int16(0)), nil
case reflect.Int8:
return reflect.TypeOf(int8(0)), nil
case reflect.Uint64:
return reflect.TypeOf(uint64(0)), nil
case reflect.Uint32:
return reflect.TypeOf(uint32(0)), nil
case reflect.Uint16:
return reflect.TypeOf(uint16(0)), nil
case reflect.Uint8:
return reflect.TypeOf(uint8(0)), nil
case reflect.Uint:
return reflect.TypeOf(uint(0)), nil
case reflect.Bool:
return reflect.TypeOf(false), nil
case reflect.Float64:
return reflect.TypeOf(float64(0)), nil
case reflect.Float32:
return reflect.TypeOf(float32(0)), nil
case reflect.String:
return reflect.TypeOf(""), nil
default:
return nil, fmt.Errorf("[%s] not supported", k.String())
}
}
func int64Ptr(n int64) *int64 {
return &n
}
// traverseRelType finds the label of a node from a relationship (decoratorConfig).
// if a special edge is passed in, the linked node's label is returned.
func traverseRelType(endType reflect.Type, direction dsl.Direction) (string, error) {
if !reflect.PtrTo(endType).Implements(edgeType) {
return endType.Name(), nil
}
endVal := reflect.New(endType)
var endTypeVal []reflect.Value
if direction == dsl.DirectionOutgoing {
endTypeVal = endVal.MethodByName("GetEndNodeType").Call(nil)
} else {
endTypeVal = endVal.MethodByName("GetStartNodeType").Call(nil)
}
if len(endTypeVal) != 1 {
return "", errors.New("GetEndNodeType failed")
}
if endTypeVal[0].IsNil() {
return "", errors.New("GetEndNodeType() can not return a nil value")
}
convertedType, ok := endTypeVal[0].Interface().(reflect.Type)
if !ok {
return "", errors.New("cannot convert to type reflect.Type")
}
if convertedType.Kind() == reflect.Ptr {
return convertedType.Elem().Name(), nil
} else {
return convertedType.Name(), nil
}
}