From cecdeeac73187f16a239accc35545bbdc2244a69 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 22 Sep 2020 14:42:17 +0300 Subject: [PATCH] [#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 --- pkg/object/raw.go | 13 +++++++++++++ pkg/object/raw_test.go | 27 +++++++++++++++++++++++++++ pkg/object/rw.go | 8 ++++++++ 3 files changed, 48 insertions(+) diff --git a/pkg/object/raw.go b/pkg/object/raw.go index 6930b6a..0ff3ca0 100644 --- a/pkg/object/raw.go +++ b/pkg/object/raw.go @@ -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 +} diff --git a/pkg/object/raw_test.go b/pkg/object/raw_test.go index 531789a..ec5563e 100644 --- a/pkg/object/raw_test.go +++ b/pkg/object/raw_test.go @@ -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()) +} diff --git a/pkg/object/rw.go b/pkg/object/rw.go index e2575d7..bb3f2c0 100644 --- a/pkg/object/rw.go +++ b/pkg/object/rw.go @@ -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) +}