Skip to content

Commit 9d1366c

Browse files
committed
Merge pull request #28 from karmakaze/return-sized-types-for-interface
Return u/int8, u/int16, u/int32 (sign-extend Int24), float32, float64 for interface{} as appropriate
2 parents fcea4fe + 6bdb7a1 commit 9d1366c

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

mysql/mysql_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -103,43 +103,43 @@ func (t *mysqlTestSuite) TestMysqlGTIDContain(c *check.C) {
103103
}
104104

105105
func (t *mysqlTestSuite) TestMysqlParseBinaryInt8(c *check.C) {
106-
i64 := ParseBinaryInt8([]byte{128})
107-
c.Assert(i64, check.Equals, int64(-128))
106+
i8 := ParseBinaryInt8([]byte{128})
107+
c.Assert(i8, check.Equals, int8(-128))
108108
}
109109

110110
func (t *mysqlTestSuite) TestMysqlParseBinaryUint8(c *check.C) {
111-
u64 := ParseBinaryUint8([]byte{128})
112-
c.Assert(u64, check.Equals, uint64(128))
111+
u8 := ParseBinaryUint8([]byte{128})
112+
c.Assert(u8, check.Equals, uint8(128))
113113
}
114114

115115
func (t *mysqlTestSuite) TestMysqlParseBinaryInt16(c *check.C) {
116-
i64 := ParseBinaryInt16([]byte{1, 128})
117-
c.Assert(i64, check.Equals, int64(-128*256 + 1))
116+
i16 := ParseBinaryInt16([]byte{1, 128})
117+
c.Assert(i16, check.Equals, int16(-128*256 + 1))
118118
}
119119

120120
func (t *mysqlTestSuite) TestMysqlParseBinaryUint16(c *check.C) {
121-
u64 := ParseBinaryUint16([]byte{1, 128})
122-
c.Assert(u64, check.Equals, uint64(128*256 + 1))
121+
u16 := ParseBinaryUint16([]byte{1, 128})
122+
c.Assert(u16, check.Equals, uint16(128*256 + 1))
123123
}
124124

125125
func (t *mysqlTestSuite) TestMysqlParseBinaryInt24(c *check.C) {
126-
i64 := ParseBinaryInt24([]byte{1, 2, 128})
127-
c.Assert(i64, check.Equals, int64(-128*65536 + 2*256 + 1))
126+
i32 := ParseBinaryInt24([]byte{1, 2, 128})
127+
c.Assert(i32, check.Equals, int32(-128*65536 + 2*256 + 1))
128128
}
129129

130130
func (t *mysqlTestSuite) TestMysqlParseBinaryUint24(c *check.C) {
131-
u64 := ParseBinaryUint24([]byte{1, 2, 128})
132-
c.Assert(u64, check.Equals, uint64(128*65536 + 2*256 + 1))
131+
u32 := ParseBinaryUint24([]byte{1, 2, 128})
132+
c.Assert(u32, check.Equals, uint32(128*65536 + 2*256 + 1))
133133
}
134134

135135
func (t *mysqlTestSuite) TestMysqlParseBinaryInt32(c *check.C) {
136-
i64 := ParseBinaryInt32([]byte{1, 2, 3, 128})
137-
c.Assert(i64, check.Equals, int64(-128*16777216 + 3*65536 + 2*256 + 1))
136+
i32 := ParseBinaryInt32([]byte{1, 2, 3, 128})
137+
c.Assert(i32, check.Equals, int32(-128*16777216 + 3*65536 + 2*256 + 1))
138138
}
139139

140140
func (t *mysqlTestSuite) TestMysqlParseBinaryUint32(c *check.C) {
141-
u64 := ParseBinaryUint32([]byte{1, 2, 3, 128})
142-
c.Assert(u64, check.Equals, uint64(128*16777216 + 3*65536 + 2*256 + 1))
141+
u32 := ParseBinaryUint32([]byte{1, 2, 3, 128})
142+
c.Assert(u32, check.Equals, uint32(128*16777216 + 3*65536 + 2*256 + 1))
143143
}
144144

