[#155] sdk/object: Add CutPayload method to RawObject

Add CutPayload method that returns RawObject's copy with untethered empty
payload. Changing the result payload doesn't affect source object. Changing
the other fields affects source object.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-22 14:42:17 +03:00 committed by Leonard Lyubich
parent dbb5102c02
commit cecdeeac73
3 changed files with 48 additions and 0 deletions

View file

@ -120,3 +120,16 @@ func (o *RawObject) SetSessionToken(v *token.SessionToken) {
func (o *RawObject) SetType(v Type) {
o.setType(v)
}
// CutPayload returns RawObject w/ empty payload.
//
// Changes of non-payload fields affect source object.
func (o *RawObject) CutPayload() *RawObject {
if o != nil {
return &RawObject{
rwObject: o.rwObject.cutPayload(),
}
}
return nil
}

View file

@ -224,3 +224,30 @@ func TestRawObject_SetType(t *testing.T) {
require.Equal(t, typ, obj.GetType())
}
func TestRawObject_CutPayload(t *testing.T) {
o1 := NewRaw()
p1 := []byte{12, 3}
o1.SetPayload(p1)
sz := uint64(13)
o1.SetPayloadSize(sz)
o2 := o1.CutPayload()
require.Equal(t, sz, o2.GetPayloadSize())
require.Empty(t, o2.GetPayload())
sz++
o1.SetPayloadSize(sz)
require.Equal(t, sz, o1.GetPayloadSize())
require.Equal(t, sz, o2.GetPayloadSize())
p2 := []byte{4, 5, 6}
o2.SetPayload(p2)
require.Equal(t, p2, o2.GetPayload())
require.Equal(t, p1, o1.GetPayload())
}

View file

@ -303,3 +303,11 @@ func (o *rwObject) setType(t Type) {
h.SetObjectType(t.ToV2())
})
}
func (o *rwObject) cutPayload() *rwObject {
ov2 := new(object.Object)
*ov2 = *(*object.Object)(o)
ov2.SetPayload(nil)
return (*rwObject)(ov2)
}