-
Notifications
You must be signed in to change notification settings - Fork 4
/
xcall.go
49 lines (44 loc) · 1.22 KB
/
xcall.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
package reflectx
import (
"reflect"
)
func FieldByIndexX(v reflect.Value, index []int) reflect.Value {
if len(index) == 1 {
return FieldX(v, index[0])
}
mustBe("reflect.Value.FieldByIndex", v, reflect.Struct)
for i, x := range index {
if i > 0 {
if v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct {
if v.IsNil() {
panic("reflect: indirection through nil pointer to embedded struct")
}
v = v.Elem()
}
}
v = FieldX(v, x)
}
return v
}
func mustBe(method string, v reflect.Value, kind reflect.Kind) {
if v.Kind() != kind {
panic(&reflect.ValueError{method, v.Kind()})
}
}
func FieldByNameX(v reflect.Value, name string) reflect.Value {
mustBe("reflect.Value.FieldByName", v, reflect.Struct)
if f, ok := v.Type().FieldByName(name); ok {
return FieldByIndexX(v, f.Index)
}
return reflect.Value{}
}
// FieldByNameFunc returns the struct field with a name
// that satisfies the match function.
// It panics if v's Kind is not struct.
// It returns the zero Value if no field was found.
func FieldByNameFuncX(v reflect.Value, match func(string) bool) reflect.Value {
if f, ok := v.Type().FieldByNameFunc(match); ok {
return FieldByIndexX(v, f.Index)
}
return reflect.Value{}
}