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

(Un)Marshaling structs with internal Binary(Un)Marshaler support #64

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
protobuf-fuzz.zip
workdir
workdir
vendor
2 changes: 2 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ func (de *decoder) putvalue(wiretype int, val reflect.Value,
t := time.Unix(sv/int64(time.Second), sv%int64(time.Second))
val.Set(reflect.ValueOf(t))
return nil
} else if enc, ok := val.Addr().Interface().(encoding.BinaryUnmarshaler); ok {
return enc.UnmarshalBinary(vb[:])
}
if wiretype != 2 {
return errors.New("bad wiretype for embedded message")
Expand Down
26 changes: 19 additions & 7 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,25 @@ func (en *encoder) value(key uint64, val reflect.Value, prefix TagPrefix) {
en.Write(b)

case reflect.Struct:
// Embedded messages.
en.uvarint(key | 2)
emb := encoder{}
emb.message(val)
b := emb.Bytes()
en.uvarint(uint64(len(b)))
en.Write(b)
if enc, ok := val.Interface().(encoding.BinaryMarshaler); ok {
en.uvarint(key | 2)
b, err := enc.MarshalBinary()
if err != nil {
panic(err.Error())
}

en.uvarint(uint64(len(b)))
en.Write(b)

} else {
// Embedded messages.
en.uvarint(key | 2)
emb := encoder{}
emb.message(val)
b := emb.Bytes()
en.uvarint(uint64(len(b)))
en.Write(b)
}

case reflect.Slice, reflect.Array:
// Length-delimited slices or byte-vectors.
Expand Down
36 changes: 36 additions & 0 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,39 @@ func TestInterface_UnknownType(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "no constructor")
}

type Cid struct{ str string }

type Block struct {
Cid Cid
A int
}

func (c Cid) MarshalBinary() ([]byte, error) {
return []byte(c.str), nil
}

func (c *Cid) UnmarshalBinary(data []byte) error {
c.str = string(data)
return nil
}

func TestBinaryMarshalerStruct(t *testing.T) {
c1 := Cid{"1234"}
buf, err := Encode(&c1)
assert.Nil(t, err)

c2 := Cid{}
err = Decode(buf, &c2)
assert.Nil(t, err)
assert.Equal(t, c1, c2)

b1 := Block{Cid: Cid{"1234"}, A: 4}
buf, err = Encode(&b1)
assert.Nil(t, err)

b2 := Block{}
err = Decode(buf, &b2)
assert.Nil(t, err)
assert.Equal(t, b1, b2)
}