[#168] object: Replace pointer slices with non-pointer slices

- []*oid.ID => []oid.ID
 - []*object.SearchFilter => []object.SearchFilter
 - []*Attribute => []Attribute

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2022-03-11 12:00:54 +03:00 committed by Alex Vanin
parent 467358f15a
commit ef96949cbf
6 changed files with 36 additions and 43 deletions

View file

@ -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) {

View file

@ -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) {

View file

@ -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

View file

@ -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
}

View file

@ -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())
}
}

View file

@ -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)
}