/
serial.go
234 lines (226 loc) · 5.04 KB
/
serial.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
/**
2 * @Author: Nico
3 * @Date: 2020/11/8 17:53
4 */
package mvc
import (
"errors"
"fmt"
"github.com/shopspring/decimal"
"reflect"
"strconv"
)
const floatBits = 10000 * 10000 * 10000 * 10000
var (
uintType = reflect.TypeOf(uint(0))
intType = reflect.TypeOf(0)
int8Type = reflect.TypeOf(int8(0))
uint8Type = reflect.TypeOf(uint8(0))
int16Type = reflect.TypeOf(int16(0))
uint16Type = reflect.TypeOf(uint64(0))
int32Type = reflect.TypeOf(int32(0))
uint32Type = reflect.TypeOf(uint32(0))
int64Type = reflect.TypeOf(int64(0))
uint64Type = reflect.TypeOf(uint64(0))
boolType = reflect.TypeOf(true)
float32Type = reflect.TypeOf(float32(0))
float64Type = reflect.TypeOf(float64(0))
stringType = reflect.TypeOf("")
stringArrayType = reflect.TypeOf([]string{})
bytesType = reflect.TypeOf([]byte{})
)
type Serial interface {
Serial(dest interface{}) string
DeSerial(src interface{}, t reflect.Type) (interface{}, error)
}
type defaultSerial struct {
json JSON
}
func DefaultSerial(jsons... JSON) Serial{
var json JSON = defaultJSON{}
if len(jsons) > 0{
json = jsons[0]
}
return &defaultSerial{json: json}
}
func (s defaultSerial) Serial(dest interface{}) string{
var key string
if dest == nil {
return key
}
switch dest.(type) {
case float64:
key = decimal.NewFromFloat(dest.(float64)).String()
case *float64:
key = decimal.NewFromFloat(*dest.(*float64)).String()
case float32:
key = decimal.NewFromFloat32(dest.(float32)).String()
case *float32:
key = decimal.NewFromFloat32(*dest.(*float32)).String()
case int:
key = strconv.Itoa(dest.(int))
case *int:
key = strconv.Itoa(*dest.(*int))
case uint:
key = strconv.Itoa(int(dest.(uint)))
case *uint:
key = strconv.Itoa(int(*dest.(*uint)))
case int8:
key = strconv.Itoa(int(dest.(int8)))
case *int8:
key = strconv.Itoa(int(*dest.(*int8)))
case uint8:
key = strconv.Itoa(int(dest.(uint8)))
case *uint8:
key = strconv.Itoa(int(*dest.(*uint8)))
case int16:
key = strconv.Itoa(int(dest.(int16)))
case *int16:
key = strconv.Itoa(int(*dest.(*int16)))
case uint16:
key = strconv.Itoa(int(dest.(uint16)))
case *uint16:
key = strconv.Itoa(int(*dest.(*uint16)))
case int32:
key = strconv.Itoa(int(dest.(int32)))
case *int32:
key = strconv.Itoa(int(*dest.(*int32)))
case uint32:
key = strconv.Itoa(int(dest.(uint32)))
case *uint32:
key = strconv.Itoa(int(*dest.(*uint32)))
case int64:
key = strconv.FormatInt(dest.(int64), 10)
case *int64:
key = strconv.FormatInt(*dest.(*int64), 10)
case uint64:
key = strconv.FormatUint(dest.(uint64), 10)
case *uint64:
key = strconv.FormatUint(*dest.(*uint64), 10)
case string:
key = dest.(string)
case *string:
key = *dest.(*string)
case []byte:
key = string(dest.([]byte))
case *[]byte:
key = string(*dest.(*[]byte))
case bool:
if dest.(bool){
key = "true"
}else{
key = "false"
}
case *bool:
if *dest.(*bool){
key = "true"
}else{
key = "false"
}
default:
newValue, _ := s.json.Marshal(dest)
key = string(newValue)
}
return key
}
func (s defaultSerial) DeSerial(src interface{}, t reflect.Type) (interface{}, error){
val := s.Serial(src)
res := interface{}(nil)
if val == "" && t != stringType{
return nil, nil
}
switch t {
case float64Type:
dec, err := decimal.NewFromString(val)
if err != nil {
return nil, err
}
res, _ = dec.Float64()
case float32Type:
dec, err := decimal.NewFromString(val)
if err != nil {
return nil, err
}
res, _ = dec.Float64()
case boolType:
res = false
if val == "true" {
res = true
} else if val != "false" {
return nil, errors.New(fmt.Sprintf("%v is not bool type. ", val))
}
case intType:
it, err := strconv.ParseInt(val, 10, strconv.IntSize)
if err != nil {
return nil, err
}
res = int(it)
case uintType:
it, err := strconv.ParseUint(val, 10, strconv.IntSize)
if err != nil {
return nil, err
}
res = uint(it)
case int8Type:
it, err := strconv.ParseInt(val, 10, 8)
if err != nil {
return nil, err
}
res = int8(it)
case uint8Type:
it, err := strconv.ParseUint(val, 10, 8)
if err != nil {
return nil, err
}
res = uint8(it)
case int16Type:
it, err := strconv.ParseInt(val, 10, 16)
if err != nil {
return nil, err
}
res = int16(it)
case uint16Type:
it, err := strconv.ParseUint(val, 10, 16)
if err != nil {
return nil, err
}
res = uint16(it)
case int32Type:
it, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return nil, err
}
res = int32(it)
case uint32Type:
it, err := strconv.ParseUint(val, 10, 32)
if err != nil {
return nil, err
}
res = uint32(it)
case int64Type:
it, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, err
}
res = it
case uint64Type:
it, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return nil, err
}
res = it
case stringType:
res = val
case bytesType:
res = []byte(val)
default:
v := reflect.New(t)
inter := v.Interface()
err := s.json.Unmarshal([]byte(val), &inter)
if err != nil {
return nil, err
}
res = v.Elem().Interface()
}
return res, nil
}