-
Notifications
You must be signed in to change notification settings - Fork 1
/
reflect.go
57 lines (52 loc) · 1.46 KB
/
reflect.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
package viot
import (
"reflect"
"fmt"
)
func typeSelect(v reflect.Value) interface{} {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint()
case reflect.Float32, reflect.Float64:
return v.Float()
case reflect.Bool:
return v.Bool()
case reflect.Complex64, reflect.Complex128:
return v.Complex()
case reflect.Invalid:
return nil
case reflect.String:
return v.String()
case reflect.UnsafePointer:
return v.Pointer()
case reflect.Slice, reflect.Array:
if v.CanInterface() {
return v.Interface()
}
l := v.Len()
c := v.Cap()
vet := reflect.SliceOf(v.Elem().Type())
cv := reflect.MakeSlice(vet, l, c)
for i:=0; i<l; i++ {
cv = reflect.Append(cv, reflect.ValueOf(typeSelect(v.Index(i))))
}
return cv.Interface()
default:
//Interface
//Map
//Struct
//Chan
//Func
//Ptr
if v.CanInterface() {
return v.Interface()
}
}
panic(fmt.Errorf("viot: 该类型 %s,无法转换为 interface 类型", v.Kind()))
}
func inDirect(v reflect.Value) reflect.Value {
for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {}
return v
}