From ef96949cbf5d1affaf54654165e3ae5cbe3fb29d Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 11 Mar 2022 12:00:54 +0300 Subject: [PATCH] [#168] object: Replace pointer slices with non-pointer slices - []*oid.ID => []oid.ID - []*object.SearchFilter => []object.SearchFilter - []*Attribute => []Attribute Signed-off-by: Alex Vanin --- object/object.go | 24 ++++++++++++------------ object/raw_test.go | 8 ++++---- object/search.go | 21 +++++++-------------- object/test/generate.go | 6 +++--- object/tombstone.go | 14 +++++++------- object/tombstone_test.go | 6 +++--- 6 files changed, 36 insertions(+), 43 deletions(-) diff --git a/object/object.go b/object/object.go index ce56c4f..ac39437 100644 --- a/object/object.go +++ b/object/object.go @@ -229,26 +229,26 @@ func (o *Object) SetPayloadHomomorphicHash(v *checksum.Checksum) { } // Attributes returns object attributes. -func (o *Object) Attributes() []*Attribute { +func (o *Object) Attributes() []Attribute { attrs := (*object.Object)(o). GetHeader(). GetAttributes() - res := make([]*Attribute, 0, len(attrs)) + res := make([]Attribute, len(attrs)) for i := range attrs { - res = append(res, NewAttributeFromV2(attrs[i])) + res[i] = *NewAttributeFromV2(&attrs[i]) } return res } // SetAttributes sets object attributes. -func (o *Object) SetAttributes(v ...*Attribute) { - attrs := make([]*object.Attribute, 0, len(v)) +func (o *Object) SetAttributes(v ...Attribute) { + attrs := make([]object.Attribute, len(v)) for i := range v { - attrs = append(attrs, v[i].ToV2()) + attrs[i] = *v[i].ToV2() } o.setHeaderField(func(h *object.Header) { @@ -274,27 +274,27 @@ func (o *Object) SetPreviousID(v *oid.ID) { } // Children return list of the identifiers of the child objects. -func (o *Object) Children() []*oid.ID { +func (o *Object) Children() []oid.ID { ids := (*object.Object)(o). GetHeader(). GetSplit(). GetChildren() - res := make([]*oid.ID, 0, len(ids)) + res := make([]oid.ID, len(ids)) for i := range ids { - res = append(res, oid.NewIDFromV2(ids[i])) + res[i] = *oid.NewIDFromV2(&ids[i]) } return res } // SetChildren sets list of the identifiers of the child objects. -func (o *Object) SetChildren(v ...*oid.ID) { - ids := make([]*refs.ObjectID, 0, len(v)) +func (o *Object) SetChildren(v ...oid.ID) { + ids := make([]refs.ObjectID, len(v)) for i := range v { - ids = append(ids, v[i].ToV2()) + ids[i] = *v[i].ToV2() } o.setSplitFields(func(split *object.SplitHeader) { diff --git a/object/raw_test.go b/object/raw_test.go index a05e4ad..4c6b9ab 100644 --- a/object/raw_test.go +++ b/object/raw_test.go @@ -152,9 +152,9 @@ func TestObject_SetAttributes(t *testing.T) { a2.SetKey("key2") a2.SetValue("val2") - obj.SetAttributes(a1, a2) + obj.SetAttributes(*a1, *a2) - require.Equal(t, []*Attribute{a1, a2}, obj.Attributes()) + require.Equal(t, []Attribute{*a1, *a2}, obj.Attributes()) } func TestObject_SetPreviousID(t *testing.T) { @@ -173,9 +173,9 @@ func TestObject_SetChildren(t *testing.T) { id1 := randID(t) id2 := randID(t) - obj.SetChildren(id1, id2) + obj.SetChildren(*id1, *id2) - require.Equal(t, []*oid.ID{id1, id2}, obj.Children()) + require.Equal(t, []oid.ID{*id1, *id2}, obj.Children()) } func TestObject_SetSplitID(t *testing.T) { diff --git a/object/search.go b/object/search.go index 240a65d..481e11c 100644 --- a/object/search.go +++ b/object/search.go @@ -172,14 +172,10 @@ func NewSearchFilters() SearchFilters { return SearchFilters{} } -func NewSearchFiltersFromV2(v2 []*v2object.SearchFilter) SearchFilters { +func NewSearchFiltersFromV2(v2 []v2object.SearchFilter) SearchFilters { filters := make(SearchFilters, 0, len(v2)) for i := range v2 { - if v2[i] == nil { - continue - } - filters.AddFilter( v2[i].GetKey(), v2[i].GetValue(), @@ -236,16 +232,13 @@ func (f *SearchFilters) AddNotificationEpochFilter(epoch uint64) { f.addFilter(MatchStringEqual, 0, v2object.SysAttributeTickEpoch, staticStringer(strconv.FormatUint(epoch, 10))) } -func (f SearchFilters) ToV2() []*v2object.SearchFilter { - result := make([]*v2object.SearchFilter, 0, len(f)) +func (f SearchFilters) ToV2() []v2object.SearchFilter { + result := make([]v2object.SearchFilter, len(f)) for i := range f { - v2 := new(v2object.SearchFilter) - v2.SetKey(f[i].header.String()) - v2.SetValue(f[i].value.String()) - v2.SetMatchType(f[i].op.ToV2()) - - result = append(result, v2) + result[i].SetKey(f[i].header.String()) + result[i].SetValue(f[i].value.String()) + result[i].SetMatchType(f[i].op.ToV2()) } return result @@ -293,7 +286,7 @@ func (f *SearchFilters) MarshalJSON() ([]byte, error) { // UnmarshalJSON decodes SearchFilters from protobuf JSON format. func (f *SearchFilters) UnmarshalJSON(data []byte) error { - var fsV2 []*v2object.SearchFilter + var fsV2 []v2object.SearchFilter if err := json.Unmarshal(data, &fsV2); err != nil { return err diff --git a/object/test/generate.go b/object/test/generate.go index 0c8e9c2..7af9347 100644 --- a/object/test/generate.go +++ b/object/test/generate.go @@ -56,8 +56,8 @@ func generate(withParent bool) *object.Object { x.SetCreationEpoch(222) x.SetPreviousID(oidtest.ID()) x.SetParentID(oidtest.ID()) - x.SetChildren(oidtest.ID(), oidtest.ID()) - x.SetAttributes(Attribute(), Attribute()) + x.SetChildren(*oidtest.ID(), *oidtest.ID()) + x.SetAttributes(*Attribute(), *Attribute()) x.SetSplitID(SplitID()) x.SetPayloadChecksum(checksumtest.Checksum()) x.SetPayloadHomomorphicHash(checksumtest.Checksum()) @@ -87,7 +87,7 @@ func Tombstone() *object.Tombstone { x.SetSplitID(SplitID()) x.SetExpirationEpoch(13) - x.SetMembers([]*oid.ID{oidtest.ID(), oidtest.ID()}) + x.SetMembers([]oid.ID{*oidtest.ID(), *oidtest.ID()}) return x } diff --git a/object/tombstone.go b/object/tombstone.go index 43fa62e..03bb05e 100644 --- a/object/tombstone.go +++ b/object/tombstone.go @@ -55,7 +55,7 @@ func (t *Tombstone) SetSplitID(v *SplitID) { } // Members returns list of objects to be deleted. -func (t *Tombstone) Members() []*oid.ID { +func (t *Tombstone) Members() []oid.ID { msV2 := (*tombstone.Tombstone)(t). GetMembers() @@ -63,18 +63,18 @@ func (t *Tombstone) Members() []*oid.ID { return nil } - ms := make([]*oid.ID, 0, len(msV2)) + ms := make([]oid.ID, len(msV2)) for i := range msV2 { - ms = append(ms, oid.NewIDFromV2(msV2[i])) + ms[i] = *oid.NewIDFromV2(&msV2[i]) } return ms } // SetMembers sets list of objects to be deleted. -func (t *Tombstone) SetMembers(v []*oid.ID) { - var ms []*refs.ObjectID +func (t *Tombstone) SetMembers(v []oid.ID) { + var ms []refs.ObjectID if v != nil { ms = (*tombstone.Tombstone)(t). @@ -83,11 +83,11 @@ func (t *Tombstone) SetMembers(v []*oid.ID) { if ln := len(v); cap(ms) >= ln { ms = ms[:0] } else { - ms = make([]*refs.ObjectID, 0, ln) + ms = make([]refs.ObjectID, 0, ln) } for i := range v { - ms = append(ms, v[i].ToV2()) + ms = append(ms, *v[i].ToV2()) } } diff --git a/object/tombstone_test.go b/object/tombstone_test.go index 48adf7f..f156353 100644 --- a/object/tombstone_test.go +++ b/object/tombstone_test.go @@ -10,12 +10,12 @@ import ( "github.com/stretchr/testify/require" ) -func generateIDList(sz int) []*oid.ID { - res := make([]*oid.ID, sz) +func generateIDList(sz int) []oid.ID { + res := make([]oid.ID, sz) cs := [sha256.Size]byte{} for i := 0; i < sz; i++ { - res[i] = oid.NewID() + res[i] = *oid.NewID() rand.Read(cs[:]) res[i].SetSHA256(cs) }