Skip to content
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

Add custom unmarshaling support #43

Open
wants to merge 2 commits 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
17 changes: 17 additions & 0 deletions internal/decoding/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,25 @@ func Decode(data []byte, v interface{}, asArray bool) error {
return err
}

type Unmarshaler interface {
UnmarshalMsgpack(any) (any, error)
}

func (d *decoder) decode(rv reflect.Value, offset int) (int, error) {
k := rv.Kind()
if uv, ok := rv.Interface().(Unmarshaler); ok {
v, o, err := d.asInterface(offset, k)
if err != nil {
return 0, err
}
v, err = uv.UnmarshalMsgpack(v)
if err != nil {
return 0, err
}
rv.Set(reflect.ValueOf(v))
offset = o
return offset, nil
}
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, o, err := d.asInt(offset, k)
Expand Down
79 changes: 79 additions & 0 deletions internal/decoding/decoding_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package decoding

import (
"net"
"reflect"
"strings"
"testing"

"github.com/shamaton/msgpack/v2/def"
Expand Down Expand Up @@ -744,4 +746,81 @@ func Test_decodeWithCode(t *testing.T) {
var vv any = int8(3)
tu.Equal(t, v.V, vv)
})

t.Run("StructWithCustomDecodeMethod", func(t *testing.T) {
testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{def.Int8},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "ok",
Data: []byte{def.Int8, 3},
Expected: true,
MethodAs: method,
},
}
v := new(StructWithCustomDecodeMethod)
target = v
testcases.Run(t)
tu.Equal(t, v.A, 9)
tu.Equal(t, v.B, "Happy birthday!")
})

t.Run("CustomDecodeNetAddrWhitelist", func(t *testing.T) {
testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{def.Int8, 0},
Error: def.ErrCanNotDecode,
MethodAs: method,
},
{
Name: "ok",
Data: []byte{def.Str8, 24, '1', '9', '2', '.', '1', '6', '8', '.', '1', '.', '0', '/', '2', '4', ',', '0', '.', '0', '.', '0', '.', '0', '/', '0'},
Expected: true,
MethodAs: method,
},
}
v := new(NetAddrWhitelist)
target = v
testcases.Run(t)
tu.Equal(t, len(*v), 2)
tu.Equal(t, (*v)[0].String(), "192.168.1.0/24")
tu.Equal(t, (*v)[1].String(), "0.0.0.0/0")
})

}

type StructWithCustomDecodeMethod struct {
A int
B string
}

func (s StructWithCustomDecodeMethod) UnmarshalMsgpack(value any) (any, error) {
if v, ok := value.(int8); ok {
s.A = int(v) * 3
s.B = "Happy birthday!"
return s, nil
}
return nil, def.ErrCanNotDecode
}

type NetAddrWhitelist []net.IPNet

func (a NetAddrWhitelist) UnmarshalMsgpack(value any) (any, error) {
if v, ok := value.(string); ok {
nets := strings.Split(v, ",")
for _, netw := range nets {
_, ipnet, err := net.ParseCIDR(netw)
if err != nil {
return nil, err
}
a = append(a, *ipnet)
}
return a, nil
}
return nil, def.ErrCanNotDecode
}