-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_type_api.go
163 lines (151 loc) · 3.17 KB
/
t_type_api.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package t
import (
"reflect"
)
var _ T = &Type{}
func ifNilDef(v interface{}, def interface{}) interface{} {
if v == nil {
return def
}
return v
}
func (t Type) Any(def ...interface{}) interface{} {
return ifNilDef(t.val, def)
}
func (t Type) String(def ...string) string {
return String(t.val, def...)
}
func (t Type) Float64(def ...float64) float64 {
return Float64(t.val, def...)
}
func (t Type) Float32(def ...float32) float32 {
return Float32(t.val, def...)
}
func (t Type) Int64(def ...int64) int64 {
return Int64(t.val, def...)
}
func (t Type) Int(def ...int) int {
return Int(t.val, def...)
}
func (t Type) Int32(def ...int32) int32 {
return Int32(t.val, def...)
}
func (t Type) Int16(def ...int16) int16 {
return Int16(t.val, def...)
}
func (t Type) Int8(def ...int8) int8 {
return Int8(t.val, def...)
}
func (t Type) Uint64(def ...uint64) uint64 {
return Uint64(t.val, def...)
}
func (t Type) Uint(def ...uint) uint {
return Uint(t.val, def...)
}
func (t Type) Uint32(def ...uint32) uint32 {
return Uint32(t.val, def...)
}
func (t Type) Uint16(def ...uint16) uint16 {
return Uint16(t.val, def...)
}
func (t Type) Uint8(def ...uint8) uint8 {
return Uint8(t.val, def...)
}
func (t Type) Bool(def ...bool) bool {
return Bool(t.val, def...)
}
// 将数据转转换成其他类型
func (t Type) ToType(typ reflect.Type) interface{} {
return valToType(t, typ)
}
func any2anyPointer(v interface{}) interface{} {
pv := reflect.New(reflect.TypeOf(v))
pv.Elem().Set(reflect.ValueOf(v))
return pv.Interface()
}
func valToType(val T, toType reflect.Type) (valValue interface{}) {
switch toType.Kind() {
case reflect.String:
valValue = val.String()
break
case reflect.Bool:
valValue = val.Bool()
break
case reflect.Float32:
valValue = val.Float32()
break
case reflect.Float64:
valValue = val.Float64()
break
case reflect.Int:
valValue = val.Int()
break
case reflect.Int8:
valValue = val.Int8()
break
case reflect.Int16:
valValue = val.Int16()
break
case reflect.Int32:
valValue = val.Int32()
break
case reflect.Int64:
valValue = val.Int64()
break
case reflect.Uint:
valValue = val.Uint()
break
case reflect.Uint8:
valValue = val.Uint8()
break
case reflect.Uint16:
valValue = val.Uint16()
break
case reflect.Uint32:
valValue = val.Uint32()
break
case reflect.Uint64:
valValue = val.Uint64()
break
case reflect.Complex64:
if c1, ok := val.Any().(complex64); ok {
valValue = c1
} else {
valValue = complex64(0)
}
break
case reflect.Complex128:
if c1, ok := val.Any().(complex128); ok {
valValue = c1
} else {
valValue = complex128(0)
}
break
case reflect.Interface:
valValue = val.Any()
break
case reflect.Slice:
valValue = val.ToSliceType(toType)
break
case reflect.Map:
valValue = val.ToMapType(toType)
break
case reflect.Struct:
valValue = val.ToStructType(toType)
break
case reflect.Ptr:
ptrElemType := toType.Elem()
for ptrElemType.Kind() == reflect.Ptr {
ptrElemType = ptrElemType.Elem()
}
if ptrElemType.Kind() == reflect.Struct {
valValue = val.ToStructType(toType)
} else {
valValue = valToType(val, ptrElemType)
}
break
default:
return val.Any()
}
return
}