forked from worldline-go/struct2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues.go
More file actions
39 lines (31 loc) · 648 Bytes
/
values.go
File metadata and controls
39 lines (31 loc) · 648 Bytes
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
package struct2
import (
"reflect"
)
// isNil return true if the given value is nil.
//
// isNil(reflect.ValueOf(x))
func isNil(v reflect.Value) bool {
if v.Kind() == reflect.Ptr {
return v.IsNil()
}
return false
}
func value2StructValue(v reflect.Value) reflect.Value {
// pointer to struct
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
if v.Kind() == reflect.Interface {
v = reflect.Indirect(v)
}
if v.IsNil() {
v = reflect.New(v.Type().Elem()).Elem()
// v = reflect.Zero(v.Type().Elem())
break
}
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic("not struct value")
}
return v
}