From ae81f4584e9ad0faf659dfdcbd183aee14cc018d Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 30 Sep 2020 18:39:27 +0300 Subject: [PATCH] [#164] sdk/object: Add parent ID getter and setter Signed-off-by: Leonard Lyubich --- pkg/object/raw.go | 5 +++++ pkg/object/raw_test.go | 9 +++++++++ pkg/object/rw.go | 16 ++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/pkg/object/raw.go b/pkg/object/raw.go index a6f3e9d8..bb7401bb 100644 --- a/pkg/object/raw.go +++ b/pkg/object/raw.go @@ -110,6 +110,11 @@ func (o *RawObject) SetChildren(v ...*ID) { o.setChildren(v...) } +// SetParentID sets identifier of the parent object. +func (o *RawObject) SetParentID(v *ID) { + o.setParentID(v) +} + // SetParent sets parent object w/o payload. func (o *RawObject) SetParent(v *Object) { o.setParent(v) diff --git a/pkg/object/raw_test.go b/pkg/object/raw_test.go index ec5563ee..2cc32462 100644 --- a/pkg/object/raw_test.go +++ b/pkg/object/raw_test.go @@ -251,3 +251,12 @@ func TestRawObject_CutPayload(t *testing.T) { require.Equal(t, p2, o2.GetPayload()) require.Equal(t, p1, o1.GetPayload()) } + +func TestRawObject_SetParentID(t *testing.T) { + obj := NewRaw() + + id := randID(t) + obj.setParentID(id) + + require.Equal(t, id, obj.GetParentID()) +} diff --git a/pkg/object/rw.go b/pkg/object/rw.go index bb3f2c0d..b223307d 100644 --- a/pkg/object/rw.go +++ b/pkg/object/rw.go @@ -251,6 +251,22 @@ func (o *rwObject) setChildren(v ...*ID) { }) } +// GetParentID returns identifier of the parent object. +func (o *rwObject) GetParentID() *ID { + return NewIDFromV2( + (*object.Object)(o). + GetHeader(). + GetSplit(). + GetParent(), + ) +} + +func (o *rwObject) setParentID(v *ID) { + o.setSplitFields(func(split *object.SplitHeader) { + split.SetParent(v.ToV2()) + }) +} + // GetParent returns parent object w/o payload. func (o *rwObject) GetParent() *Object { h := (*object.Object)(o).