-
Notifications
You must be signed in to change notification settings - Fork 2
/
elem.go
64 lines (58 loc) · 1.44 KB
/
elem.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
package reflecthelper
import "reflect"
// GetElem gets the elem of the pointer val without initialize the pointer val.
// GetElem is similar to GetInitElem but without initialization.
func GetElem(val reflect.Value) (res reflect.Value) {
res = val
if !IsValueElemable(res) {
return
}
if res.IsNil() {
return
}
res = res.Elem()
return
}
// GetNilElem is similar with GetElem but it doesn't check if reflect.Ptr or reflect.Interface is nil.
func GetNilElem(val reflect.Value) (res reflect.Value) {
res = val
if !IsValueElemable(res) {
return
}
res = res.Elem()
return
}
// GetChildElem is similar with GetInitChildElem but without initialize the child elem.
func GetChildElem(val reflect.Value) (res reflect.Value) {
res = val
var tempRes reflect.Value
for IsValueElemable(res) {
tempRes = GetElem(res)
if res == tempRes {
return
}
res = tempRes
}
return
}
// GetChildPtrElem is similar with GetChildElem but the function stops when the elem is ptr and the elem of that ptr is non ptr.
func GetChildPtrElem(val reflect.Value) (res reflect.Value) {
res = val
var tempRes reflect.Value
for IsValueElemableParentElem(res) {
tempRes = GetElem(res)
if res == tempRes {
return
}
res = tempRes
}
return
}
// GetChildNilElem is similar with GetChildElem but it uses GetNilElem function.
func GetChildNilElem(val reflect.Value) (res reflect.Value) {
res = val
for IsValueElemable(res) {
res = GetNilElem(res)
}
return
}