From e222c441e55b6bba9a6afa824aabb424bac7cb80 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 2 Sep 2020 14:46:43 +0300 Subject: [PATCH] [#132] v2/object: Implement stable unmarshaler on Object Signed-off-by: Leonard Lyubich --- v2/object/marshal.go | 16 ++++++++++++++++ v2/object/marshal_test.go | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/v2/object/marshal.go b/v2/object/marshal.go index 79bd131..f19c54f 100644 --- a/v2/object/marshal.go +++ b/v2/object/marshal.go @@ -2,6 +2,7 @@ package object import ( "github.com/nspcc-dev/neofs-api-go/util/proto" + object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" ) const ( @@ -440,6 +441,21 @@ func (o *Object) StableSize() (size int) { return size } +func (o *Object) StableUnmarshal(data []byte) error { + if o == nil { + return nil + } + + objGRPC := new(object.Object) + if err := objGRPC.Unmarshal(data); err != nil { + return err + } + + *o = *ObjectFromGRPCMessage(objGRPC) + + return nil +} + func (r *GetRequestBody) StableMarshal(buf []byte) ([]byte, error) { if r == nil { return []byte{}, nil diff --git a/v2/object/marshal_test.go b/v2/object/marshal_test.go index 19ab6d5..04f3a39 100644 --- a/v2/object/marshal_test.go +++ b/v2/object/marshal_test.go @@ -688,3 +688,15 @@ func generateRangeHashResponseBody(n int) *object.GetRangeHashResponseBody { return resp } + +func TestObject_StableUnmarshal(t *testing.T) { + obj := generateObject("some data") + + data, err := obj.StableMarshal(nil) + require.NoError(t, err) + + obj2 := new(object.Object) + require.NoError(t, obj2.StableUnmarshal(data)) + + require.Equal(t, obj, obj2) +}