-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
69 lines (63 loc) · 1.31 KB
/
fields.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
package mptt
import (
"context"
"gorm.io/gorm/schema"
"reflect"
)
// KeyField mptt has id parent_id tree_id left right level fields
// id parent_id tree_id only support positive integer or string
// left right level only support positive integer
type KeyField struct {
*schema.Field
Attr string
}
// setFieldValue ignore error
func setFieldValue(n interface{}, field KeyField, value interface{}) {
ctx := context.Background()
_ = field.Set(ctx, reflect.ValueOf(n), value)
}
func getIntFieldValue(n interface{}, field KeyField) int {
ctx := context.Background()
v, _ := field.ValueOf(ctx, reflect.ValueOf(n))
switch data := v.(type) {
case int64:
return int(data)
case int:
return data
case int8:
return int(data)
case int16:
return int(data)
case int32:
return int(data)
case uint:
return int(data)
case uint8:
return int(data)
case uint16:
return int(data)
case uint32:
return int(data)
case uint64:
return int(data)
case float32:
return int(data)
case float64:
return int(data)
default:
return 0
}
}
func getFieldValue(n interface{}, field KeyField) interface{} {
ctx := context.Background()
v, _ := field.ValueOf(ctx, reflect.ValueOf(n))
return v
}
type KeyFields struct {
ID KeyField
Parent KeyField
Tree KeyField
Left KeyField
Right KeyField
Level KeyField
}