-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvaluedecoder.go
More file actions
159 lines (147 loc) · 3.47 KB
/
valuedecoder.go
File metadata and controls
159 lines (147 loc) · 3.47 KB
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
package urlquery
import (
"reflect"
"strconv"
)
// A valueDecode is a converter from string to go basic structure
type valueDecode func(string) (reflect.Value, error)
// convert from string to bool
func boolDecode(value string) (reflect.Value, error) {
b, err := strconv.ParseBool(value)
if err != nil {
err = ErrTranslated{err: err}
return reflect.Value{}, err
}
return reflect.ValueOf(b), nil
}
// convert from string to int(8-64)
func baseIntDecode(value string, bitSize int) (rv reflect.Value, err error) {
v, err := strconv.ParseInt(value, 10, bitSize)
if err != nil {
err = ErrTranslated{err: err}
return
}
switch bitSize {
case 64:
rv = reflect.ValueOf(v)
case 32:
rv = reflect.ValueOf(int32(v))
case 16:
rv = reflect.ValueOf(int16(v))
case 8:
rv = reflect.ValueOf(int8(v))
case 0:
rv = reflect.ValueOf(int(v))
default:
err = ErrUnsupportedBitSize{bitSize: bitSize}
}
return
}
// convert from string to int(8-64)
func intDecode(bitSize int) valueDecode {
return func(value string) (reflect.Value, error) {
return baseIntDecode(value, bitSize)
}
}
// convert from string to uint(8-64)
func baseUintDecode(value string, bitSize int) (rv reflect.Value, err error) {
v, err := strconv.ParseUint(value, 10, bitSize)
if err != nil {
err = ErrTranslated{err: err}
return
}
switch bitSize {
case 64:
rv = reflect.ValueOf(v)
case 32:
rv = reflect.ValueOf(uint32(v))
case 16:
rv = reflect.ValueOf(uint16(v))
case 8:
rv = reflect.ValueOf(uint8(v))
case 0:
rv = reflect.ValueOf(uint(v))
default:
err = ErrUnsupportedBitSize{bitSize: bitSize}
}
return
}
// convert from string to uint(8-64)
func uintDecode(bitSize int) valueDecode {
return func(value string) (reflect.Value, error) {
return baseUintDecode(value, bitSize)
}
}
// convert from string to uintPrt
func uintPrtDecode(value string) (rv reflect.Value, err error) {
v, err := strconv.ParseUint(value, 10, 0)
if err != nil {
err = ErrTranslated{err: err}
return
}
return reflect.ValueOf(uintptr(v)), nil
}
// convert from string to float,double
func baseFloatDecode(value string, bitSize int) (rv reflect.Value, err error) {
v, err := strconv.ParseFloat(value, bitSize)
if err != nil {
err = ErrTranslated{err: err}
return
}
switch bitSize {
case 64:
rv = reflect.ValueOf(v)
case 32:
rv = reflect.ValueOf(float32(v))
default:
err = ErrUnsupportedBitSize{bitSize: bitSize}
}
return
}
// convert from string to float
func floatDecode(bitSize int) valueDecode {
return func(value string) (reflect.Value, error) {
return baseFloatDecode(value, bitSize)
}
}
// convert from string to string
func stringDecode(value string) (reflect.Value, error) {
return reflect.ValueOf(value), nil
}
// get decode func for specified reflect kind
func getDecodeFunc(kind reflect.Kind) valueDecode {
switch kind {
case reflect.Bool:
return boolDecode
case reflect.Int:
return intDecode(0)
case reflect.Int8:
return intDecode(8)
case reflect.Int16:
return intDecode(16)
case reflect.Int32:
return intDecode(32)
case reflect.Int64:
return intDecode(64)
case reflect.Uint:
return uintDecode(0)
case reflect.Uint8:
return uintDecode(8)
case reflect.Uint16:
return uintDecode(16)
case reflect.Uint32:
return uintDecode(32)
case reflect.Uint64:
return uintDecode(64)
case reflect.Uintptr:
return uintPrtDecode
case reflect.Float32:
return floatDecode(32)
case reflect.Float64:
return floatDecode(64)
case reflect.String:
return stringDecode
default:
return nil
}
}