Skip to content

Commit c0f14a1

Browse files
committed
fix tests
1 parent 79ef761 commit c0f14a1

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

marshal.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func (smallIntTypeInfo) Marshal(value interface{}) ([]byte, error) {
434434
func (s smallIntTypeInfo) Unmarshal(data []byte, value interface{}) error {
435435
decodedData, err := decShort(data)
436436
if err != nil {
437-
return err
437+
return unmarshalErrorf("%s", err.Error())
438438
}
439439
if iptr, ok := value.(*interface{}); ok && iptr != nil {
440440
var v int16
@@ -546,7 +546,7 @@ func (tinyIntTypeInfo) Marshal(value interface{}) ([]byte, error) {
546546
func (t tinyIntTypeInfo) Unmarshal(data []byte, value interface{}) error {
547547
decodedData, err := decTiny(data)
548548
if err != nil {
549-
return err
549+
return unmarshalErrorf("%s", err.Error())
550550
}
551551
if iptr, ok := value.(*interface{}); ok && iptr != nil {
552552
var v int8
@@ -646,7 +646,7 @@ func (intTypeInfo) Marshal(value interface{}) ([]byte, error) {
646646
func (i intTypeInfo) Unmarshal(data []byte, value interface{}) error {
647647
decodedData, err := decInt(data)
648648
if err != nil {
649-
return err
649+
return unmarshalErrorf("%s", err.Error())
650650
}
651651
if iptr, ok := value.(*interface{}); ok && iptr != nil {
652652
var v int
@@ -1328,23 +1328,21 @@ func (decimalTypeInfo) Marshal(value interface{}) ([]byte, error) {
13281328

13291329
// Unmarshal unmarshals the byte slice into the value.
13301330
func (decimalTypeInfo) Unmarshal(data []byte, value interface{}) error {
1331+
if len(data) < 4 {
1332+
return unmarshalErrorf("inf.Dec needs at least 4 bytes, while value has only %d", len(data))
1333+
}
1334+
13311335
decodedData, err := decInt(data[0:4])
13321336
if err != nil {
13331337
return err
13341338
}
13351339
switch v := value.(type) {
13361340
case *inf.Dec:
1337-
if len(data) < 4 {
1338-
return unmarshalErrorf("inf.Dec needs at least 4 bytes, while value has only %d", len(data))
1339-
}
13401341
scale := decodedData
13411342
unscaled := decBigInt2C(data[4:], nil)
13421343
*v = *inf.NewDecBig(unscaled, inf.Scale(scale))
13431344
return nil
13441345
case *interface{}:
1345-
if len(data) < 4 {
1346-
return unmarshalErrorf("inf.Dec needs at least 4 bytes, while value has only %d", len(data))
1347-
}
13481346
scale := decodedData
13491347
unscaled := decBigInt2C(data[4:], nil)
13501348
*v = inf.NewDecBig(unscaled, inf.Scale(scale))

marshal_test.go

Lines changed: 4 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("can not unmarshal into non-pointer <nil>"),
607+
UnmarshalError("expected 4 bytes decoding int but got 0"),
608608
},
609609
{
610610
varcharLikeTypeInfo{typ: TypeVarchar},
@@ -1704,10 +1704,7 @@ func TestMarshalTime(t *testing.T) {
17041704
t.Errorf("marshalTest[%d]: %v", i, err)
17051705
continue
17061706
}
1707-
decoded, err := decInt(test.Data)
1708-
if err != nil {
1709-
t.Error(err)
1710-
}
1707+
decoded := decBigInt(test.Data)
17111708
if !bytes.Equal(data, test.Data) {
17121709
t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
17131710
test.Data, decoded, data, decoded, test.Value)
@@ -2390,13 +2387,9 @@ func TestMarshalDuration(t *testing.T) {
23902387
t.Errorf("marshalTest[%d]: %v", i, err)
23912388
continue
23922389
}
2393-
decoded, err := decInt(test.Data)
2394-
if err != nil {
2395-
t.Error(err)
2396-
}
23972390
if !bytes.Equal(data, test.Data) {
2398-
t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
2399-
test.Data, decoded, data, decoded, test.Value)
2391+
t.Errorf("marshalTest[%d]: expected %x, got %x for time %s", i,
2392+
test.Data, data, test.Value)
24002393
}
24012394
}
24022395
}

0 commit comments

Comments
 (0)