[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-02 14:51:29 +03:00 committed by Stanislav Bogatyrev
parent f18e8535c5
commit acbdbd410e

View file

@ -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)
}