[#132] v2/object: Implement stable unmarshaler on Object

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-02 14:46:43 +03:00 committed by Stanislav Bogatyrev
parent c125749c6e
commit e222c441e5
2 changed files with 28 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package object
import ( import (
"github.com/nspcc-dev/neofs-api-go/util/proto" "github.com/nspcc-dev/neofs-api-go/util/proto"
object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
) )
const ( const (
@ -440,6 +441,21 @@ func (o *Object) StableSize() (size int) {
return size 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) { func (r *GetRequestBody) StableMarshal(buf []byte) ([]byte, error) {
if r == nil { if r == nil {
return []byte{}, nil return []byte{}, nil

View file

@ -688,3 +688,15 @@ func generateRangeHashResponseBody(n int) *object.GetRangeHashResponseBody {
return resp 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)
}