From 2a94fdc5e70977c94100aa6bc145bd9310efcbf2 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 26 Nov 2020 13:00:03 +0300 Subject: [PATCH] [#209] Support nil value in SplitID SplitID is not set on non-split and virtual objects, so we should support this state in library. Signed-off-by: Alex Vanin --- pkg/object/splitid.go | 16 +++++++++++++--- pkg/object/splitid_test.go | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/object/splitid.go b/pkg/object/splitid.go index 7f39567..8e9eaa2 100644 --- a/pkg/object/splitid.go +++ b/pkg/object/splitid.go @@ -42,17 +42,27 @@ func (id *SplitID) Parse(s string) (err error) { } // String returns UUIDv4 string representation of SplitID. -func (id SplitID) String() string { +func (id *SplitID) String() string { + if id == nil { + return "" + } + return id.uuid.String() } // SetUUID sets pre created UUID structure as SplitID. func (id *SplitID) SetUUID(v uuid.UUID) { - id.uuid = v + if id != nil { + id.uuid = v + } } // ToV2 converts SplitID to a representation of SplitID in neofs-api v2. -func (id SplitID) ToV2() []byte { +func (id *SplitID) ToV2() []byte { + if id == nil { + return nil + } + data, _ := id.uuid.MarshalBinary() // err is always nil return data diff --git a/pkg/object/splitid_test.go b/pkg/object/splitid_test.go index 3e144eb..8adcc86 100644 --- a/pkg/object/splitid_test.go +++ b/pkg/object/splitid_test.go @@ -35,4 +35,13 @@ func TestSplitID(t *testing.T) { require.Equal(t, newUUID.String(), id.String()) }) + + t.Run("nil value", func(t *testing.T) { + var newID *object.SplitID + + require.NotPanics(t, func() { + require.Nil(t, newID.ToV2()) + require.Equal(t, "", newID.String()) + }) + }) }