mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-25 23:42:23 +00:00
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
|
@ -48,9 +48,12 @@ func (w *BinWriter) WriteArray(arr interface{}) {
|
|||
w.WriteVarUint(uint64(val.Len()))
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
el, ok := val.Index(i).Interface().(encodable)
|
||||
if !ok {
|
||||
el, ok = val.Index(i).Addr().Interface().(encodable)
|
||||
if !ok {
|
||||
panic(typ.String() + " is not encodable")
|
||||
}
|
||||
}
|
||||
|
||||
el.EncodeBinary(w)
|
||||
}
|
||||
|
|
|
@ -229,6 +229,18 @@ func (t *testSerializable) DecodeBinary(r *BinReader) {
|
|||
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) {
|
||||
var arr [3]testSerializable
|
||||
for i := range arr {
|
||||
|
@ -272,6 +284,16 @@ func TestBinWriter_WriteArray(t *testing.T) {
|
|||
w.Reset()
|
||||
w.Err = errors.New("error")
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue