Skip to content

Commit 163773c

Browse files
committed
fix tests
1 parent c0f14a1 commit 163773c

File tree

2 files changed

+72
-34
lines changed

2 files changed

+72
-34
lines changed

marshal.go

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ func encInt(x int32) []byte {
664664
}
665665

666666
func decInt(x []byte) (int32, error) {
667+
if x == nil {
668+
return 0, nil
669+
}
667670
if len(x) != 4 {
668671
return 0, fmt.Errorf("expected 4 bytes decoding int but got %v", len(x))
669672
}
@@ -678,13 +681,19 @@ func encShort(x int16) []byte {
678681
}
679682

680683
func decShort(p []byte) (int16, error) {
684+
if p == nil {
685+
return 0, nil
686+
}
681687
if len(p) != 2 {
682688
return 0, fmt.Errorf("expected 2 bytes decoding short but got %v", len(p))
683689
}
684690
return int16(p[0])<<8 | int16(p[1]), nil
685691
}
686692

687693
func decTiny(p []byte) (int8, error) {
694+
if p == nil {
695+
return 0, nil
696+
}
688697
if len(p) != 1 {
689698
return 0, fmt.Errorf("expected 1 byte decoding tinyint but got %v", len(p))
690699
}
@@ -786,15 +795,19 @@ func bytesToUint64(data []byte) (ret uint64) {
786795

787796
// Unmarshal unmarshals the byte slice into the value.
788797
func (b bigIntLikeTypeInfo) Unmarshal(data []byte, value interface{}) error {
798+
decodedData, err := decBigInt(data)
799+
if err != nil {
800+
return unmarshalErrorf("can not unmarshal bigint: %s", err.Error())
801+
}
789802
if iptr, ok := value.(*interface{}); ok && iptr != nil {
790803
var v int64
791-
if err := unmarshalIntlike(b.typ, decBigInt(data), data, &v); err != nil {
804+
if err := unmarshalIntlike(b.typ, decodedData, data, &v); err != nil {
792805
return err
793806
}
794807
*iptr = v
795808
return nil
796809
}
797-
return unmarshalIntlike(b.typ, decBigInt(data), data, value)
810+
return unmarshalIntlike(b.typ, decodedData, data, value)
798811
}
799812

800813
type varintTypeInfo struct{}
@@ -1095,14 +1108,17 @@ func unmarshalIntlike(typ Type, int64Val int64, data []byte, value interface{})
10951108
return unmarshalErrorf("can not unmarshal int-like into %T. Accepted types: big.Int, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string, *interface{}.", value)
10961109
}
10971110

1098-
func decBigInt(data []byte) int64 {
1111+
func decBigInt(data []byte) (int64, error) {
1112+
if data == nil {
1113+
return 0, nil
1114+
}
10991115
if len(data) != 8 {
1100-
return 0
1116+
return 0, fmt.Errorf("expected 8 bytes, got %d", len(data))
11011117
}
11021118
return int64(data[0])<<56 | int64(data[1])<<48 |
11031119
int64(data[2])<<40 | int64(data[3])<<32 |
11041120
int64(data[4])<<24 | int64(data[5])<<16 |
1105-
int64(data[6])<<8 | int64(data[7])
1121+
int64(data[6])<<8 | int64(data[7]), nil
11061122
}
11071123

11081124
type booleanTypeInfo struct{}
@@ -1142,12 +1158,16 @@ func (b booleanTypeInfo) Marshal(value interface{}) ([]byte, error) {
11421158

11431159
// Unmarshal unmarshals the byte slice into the value.
11441160
func (b booleanTypeInfo) Unmarshal(data []byte, value interface{}) error {
1161+
decodedData, err := decBool(data)
1162+
if err != nil {
1163+
return unmarshalErrorf("can not unmarshal boolean: %s", err.Error())
1164+
}
11451165
switch v := value.(type) {
11461166
case *bool:
1147-
*v = decBool(data)
1167+
*v = decodedData
11481168
return nil
11491169
case *interface{}:
1150-
*v = decBool(data)
1170+
*v = decodedData
11511171
return nil
11521172
}
11531173
rv := reflect.ValueOf(value)
@@ -1157,7 +1177,7 @@ func (b booleanTypeInfo) Unmarshal(data []byte, value interface{}) error {
11571177
rv = rv.Elem()
11581178
switch rv.Type().Kind() {
11591179
case reflect.Bool:
1160-
rv.SetBool(decBool(data))
1180+
rv.SetBool(decodedData)
11611181
return nil
11621182
}
11631183
return unmarshalErrorf("can not unmarshal boolean into %T. Accepted types: *bool, *interface{}.", value)
@@ -1170,11 +1190,14 @@ func encBool(v bool) []byte {
11701190
return []byte{0}
11711191
}
11721192

1173-
func decBool(v []byte) bool {
1174-
if len(v) == 0 {
1175-
return false
1193+
func decBool(v []byte) (bool, error) {
1194+
if v == nil {
1195+
return false, nil
1196+
}
1197+
if len(v) != 1 {
1198+
return false, fmt.Errorf("expected 1 byte, got %d", len(v))
11761199
}
1177-
return v[0] != 0
1200+
return v[0] != 0, nil
11781201
}
11791202

11801203
type floatTypeInfo struct{}
@@ -1270,12 +1293,17 @@ func (doubleTypeInfo) Marshal(value interface{}) ([]byte, error) {
12701293

12711294
// Unmarshal unmarshals the byte slice into the value.
12721295
func (doubleTypeInfo) Unmarshal(data []byte, value interface{}) error {
1296+
decodedData, err := decBigInt(data)
1297+
if err != nil {
1298+
return unmarshalErrorf("can not unmarshal double: %s", err.Error())
1299+
}
1300+
decodedUint64 := uint64(decodedData)
12731301
switch v := value.(type) {
12741302
case *float64:
1275-
*v = math.Float64frombits(uint64(decBigInt(data)))
1303+
*v = math.Float64frombits(decodedUint64)
12761304
return nil
12771305
case *interface{}:
1278-
*v = math.Float64frombits(uint64(decBigInt(data)))
1306+
*v = math.Float64frombits(decodedUint64)
12791307
return nil
12801308
}
12811309
rv := reflect.ValueOf(value)
@@ -1285,7 +1313,7 @@ func (doubleTypeInfo) Unmarshal(data []byte, value interface{}) error {
12851313
rv = rv.Elem()
12861314
switch rv.Type().Kind() {
12871315
case reflect.Float64:
1288-
rv.SetFloat(math.Float64frombits(uint64(decBigInt(data))))
1316+
rv.SetFloat(math.Float64frombits(decodedUint64))
12891317
return nil
12901318
}
12911319
return unmarshalErrorf("can not unmarshal double into %T. Accepted types: *float64, *interface{}.", value)
@@ -1432,16 +1460,20 @@ func (timestampTypeInfo) Marshal(value interface{}) ([]byte, error) {
14321460

14331461
// Unmarshal unmarshals the byte slice into the value.
14341462
func (timestampTypeInfo) Unmarshal(data []byte, value interface{}) error {
1463+
decodedData, err := decBigInt(data)
1464+
if err != nil {
1465+
return unmarshalErrorf("can not unmarshal timestamp: %s", err.Error())
1466+
}
14351467
switch v := value.(type) {
14361468
case *int64:
1437-
*v = decBigInt(data)
1469+
*v = decodedData
14381470
return nil
14391471
case *time.Time:
14401472
if len(data) == 0 {
14411473
*v = time.Time{}
14421474
return nil
14431475
}
1444-
x := decBigInt(data)
1476+
x := decodedData
14451477
sec := x / 1000
14461478
nsec := (x - sec*1000) * 1000000
14471479
*v = time.Unix(sec, nsec).In(time.UTC)
@@ -1451,7 +1483,7 @@ func (timestampTypeInfo) Unmarshal(data []byte, value interface{}) error {
14511483
*v = time.Time{}
14521484
return nil
14531485
}
1454-
x := decBigInt(data)
1486+
x := decodedData
14551487
sec := x / 1000
14561488
nsec := (x - sec*1000) * 1000000
14571489
*v = time.Unix(sec, nsec).In(time.UTC)
@@ -1465,7 +1497,7 @@ func (timestampTypeInfo) Unmarshal(data []byte, value interface{}) error {
14651497
rv = rv.Elem()
14661498
switch rv.Type().Kind() {
14671499
case reflect.Int64:
1468-
rv.SetInt(decBigInt(data))
1500+
rv.SetInt(decodedData)
14691501
return nil
14701502
}
14711503
return unmarshalErrorf("can not unmarshal timestamp into %T. Accepted types: *int64, *time.Time, *interface{}.", value)
@@ -1508,15 +1540,19 @@ func (timeTypeInfo) Marshal(value interface{}) ([]byte, error) {
15081540

15091541
// Unmarshal unmarshals the byte slice into the value.
15101542
func (timeTypeInfo) Unmarshal(data []byte, value interface{}) error {
1543+
decodedData, err := decBigInt(data)
1544+
if err != nil {
1545+
return unmarshalErrorf("can not unmarshal time: %s", err.Error())
1546+
}
15111547
switch v := value.(type) {
15121548
case *int64:
1513-
*v = decBigInt(data)
1549+
*v = decodedData
15141550
return nil
15151551
case *time.Duration:
1516-
*v = time.Duration(decBigInt(data))
1552+
*v = time.Duration(decodedData)
15171553
return nil
15181554
case *interface{}:
1519-
*v = time.Duration(decBigInt(data))
1555+
*v = time.Duration(decodedData)
15201556
return nil
15211557
}
15221558

@@ -1527,7 +1563,7 @@ func (timeTypeInfo) Unmarshal(data []byte, value interface{}) error {
15271563
rv = rv.Elem()
15281564
switch rv.Type().Kind() {
15291565
case reflect.Int64:
1530-
rv.SetInt(decBigInt(data))
1566+
rv.SetInt(decodedData)
15311567
return nil
15321568
}
15331569
return unmarshalErrorf("can not unmarshal time into %T. Accepted types: *int64, *time.Duration, *interface{}.", value)

marshal_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ var marshalTests = []struct {
604604
[]byte(nil),
605605
nil,
606606
nil,
607-
UnmarshalError("expected 4 bytes decoding int but got 0"),
607+
UnmarshalError("can not unmarshal into non-pointer <nil>"),
608608
},
609609
{
610610
varcharLikeTypeInfo{typ: TypeVarchar},
@@ -1704,7 +1704,10 @@ func TestMarshalTime(t *testing.T) {
17041704
t.Errorf("marshalTest[%d]: %v", i, err)
17051705
continue
17061706
}
1707-
decoded := decBigInt(test.Data)
1707+
decoded, err := decBigInt(test.Data)
1708+
if err != nil {
1709+
t.Error(err)
1710+
}
17081711
if !bytes.Equal(data, test.Data) {
17091712
t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
17101713
test.Data, decoded, data, decoded, test.Value)
@@ -1759,7 +1762,7 @@ func TestMarshalTimestamp(t *testing.T) {
17591762
{
17601763
// Store the zero time as a blank slice
17611764
timestampTypeInfo{},
1762-
[]byte{},
1765+
[]byte(nil),
17631766
time.Time{},
17641767
},
17651768
}
@@ -1771,8 +1774,8 @@ func TestMarshalTimestamp(t *testing.T) {
17711774
continue
17721775
}
17731776
if !bytes.Equal(data, test.Data) {
1774-
t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
1775-
test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
1777+
t.Errorf("marshalTest[%d]: expected %x, got %x for time %s", i,
1778+
test.Data, data, test.Value)
17761779
}
17771780
}
17781781
}
@@ -1898,23 +1901,22 @@ func TestMarshalTuple(t *testing.T) {
18981901
},
18991902
}
19001903

1901-
for _, tc := range testCases {
1904+
for i, tc := range testCases {
19021905
t.Run(tc.name, func(t *testing.T) {
19031906
data, err := Marshal(info, tc.value)
19041907
if err != nil {
1905-
t.Errorf("marshalTest: %v", err)
1908+
t.Errorf("marshalTest[%d]: %v", i, err)
19061909
return
19071910
}
1908-
19091911
if !bytes.Equal(data, tc.expected) {
1910-
t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
1911-
tc.expected, decBigInt(tc.expected), data, decBigInt(data))
1912+
t.Errorf("marshalTest[%d]: expected %x, got %x",
1913+
i, tc.expected, data)
19121914
return
19131915
}
19141916

19151917
err = Unmarshal(info, data, tc.checkValue)
19161918
if err != nil {
1917-
t.Errorf("unmarshalTest: %v", err)
1919+
t.Errorf("marshalTest[%d]: %v", i, err)
19181920
return
19191921
}
19201922

0 commit comments

Comments
 (0)