-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueunit.go
288 lines (274 loc) · 6.51 KB
/
valueunit.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
277
278
279
280
281
282
283
284
285
286
287
288
package xstorage
import (
"encoding/json"
"strconv"
"strings"
)
// TODO: 考虑用泛型改写一个v2的
// ValueUnit 加入类型,用于在非反射的情况下直接处理类型
type ValueUnit struct {
Type ValueType
Data interface{}
dirty bool
}
func (v *ValueUnit) Reset() {
*v = ValueUnit{}
}
// ToBase 直接转换为基础类型,一方面是为了避免频繁的类型转换,另一方面是为了限制类型
func ToBase[T IValueType](unit *ValueUnit) T {
// 判断类型是否正确
if unit == nil {
var empty T
return empty
}
t, ok := unit.Data.(T)
if !ok {
var empty T
return empty
}
return t
}
// UnitToJStruct 直接转换为结构体
func UnitToJStruct[T any](unit *ValueUnit) *T {
s := ToBase[string](unit)
v := new(T)
err := json.Unmarshal([]byte(s), &v)
if err != nil {
return v
}
return v
}
func ToBaseF[T IValueType](unit *ValueUnit, err error) T {
if err != nil {
var empty T
return empty
}
return ToBase[T](unit)
}
func ToUnit[T IValueType](value T, valueType ValueType) *ValueUnit {
return &ValueUnit{
Type: valueType,
Data: value,
dirty: false,
}
}
func JStructToUnit[T any](value *T) *ValueUnit {
s, err := json.Marshal(value)
if err != nil {
return nil
}
return ToUnit(string(s), ValueTypeString)
}
// StringToUnit 将json字符串转为ValueUnit
func StringToUnit(value string, valueType ValueType) *ValueUnit {
switch valueType {
case ValueTypeString:
var v string
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
case ValueTypeInt:
var v int
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
case ValueTypeFloat:
var v float32
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
case ValueTypeBool:
var v bool
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
case ValueTypeSliceInt:
var v []int
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
case ValueTypeSliceString:
var v []string
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
case ValueTypeSliceFloat:
var v []float32
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
case ValueTypeSliceBool:
var v []bool
err := json.Unmarshal([]byte(value), &v)
if err != nil {
return nil
}
return ToUnit(v, valueType)
default:
return nil
}
}
func UnitToString(unit *ValueUnit) string {
s := ""
switch unit.Type {
case ValueTypeString:
s = ToBase[string](unit)
case ValueTypeInt:
s = strconv.Itoa(ToBase[int](unit))
case ValueTypeFloat:
s = strconv.FormatFloat(float64(ToBase[float32](unit)), 'f', -1, 32)
// 如果不含小数点,就加上小数点
if !strings.Contains(s, ".") {
s += ".0"
}
case ValueTypeBool:
s = strconv.FormatBool(ToBase[bool](unit))
}
return ""
}
func Compare(unit1 *ValueUnit, unit2 *ValueUnit) bool {
if unit1 == nil || unit2 == nil {
return true
}
if unit1 == nil || unit2 == nil {
return false
}
if unit1.Type != unit2.Type {
return false
}
switch unit1.Type {
case ValueTypeString:
if ToBase[string](unit1) != ToBase[string](unit2) {
return false
}
case ValueTypeInt:
if ToBase[int](unit1) != ToBase[int](unit2) {
return false
}
case ValueTypeFloat:
if ToBase[float32](unit1) != ToBase[float32](unit2) {
return false
}
case ValueTypeBool:
if ToBase[bool](unit1) != ToBase[bool](unit2) {
return false
}
case ValueTypeSliceInt:
if len(ToBase[[]int](unit1)) != len(ToBase[[]int](unit2)) {
return false
}
for i, val := range ToBase[[]int](unit1) {
if val != ToBase[[]int](unit2)[i] {
return false
}
}
case ValueTypeSliceString:
if len(ToBase[[]string](unit1)) != len(ToBase[[]string](unit2)) {
return false
}
for i, val := range ToBase[[]string](unit1) {
if val != ToBase[[]string](unit2)[i] {
return false
}
}
case ValueTypeSliceFloat:
if len(ToBase[[]float32](unit1)) != len(ToBase[[]float32](unit2)) {
return false
}
for i, val := range ToBase[[]float32](unit1) {
if val != ToBase[[]float32](unit2)[i] {
return false
}
}
case ValueTypeSliceBool:
if len(ToBase[[]bool](unit1)) != len(ToBase[[]bool](unit2)) {
return false
}
for i, val := range ToBase[[]bool](unit1) {
if val != ToBase[[]bool](unit2)[i] {
return false
}
}
}
return true
}
func Copy(srcValue *ValueUnit, newValue *ValueUnit) {
switch srcValue.Type {
case ValueTypeString, ValueTypeInt, ValueTypeFloat, ValueTypeBool:
*newValue = *srcValue
case ValueTypeSliceInt:
newValue.Type = ValueTypeSliceInt
_, ok := srcValue.Data.([]int)
var newData []int
if !ok {
// 将类型强制转换为[]int
newData = make([]int, len(srcValue.Data.([]interface{})))
for i, val := range srcValue.Data.([]interface{}) {
newData[i] = int(val.(int64))
}
} else {
newData = make([]int, len(srcValue.Data.([]int)))
copy(newData, srcValue.Data.([]int))
}
newValue.Data = newData
case ValueTypeSliceString:
newValue.Type = ValueTypeSliceString
_, ok := srcValue.Data.([]string)
var newData []string
if !ok {
// 将类型强制转换为[]string
newData = make([]string, len(srcValue.Data.([]interface{})))
for i, val := range srcValue.Data.([]interface{}) {
newData[i] = val.(string)
}
} else {
newData = make([]string, len(srcValue.Data.([]string)))
copy(newData, srcValue.Data.([]string))
}
newValue.Data = newData
case ValueTypeSliceFloat:
newValue.Type = ValueTypeSliceFloat
_, ok := srcValue.Data.([]float32)
var newData []float32
if !ok {
// 将类型强制转换为[]float32
newData = make([]float32, len(srcValue.Data.([]interface{})))
for i, val := range srcValue.Data.([]interface{}) {
newData[i] = float32(val.(float64))
}
} else {
newData = make([]float32, len(srcValue.Data.([]float32)))
copy(newData, srcValue.Data.([]float32))
}
newValue.Data = newData
case ValueTypeSliceBool:
newValue.Type = ValueTypeSliceBool
_, ok := srcValue.Data.([]bool)
var newData []bool
if !ok {
// 将类型强制转换为[]bool
newData = make([]bool, len(srcValue.Data.([]interface{})))
for i, val := range srcValue.Data.([]interface{}) {
newData[i] = val.(bool)
}
} else {
newData = make([]bool, len(srcValue.Data.([]bool)))
copy(newData, srcValue.Data.([]bool))
}
newValue.Data = newData
}
}