forked from TrueCloudLab/neoneo-go
io: add support for pointer receivers in WriteArray()
It's actually preferable to have pointer receivers for serializable types, so this should be supported.
This commit is contained in:
parent
052ba1e94f
commit
b542a5e7a0
2 changed files with 26 additions and 1 deletions
|
@ -49,7 +49,10 @@ func (w *BinWriter) WriteArray(arr interface{}) {
|
||||||
for i := 0; i < val.Len(); i++ {
|
for i := 0; i < val.Len(); i++ {
|
||||||
el, ok := val.Index(i).Interface().(encodable)
|
el, ok := val.Index(i).Interface().(encodable)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(typ.String() + "is not encodable")
|
el, ok = val.Index(i).Addr().Interface().(encodable)
|
||||||
|
if !ok {
|
||||||
|
panic(typ.String() + " is not encodable")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
el.EncodeBinary(w)
|
el.EncodeBinary(w)
|
||||||
|
|
|
@ -229,6 +229,18 @@ func (t *testSerializable) DecodeBinary(r *BinReader) {
|
||||||
r.ReadLE(t)
|
r.ReadLE(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type testPtrSerializable uint16
|
||||||
|
|
||||||
|
// EncodeBinary implements io.Serializable interface.
|
||||||
|
func (t *testPtrSerializable) EncodeBinary(w *BinWriter) {
|
||||||
|
w.WriteLE(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeBinary implements io.Serializable interface.
|
||||||
|
func (t *testPtrSerializable) DecodeBinary(r *BinReader) {
|
||||||
|
r.ReadLE(t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBinWriter_WriteArray(t *testing.T) {
|
func TestBinWriter_WriteArray(t *testing.T) {
|
||||||
var arr [3]testSerializable
|
var arr [3]testSerializable
|
||||||
for i := range arr {
|
for i := range arr {
|
||||||
|
@ -272,6 +284,16 @@ func TestBinWriter_WriteArray(t *testing.T) {
|
||||||
w.Reset()
|
w.Reset()
|
||||||
w.Err = errors.New("error")
|
w.Err = errors.New("error")
|
||||||
require.Panics(t, func() { w.WriteArray(make(chan testSerializable)) })
|
require.Panics(t, func() { w.WriteArray(make(chan testSerializable)) })
|
||||||
|
|
||||||
|
// Ptr receiver test
|
||||||
|
var arrPtr [3]testPtrSerializable
|
||||||
|
for i := range arrPtr {
|
||||||
|
arrPtr[i] = testPtrSerializable(i)
|
||||||
|
}
|
||||||
|
w.Reset()
|
||||||
|
w.WriteArray(arr[:])
|
||||||
|
require.NoError(t, w.Err)
|
||||||
|
require.Equal(t, expected, w.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBinReader_ReadArray(t *testing.T) {
|
func TestBinReader_ReadArray(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue