[#253] container: Implement marshalers on UsedSpaceAnnouncement structure

Implement Unmarshal method on UsedSpaceAnnouncement v2 message. Implement
Marshal/Unmarshal methods on cross-version UsedSpaceAnnouncement type.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-01-28 16:01:07 +03:00 committed by Alex Vanin
parent 9e01f29dda
commit 93e1580684
4 changed files with 85 additions and 10 deletions

View file

@ -55,3 +55,23 @@ func (a *UsedSpaceAnnouncement) SetUsedSpace(value uint64) {
func (a *UsedSpaceAnnouncement) ToV2() *container.UsedSpaceAnnouncement {
return (*container.UsedSpaceAnnouncement)(a)
}
// Marshal marshals UsedSpaceAnnouncement into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (a *UsedSpaceAnnouncement) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return a.ToV2().
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of UsedSpaceAnnouncement.
func (a *UsedSpaceAnnouncement) Unmarshal(data []byte) error {
return a.ToV2().
Unmarshal(data)
}

View file

@ -1,6 +1,7 @@
package container_test
import (
"crypto/sha256"
"testing"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
@ -47,3 +48,24 @@ func TestAnnouncement(t *testing.T) {
require.Equal(t, container.NewIDFromV2(newCID), newA.ContainerID())
})
}
func TestUsedSpaceEncoding(t *testing.T) {
a := container.NewAnnouncement()
a.SetUsedSpace(13)
a.SetEpoch(666)
id := container.NewID()
id.SetSHA256([sha256.Size]byte{1, 2, 3})
a.SetContainerID(id)
t.Run("binary", func(t *testing.T) {
data, err := a.Marshal()
require.NoError(t, err)
a2 := container.NewAnnouncement()
require.NoError(t, a2.Unmarshal(data))
require.Equal(t, a, a2)
})
}