/
read_convert.go
152 lines (148 loc) · 3.69 KB
/
read_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
package jsondecoder
import (
"encoding/base64"
"errors"
"strconv"
"unsafe"
)
func (dec *Decoder) unquoteBytes(b []byte) (t []byte, err error) {
var ok bool
if t, ok = unquoteStdBytes(b); !ok {
err = errors.New("invalid bytes format")
return nil, err
}
return
}
func (dec *Decoder) unquoteString(b []byte) (t []byte, err error) {
var ok bool
if t, ok = unquoteStdBytes(b); !ok {
err = errors.New("invalid string format")
return nil, err
}
return
}
func (dec *Decoder) convertToBytes(b []byte) (vv []byte, err error) {
if b, err = dec.unquoteBytes(b); err != nil {
return
}
dst := make([]byte, base64.StdEncoding.DecodedLen(len(b)))
n, err := base64.StdEncoding.Decode(dst, b)
if err != nil {
return nil, err
}
vv = dst[:n]
return
}
func (dec *Decoder) convertToString(b []byte) (vv string, err error) {
v, ok := unquoteJSONBytes(b)
if !ok {
err = errors.New("unable to convert to the type string")
return
}
vv = string(v)
return
}
func (dec *Decoder) convertToBool(unquote bool, b []byte) (vv bool, err error) {
if unquote {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
if vv, err = strconv.ParseBool(*(*string)(unsafe.Pointer(&b))); err != nil {
err = errors.New("unable to convert to the type bool")
return
}
return
}
func (dec *Decoder) convertToInt32(unquote bool, b []byte) (vv int32, err error) {
if unquote {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
var i64 int64
if i64, err = strconv.ParseInt(*(*string)(unsafe.Pointer(&b)), 10, 32); err != nil {
err = errors.New("unable to convert to the type int32")
return
}
vv = int32(i64)
return
}
func (dec *Decoder) convertToInt64(unquote bool, b []byte) (vv int64, err error) {
if unquote {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
if vv, err = strconv.ParseInt(*(*string)(unsafe.Pointer(&b)), 10, 64); err != nil {
err = errors.New("unable to convert to the type int64")
return
}
return
}
func (dec *Decoder) convertToUint32(unquote bool, b []byte) (vv uint32, err error) {
if unquote {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
var u64 uint64
if u64, err = strconv.ParseUint(*(*string)(unsafe.Pointer(&b)), 10, 32); err != nil {
err = errors.New("unable to convert to the type uint32")
return
}
vv = uint32(u64)
return
}
func (dec *Decoder) convertToUint64(unquote bool, b []byte) (vv uint64, err error) {
if unquote {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
if vv, err = strconv.ParseUint(*(*string)(unsafe.Pointer(&b)), 10, 64); err != nil {
err = errors.New("unable to convert to the type uint64")
return
}
return
}
func (dec *Decoder) convertToFloat32(unquote bool, b []byte) (vv float32, err error) {
if unquote && floatIsNumber(b) {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
var f64 float64
if f64, err = strconv.ParseFloat(*(*string)(unsafe.Pointer(&b)), 32); err != nil {
err = errors.New("unable to convert to the type float32")
return
}
vv = float32(f64)
return
}
func (dec *Decoder) convertToFloat64(unquote bool, b []byte) (vv float64, err error) {
if unquote && floatIsNumber(b) {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
if vv, err = strconv.ParseFloat(*(*string)(unsafe.Pointer(&b)), 64); err != nil {
err = errors.New("unable to convert to the type float64")
return
}
return
}
func (dec *Decoder) convertToEnum(unquote bool, b []byte) (vv int32, err error) {
if unquote {
if b, err = dec.unquoteString(b); err != nil {
return
}
}
var i64 int64
if i64, err = strconv.ParseInt(*(*string)(unsafe.Pointer(&b)), 10, 32); err != nil {
err = errors.New("unable to convert to the type Enum")
return
}
vv = int32(i64)
return
}