145145
func (t *mysqlTestSuite) TestMysqlParseBinaryInt64(c *check.C) {

mysql/parse_binary.go

+25-16
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,39 @@ package mysql
22

33
import (
44
"encoding/binary"
5+
"math"
56
)
67

7-
func ParseBinaryInt8(data []byte) int64 {
8-
return int64(int8(data[0]))
8+
func ParseBinaryInt8(data []byte) int8 {
9+
return int8(data[0])
910
}
10-
func ParseBinaryUint8(data []byte) uint64 {
11-
return uint64((data[0]))
11+
func ParseBinaryUint8(data []byte) uint8 {
12+
return data[0]
1213
}
1314

14-
func ParseBinaryInt16(data []byte) int64 {
15-
return int64(int16(binary.LittleEndian.Uint16(data)))
15+
func ParseBinaryInt16(data []byte) int16 {
16+
return int16(binary.LittleEndian.Uint16(data))
1617
}
17-
func ParseBinaryUint16(data []byte) uint64 {
18-
return uint64(binary.LittleEndian.Uint16(data))
18+
func ParseBinaryUint16(data []byte) uint16 {
19+
return binary.LittleEndian.Uint16(data)
1920
}
2021

21-
func ParseBinaryInt24(data []byte) int64 {
22+
func ParseBinaryInt24(data []byte) int32 {
2223
u32 := uint32(ParseBinaryUint24(data))
2324
if u32&0x00800000 != 0 {
2425
u32 |= 0xFF000000
2526
}
26-
return int64(int32(u32))
27+
return int32(u32)
2728
}
28-
func ParseBinaryUint24(data []byte) uint64 {
29-
return uint64(uint32(data[0]) + uint32(data[1])<<8 + uint32(data[2])<<16)
29+
func ParseBinaryUint24(data []byte) uint32 {
30+
return uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16
3031
}
3132

32-
func ParseBinaryInt32(data []byte) int64 {
33-
return int64(int32(binary.LittleEndian.Uint32(data)))
33+
func ParseBinaryInt32(data []byte) int32 {
34+
return int32(binary.LittleEndian.Uint32(data))
3435
}
35-
func ParseBinaryUint32(data []byte) uint64 {
36-
return uint64(binary.LittleEndian.Uint32(data))
36+
func ParseBinaryUint32(data []byte) uint32 {
37+
return binary.LittleEndian.Uint32(data)
3738
}
3839

3940
func ParseBinaryInt64(data []byte) int64 {
@@ -42,3 +43,11 @@ func ParseBinaryInt64(data []byte) int64 {
4243
func ParseBinaryUint64(data []byte) uint64 {
4344
return binary.LittleEndian.Uint64(data)
4445
}
46+
47+
func ParseBinaryFloat32(data []byte) float32 {
48+
return math.Float32frombits(binary.LittleEndian.Uint32(data))
49+
}
50+
51+
func ParseBinaryFloat64(data []byte) float64 {
52+
return math.Float64frombits(binary.LittleEndian.Uint64(data))
53+
}

mysql/resultset.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package mysql
22

33
import (
4-
"encoding/binary"
5-
"math"
64
"strconv"
75

86
"github.com/juju/errors"
@@ -138,12 +136,12 @@ func (p RowData) ParseBinary(f []*Field) ([]interface{}, error) {
138136
continue
139137

140138
case MYSQL_TYPE_FLOAT:
141-
data[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(p[pos : pos+4])))
139+
data[i] = ParseBinaryFloat32(p[pos : pos+4])
142140
pos += 4
143141
continue
144142

145143
case MYSQL_TYPE_DOUBLE:
146-
data[i] = math.Float64frombits(binary.LittleEndian.Uint64(p[pos : pos+8]))
144+
data[i] = ParseBinaryFloat64(p[pos : pos+4])
147145
pos += 8
148146
continue
149147

replication/row_event.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/hex"
77
"fmt"
88
"io"
9-
"math"
109
"strconv"
1110
"time"
1211

@@ -361,10 +360,10 @@ func (e *RowsEvent) decodeValue(data []byte, tp byte, meta uint16) (v interface{
361360
v, n, err = decodeDecimal(data, int(prec), int(scale))
362361
case MYSQL_TYPE_FLOAT:
363362
n = 4
364-
v = float64(math.Float32frombits(binary.LittleEndian.Uint32(data)))
363+
v = ParseBinaryFloat32(data)
365364
case MYSQL_TYPE_DOUBLE:
366365
n = 8
367-
v = math.Float64frombits(binary.LittleEndian.Uint64(data))
366+
v = ParseBinaryFloat64(data)
368367
case MYSQL_TYPE_BIT:
369368
nbits := ((meta >> 8) * 8) + (meta & 0xFF)
370369
n = int(nbits+7) / 8

0 commit comments

Comments
 (0)