Skip to content

Commit

Permalink
add more decoding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamaton committed Aug 9, 2024
1 parent 041830c commit 795043d
Show file tree
Hide file tree
Showing 2 changed files with 422 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/stream/decoding/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (d *decoder) decodeWithCode(code byte, rv reflect.Value) error {
return err
}
if len(bs) > rv.Len() {
return fmt.Errorf("%v len is %d, but msgpack has %d elements", rv.Type(), rv.Len(), len(bs))
return fmt.Errorf("%v len is %d, but msgpack has %d elements, %w", rv.Type(), rv.Len(), len(bs), ErrNotMatchArrayElement)
}
for i, b := range bs {
rv.Index(i).SetUint(uint64(b))
Expand All @@ -204,7 +204,7 @@ func (d *decoder) decodeWithCode(code byte, rv reflect.Value) error {
return err
}
if l > rv.Len() {
return fmt.Errorf("%v len is %d, but msgpack has %d elements", rv.Type(), rv.Len(), l)
return fmt.Errorf("%v len is %d, but msgpack has %d elements, %w", rv.Type(), rv.Len(), l, ErrNotMatchArrayElement)
}
bs, err := d.asStringByteByLength(l, k)
if err != nil {
Expand All @@ -223,7 +223,7 @@ func (d *decoder) decodeWithCode(code byte, rv reflect.Value) error {
}

if l > rv.Len() {
return fmt.Errorf("%v len is %d, but msgpack has %d elements", rv.Type(), rv.Len(), l)
return fmt.Errorf("%v len is %d, but msgpack has %d elements, %w", rv.Type(), rv.Len(), l, ErrNotMatchArrayElement)
}

// create array dynamically
Expand Down Expand Up @@ -320,6 +320,8 @@ func (d *decoder) decodeWithCode(code byte, rv reflect.Value) error {
return nil
}

var ErrNotMatchArrayElement = errors.New("not match array element")

var ErrCanNotDecode = errors.New("msgpack : invalid code")

func (d *decoder) errorTemplate(code byte, k reflect.Kind) error {
Expand Down
Loading

0 comments on commit 795043d

Please sign in to comment.