-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
44 lines (39 loc) · 900 Bytes
/
utils.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
package srm
import (
"github.com/CloudyKit/framework/validation"
"reflect"
)
var modelTYPE = reflect.TypeOf((*IModel)(nil)).Elem()
func validModelSlice(v reflect.Type) bool {
kind := v.Kind()
if kind == reflect.Slice || kind == reflect.Array {
v = v.Elem()
if v.Implements(modelTYPE) {
return true
}
if reflect.PtrTo(v).Implements(modelTYPE) {
return true
}
}
return false
}
func canBeNil(r reflect.Kind) bool {
return r > reflect.Array && r < reflect.String || r == reflect.UnsafePointer
}
func getRefModel(v reflect.Value) (reflect.Value, bool) {
t := v.Type()
ok := t.Implements(modelTYPE)
if !ok {
kind := t.Kind()
if kind == reflect.Struct && reflect.PtrTo(t).Implements(modelTYPE) {
if v.CanAddr() {
v = v.Addr()
ok = true
}
} else if kind == reflect.Ptr && t.Elem().Implements(modelTYPE) {
v = v.Elem()
ok = true
}
}
return v, ok
}