Skip to content

add Decoder to read stream of messages #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import "github.com/shamaton/msgpack/v2/internal/decoding"
// UnmarshalAsMap decodes data that is encoded as map format.
// This is the same thing that StructAsArray sets false.
func UnmarshalAsMap(data []byte, v interface{}) error {
return decoding.Decode(data, v, false)
_, err := decoding.Decode(data, v, false, true)
return err
}

// UnmarshalAsArray decodes data that is encoded as array format.
// This is the same thing that StructAsArray sets true.
func UnmarshalAsArray(data []byte, v interface{}) error {
return decoding.Decode(data, v, true)
_, err := decoding.Decode(data, v, true, true)
return err
}
15 changes: 8 additions & 7 deletions internal/decoding/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package decoding

import (
"fmt"
"io"
"reflect"

"github.com/shamaton/msgpack/v2/internal/common"
Expand All @@ -15,27 +16,27 @@ type decoder struct {

// Decode analyzes the MessagePack-encoded data and stores
// the result into the pointer of v.
func Decode(data []byte, v interface{}, asArray bool) error {
func Decode(data []byte, v interface{}, asArray bool, strictLengthCheck bool) (int, error) {
d := decoder{data: data, asArray: asArray}

if d.data == nil || len(d.data) < 1 {
return fmt.Errorf("data is empty")
return 0, io.EOF
}
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return fmt.Errorf("holder must set pointer value. but got: %t", v)
return 0, fmt.Errorf("holder must set pointer value. but got: %t", v)
}

rv = rv.Elem()

last, err := d.decode(rv, 0)
if err != nil {
return err
return 0, err
}
if len(data) != last {
return fmt.Errorf("failed deserialization size=%d, last=%d", len(data), last)
if strictLengthCheck && len(data) != last {
return 0, fmt.Errorf("failed deserialization size=%d, last=%d", len(data), last)
}
return err
return last, err
}

func (d *decoder) decode(rv reflect.Value, offset int) (int, error) {
Expand Down
3 changes: 2 additions & 1 deletion msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func Marshal(v interface{}) ([]byte, error) {
// Unmarshal analyzes the MessagePack-encoded data and stores
// the result into the pointer of v.
func Unmarshal(data []byte, v interface{}) error {
return decoding.Decode(data, v, StructAsArray)
_, err := decoding.Decode(data, v, StructAsArray, true)
return err
}

// AddExtCoder adds encoders for extension types.
Expand Down