forked from ByteStorage/FlyDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
172 lines (163 loc) · 3.93 KB
/
types.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
package structure
import (
"errors"
"strconv"
)
type DataStructure = byte
const (
// String is a string data structure
String DataStructure = iota + 1
// Hash is a hash data structure
Hash
// List is a list data structure
List
// Set is a set data structure
Set
// ZSet is a zset data structure
ZSet
// bitmap is a bitmap data structure
Bitmap
// Stream is a stream data structure
Stream
// Expire is a expire data structure
Expire
)
// interfaceToBytes converts an interface to a byte slice
func interfaceToBytes(value interface{}) ([]byte, error, string) {
switch value := value.(type) {
case string:
return []byte(value), nil, "string"
case int:
return []byte(strconv.Itoa(value)), nil, "int"
case int64:
return []byte(strconv.FormatInt(value, 10)), nil, "int64"
case float64:
return []byte(strconv.FormatFloat(value, 'f', -1, 64)), nil, "float64"
case bool:
return []byte(strconv.FormatBool(value)), nil, "bool"
case []byte:
return value, nil, "[]byte"
default:
return nil, errors.New("unsupported type"), ""
}
}
//func byteToInterface(value []byte, dataType string) (interface{}, error) {
// switch dataType {
// case "string":
// return string(value), nil
// case "int":
// return strconv.Atoi(string(value))
// case "int64":
// return strconv.ParseInt(string(value), 10, 64)
// case "float64":
// return strconv.ParseFloat(string(value), 64)
// case "bool":
// return strconv.ParseBool(string(value))
// case "[]byte":
// return value, nil
// default:
// return nil, errors.New("unsupported type")
// }
//}
func byteToInterface(value []byte, dataType string) (interface{}, error) {
switch dataType {
case "string":
return string(value), nil
case "int":
intValue, err := strconv.Atoi(string(value))
if err != nil {
return nil, errors.New("cannot convert to int: " + err.Error())
}
return intValue, nil
case "int64":
int64Value, err := strconv.ParseInt(string(value), 10, 64)
if err != nil {
return nil, errors.New("cannot convert to int64: " + err.Error())
}
return int64Value, nil
case "float64":
float64Value, err := strconv.ParseFloat(string(value), 64)
if err != nil {
return nil, errors.New("cannot convert to float64: " + err.Error())
}
return float64Value, nil
case "bool":
boolValue, err := strconv.ParseBool(string(value))
if err != nil {
return nil, errors.New("cannot convert to bool: " + err.Error())
}
return boolValue, nil
case "[]byte":
return value, nil
default:
return nil, errors.New("unsupported type")
}
}
// convertToInt converts an interface to an int
func convertToInt(Value interface{}) (int, error) {
switch value := Value.(type) {
case int:
return value, nil
case int32:
return int(value), nil
case int64:
return int(value), nil
case float32:
return int(value), nil
case float64:
return int(value), nil
case uint:
return int(value), nil
case uint8:
return int(value), nil
case uint16:
return int(value), nil
case uint32:
return int(value), nil
case uint64:
return int(value), nil
case string:
intValue, err := strconv.Atoi(value)
if err != nil {
return 0, err
}
return intValue, nil
case []byte:
strValue := string(value)
intValue, err := strconv.Atoi(strValue)
if err != nil {
return 0, err
}
return intValue, nil
default:
return 0, errors.New("unsupported type")
}
}
// convertToFloat converts an interface to a float64
func convertToFloat(Value interface{}) (float64, error) {
switch value := Value.(type) {
case int:
return float64(value), nil
case uint:
return float64(value), nil
case float32:
return float64(value), nil
case float64:
return value, nil
case string:
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, err
}
return floatValue, nil
case []byte:
strValue := string(value)
floatValue, err := strconv.ParseFloat(strValue, 64)
if err != nil {
return 0, err
}
return floatValue, nil
default:
return 0, errors.New("unsupported type")
}
}