From acbdbd410e0be911b801c55849447861ca1b1969 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 2 Sep 2020 14:51:29 +0300 Subject: [PATCH] [#132] sdk/object: Implement useful methods and function Implement method to get address of the object. Implement method to copy the object w/o payload. Implement function of object deserialization. Signed-off-by: Leonard Lyubich --- pkg/object/object.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pkg/object/object.go b/pkg/object/object.go index 22688dd5..be1f5b8b 100644 --- a/pkg/object/object.go +++ b/pkg/object/object.go @@ -77,6 +77,19 @@ func (o *Object) Verify() error { return nil } +// Address returns address of the object. +func (o *Object) Address() *Address { + if o != nil { + return &Address{ + cid: o.cid, + oid: o.id, + } + } + + return nil +} + +// GetPayload returns object payload bytes. func (o *Object) GetPayload() []byte { if o != nil { return o.payload @@ -85,6 +98,25 @@ func (o *Object) GetPayload() []byte { return nil } +// CutPayload copies object fields w/o payload. +func (o *Object) CutPayload() *Object { + if o != nil { + return &Object{ + rwObject: rwObject{ + fin: o.fin, + id: o.id, + key: o.key, + sig: o.sig, + cid: o.cid, + ownerID: o.ownerID, + payloadChecksum: o.payloadChecksum, + }, + } + } + + return nil +} + // ToV2 converts object to v2 Object message. func (o *Object) ToV2() *object.Object { obj, _ := o.rwObject.ToV2(nil) @@ -210,3 +242,13 @@ func FromV2(oV2 *object.Object) (*Object, error) { }, }, nil } + +// FromBytes restores Object from a binary representation. +func FromBytes(data []byte) (*Object, error) { + oV2 := new(object.Object) + if err := oV2.StableUnmarshal(data); err != nil { + return nil, errors.Wrap(err, "could not unmarshal object") + } + + return FromV2(oV2) +}