-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.go
276 lines (265 loc) · 8.18 KB
/
convert.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package gentity
import (
"errors"
"fmt"
"github.com/fish-tennis/gentity/util"
"google.golang.org/protobuf/proto"
"reflect"
"strconv"
)
// reflect.Value -> interface{}
func ConvertValueToInterface(srcType, dstType reflect.Type, srcValue reflect.Value) interface{} {
switch srcType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return ConvertInterfaceToRealType(dstType, srcValue.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return ConvertInterfaceToRealType(dstType, srcValue.Uint())
case reflect.Float32, reflect.Float64:
return ConvertInterfaceToRealType(dstType, srcValue.Float())
case reflect.Complex64, reflect.Complex128:
return ConvertInterfaceToRealType(dstType, srcValue.Complex())
case reflect.String:
return ConvertInterfaceToRealType(dstType, srcValue.String())
case reflect.Bool:
return ConvertInterfaceToRealType(dstType, srcValue.Bool())
case reflect.Interface, reflect.Ptr:
return ConvertInterfaceToRealType(dstType, srcValue.Interface())
case reflect.Slice:
// dstType是proto.Message, []byte -> proto.Message
if dstType.Implements(reflect.TypeOf((*proto.Message)(nil)).Elem()) {
return ConvertInterfaceToRealType(dstType, srcValue.Bytes())
} else {
return ConvertInterfaceToRealType(dstType, srcValue.Interface())
}
default:
GetLogger().Error("unsupported type:%v", srcType.Kind())
}
return nil
}
// reflect.Value -> int
func ConvertValueToInt(srcType reflect.Type, v reflect.Value) int64 {
switch srcType.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:
return int64(v.Uint())
case reflect.Float32, reflect.Float64:
// NOTE:有精度问题
return int64(v.Float())
default:
GetLogger().Error("unsupported type:%v", srcType.Kind())
}
return 0
}
// interface{} -> int or string or proto.Message
func ConvertInterfaceToRealType(typ reflect.Type, v interface{}) interface{} {
switch typ.Kind() {
case reflect.Int:
return int(v.(int64))
case reflect.Int8:
return int8(v.(int64))
case reflect.Int16:
return int16(v.(int64))
case reflect.Int32:
return int32(v.(int64))
case reflect.Int64:
return v.(int64)
case reflect.Uint:
return uint(v.(uint64))
case reflect.Uint8:
return uint8(v.(uint64))
case reflect.Uint16:
return uint16(v.(uint64))
case reflect.Uint32:
return uint32(v.(uint64))
case reflect.Uint64:
return v.(uint64)
case reflect.Float32:
return v.(float32)
case reflect.Float64:
return v.(float64)
case reflect.Complex64:
return v.(complex64)
case reflect.Complex128:
return v.(complex128)
case reflect.String:
return v
case reflect.Bool:
return v.(bool)
case reflect.Ptr:
if bytes, ok := v.([]byte); ok {
newProto := reflect.New(typ.Elem())
if protoMessage, ok2 := newProto.Interface().(proto.Message); ok2 {
protoErr := proto.Unmarshal(bytes, protoMessage)
if protoErr != nil {
return protoErr
}
return protoMessage
}
}
if protoMessage, ok := v.(proto.Message); ok {
return protoMessage
}
case reflect.Slice:
return v
//if bytes, ok := v.([]byte); ok {
// if typ.Elem().Kind() == reflect.Uint8 {
// return v
// }
// newProto := reflect.New(typ.Elem())
// if protoMessage, ok2 := newProto.Interface().(proto.Message); ok2 {
// protoErr := proto.Unmarshal(bytes, protoMessage)
// if protoErr != nil {
// return protoErr
// }
// return protoMessage
// }
//}
}
GetLogger().Error("unsupported type:%v", typ.Kind())
return nil
}
func GetFieldValue(obj reflect.Value, fieldName string) reflect.Value {
if obj.Kind() == reflect.Ptr {
obj = obj.Elem()
}
if obj.Kind() == reflect.Map {
return obj.MapIndex(reflect.ValueOf(fieldName))
} else if obj.Kind() == reflect.Struct {
return obj.FieldByName(fieldName)
} else {
GetLogger().Error("unsupported kind:%v", obj.Kind())
}
return reflect.Value{}
}
// 支持int,float,string,[]byte,complex,bool,proto.Message
func ConvertStringToRealType(typ reflect.Type, v string) interface{} {
switch typ.Kind() {
case reflect.Int:
return util.Atoi(v)
case reflect.Int8:
return int8(util.Atoi(v))
case reflect.Int16:
return int16(util.Atoi(v))
case reflect.Int32:
return int32(util.Atoi(v))
case reflect.Int64:
return util.Atoi64(v)
case reflect.Uint:
return uint(util.Atou(v))
case reflect.Uint8:
return uint8(util.Atou(v))
case reflect.Uint16:
return uint16(util.Atou(v))
case reflect.Uint32:
return uint32(util.Atou(v))
case reflect.Uint64:
return util.Atou(v)
case reflect.Float32:
f, _ := strconv.ParseFloat(v, 32)
return float32(f)
case reflect.Float64:
f, _ := strconv.ParseFloat(v, 64)
return f
case reflect.Complex64:
c, _ := strconv.ParseComplex(v, 64)
return c
case reflect.Complex128:
c, _ := strconv.ParseComplex(v, 128)
return c
case reflect.String:
return v
case reflect.Bool:
return v == "true" || v == "1"
case reflect.Slice:
// []byte
if typ.Elem().Kind() == reflect.Uint8 {
return []byte(v)
}
case reflect.Ptr:
newProto := reflect.New(typ.Elem())
if protoMessage, ok := newProto.Interface().(proto.Message); ok {
protoErr := proto.Unmarshal([]byte(v), protoMessage)
if protoErr != nil {
GetLogger().Error("proto err:%v", protoErr.Error())
return protoErr
}
return protoMessage
}
default:
GetLogger().Error("unsupported type:%v", typ.Kind())
}
return nil
}
func convertValueToString(val reflect.Value) (string, error) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.Itoa(int(val.Int())), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(val.Uint(), 10), nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(val.Float(), 'f', 2, 64), nil
case reflect.Complex64, reflect.Complex128:
return strconv.FormatComplex(val.Complex(), 'f', 2, 128), nil
case reflect.String:
return val.String(), nil
case reflect.Interface:
if !val.CanInterface() {
GetLogger().Error("unsupport type:%v", val.Kind())
return "", errors.New(fmt.Sprintf("unsupport type:%v", val.Kind()))
}
return util.ToString(val.Interface())
default:
GetLogger().Error("unsupport type:%v", val.Kind())
return "", errors.New(fmt.Sprintf("unsupport type:%v", val.Kind()))
}
}
func convertValueToStringOrInterface(val reflect.Value) (interface{}, error) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64,
reflect.Complex64, reflect.Complex128,
reflect.String:
return convertValueToString(val)
case reflect.Interface, reflect.Ptr:
if !util.IsValueNil(val) {
if !val.CanInterface() {
GetLogger().Error("unsupport type:%v", val.Kind())
return nil, errors.New(fmt.Sprintf("unsupport type:%v", val.Kind()))
}
i := val.Interface()
// protobuf格式
if protoMessage, ok := i.(proto.Message); ok {
bytes, protoErr := proto.Marshal(protoMessage)
if protoErr != nil {
GetLogger().Error("convert proto err:%v", protoErr.Error())
return nil, protoErr
}
return bytes, nil
}
// 支持map[key]Saveable的特殊动态结构
if valueSaveable, ok := i.(Saveable); ok {
valueSaveData, valueSaveErr := GetSaveData(valueSaveable, "")
if valueSaveErr != nil {
GetLogger().Error("convert Saveable err:%v", valueSaveErr.Error())
return nil, valueSaveErr
}
return valueSaveData, nil
}
return i, nil
}
case reflect.Struct:
valInterface := convertStructToInterface(val)
if valInterface == nil {
GetLogger().Error("convertStructToInterfaceErr type:%v", val.Kind())
return nil, errors.New(fmt.Sprintf("convertStructToInterfaceErr type:%v", val.Kind()))
}
return convertValueToStringOrInterface(reflect.ValueOf(valInterface))
default:
GetLogger().Error("unsupport type:%v", val.Kind())
return nil, errors.New(fmt.Sprintf("unsupport type:%v", val.Kind()))
}
GetLogger().Error("unsupport type:%v", val.Kind())
return nil, errors.New(fmt.Sprintf("unsupport type:%v", val.Kind()))
}