[#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. // Attributes returns object attributes.
func (o *Object) Attributes() []*Attribute { func (o *Object) Attributes() []Attribute {
attrs := (*object.Object)(o). attrs := (*object.Object)(o).
GetHeader(). GetHeader().
GetAttributes() GetAttributes()
res := make([]*Attribute, 0, len(attrs)) res := make([]Attribute, len(attrs))
for i := range attrs { for i := range attrs {
res = append(res, NewAttributeFromV2(attrs[i])) res[i] = *NewAttributeFromV2(&attrs[i])
} }
return res return res
} }
// SetAttributes sets object attributes. // SetAttributes sets object attributes.
func (o *Object) SetAttributes(v ...*Attribute) { func (o *Object) SetAttributes(v ...Attribute) {
attrs := make([]*object.Attribute, 0, len(v)) attrs := make([]object.Attribute, len(v))
for i := range v { for i := range v {
attrs = append(attrs, v[i].ToV2()) attrs[i] = *v[i].ToV2()
} }
o.setHeaderField(func(h *object.Header) { 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. // 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). ids := (*object.Object)(o).
GetHeader(). GetHeader().
GetSplit(). GetSplit().
GetChildren() GetChildren()
res := make([]*oid.ID, 0, len(ids)) res := make([]oid.ID, len(ids))
for i := range ids { for i := range ids {
res = append(res, oid.NewIDFromV2(ids[i])) res[i] = *oid.NewIDFromV2(&ids[i])
} }
return res return res
} }
// SetChildren sets list of the identifiers of the child objects. // SetChildren sets list of the identifiers of the child objects.
func (o *Object) SetChildren(v ...*oid.ID) { func (o *Object) SetChildren(v ...oid.ID) {
ids := make([]*refs.ObjectID, 0, len(v)) ids := make([]refs.ObjectID, len(v))
for i := range v { for i := range v {
ids = append(ids, v[i].ToV2()) ids[i] = *v[i].ToV2()
} }
o.setSplitFields(func(split *object.SplitHeader) { o.setSplitFields(func(split *object.SplitHeader) {

View file

@ -152,9 +152,9 @@ func TestObject_SetAttributes(t *testing.T) {
a2.SetKey("key2") a2.SetKey("key2")
a2.SetValue("val2") 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) { func TestObject_SetPreviousID(t *testing.T) {
@ -173,9 +173,9 @@ func TestObject_SetChildren(t *testing.T) {
id1 := randID(t) id1 := randID(t)
id2 := 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) { func TestObject_SetSplitID(t *testing.T) {

View file

@ -172,14 +172,10 @@ func NewSearchFilters() SearchFilters {
return SearchFilters{} return SearchFilters{}
} }
func NewSearchFiltersFromV2(v2 []*v2object.SearchFilter) SearchFilters { func NewSearchFiltersFromV2(v2 []v2object.SearchFilter) SearchFilters {
filters := make(SearchFilters, 0, len(v2)) filters := make(SearchFilters, 0, len(v2))
for i := range v2 { for i := range v2 {
if v2[i] == nil {
continue
}
filters.AddFilter( filters.AddFilter(
v2[i].GetKey(), v2[i].GetKey(),
v2[i].GetValue(), v2[i].GetValue(),
@ -236,16 +232,13 @@ func (f *SearchFilters) AddNotificationEpochFilter(epoch uint64) {
f.addFilter(MatchStringEqual, 0, v2object.SysAttributeTickEpoch, staticStringer(strconv.FormatUint(epoch, 10))) f.addFilter(MatchStringEqual, 0, v2object.SysAttributeTickEpoch, staticStringer(strconv.FormatUint(epoch, 10)))
} }
func (f SearchFilters) ToV2() []*v2object.SearchFilter { func (f SearchFilters) ToV2() []v2object.SearchFilter {
result := make([]*v2object.SearchFilter, 0, len(f)) result := make([]v2object.SearchFilter, len(f))
for i := range f { for i := range f {
v2 := new(v2object.SearchFilter) result[i].SetKey(f[i].header.String())
v2.SetKey(f[i].header.String()) result[i].SetValue(f[i].value.String())
v2.SetValue(f[i].value.String()) result[i].SetMatchType(f[i].op.ToV2())
v2.SetMatchType(f[i].op.ToV2())
result = append(result, v2)
} }
return result return result
@ -293,7 +286,7 @@ func (f *SearchFilters) MarshalJSON() ([]byte, error) {
// UnmarshalJSON decodes SearchFilters from protobuf JSON format. // UnmarshalJSON decodes SearchFilters from protobuf JSON format.
func (f *SearchFilters) UnmarshalJSON(data []byte) error { func (f *SearchFilters) UnmarshalJSON(data []byte) error {
var fsV2 []*v2object.SearchFilter var fsV2 []v2object.SearchFilter
if err := json.Unmarshal(data, &fsV2); err != nil { if err := json.Unmarshal(data, &fsV2); err != nil {
return err return err

View file

@ -56,8 +56,8 @@ func generate(withParent bool) *object.Object {
x.SetCreationEpoch(222) x.SetCreationEpoch(222)
x.SetPreviousID(oidtest.ID()) x.SetPreviousID(oidtest.ID())
x.SetParentID(oidtest.ID()) x.SetParentID(oidtest.ID())
x.SetChildren(oidtest.ID(), oidtest.ID()) x.SetChildren(*oidtest.ID(), *oidtest.ID())
x.SetAttributes(Attribute(), Attribute()) x.SetAttributes(*Attribute(), *Attribute())
x.SetSplitID(SplitID()) x.SetSplitID(SplitID())
x.SetPayloadChecksum(checksumtest.Checksum()) x.SetPayloadChecksum(checksumtest.Checksum())
x.SetPayloadHomomorphicHash(checksumtest.Checksum()) x.SetPayloadHomomorphicHash(checksumtest.Checksum())
@ -87,7 +87,7 @@ func Tombstone() *object.Tombstone {
x.SetSplitID(SplitID()) x.SetSplitID(SplitID())
x.SetExpirationEpoch(13) x.SetExpirationEpoch(13)
x.SetMembers([]*oid.ID{oidtest.ID(), oidtest.ID()}) x.SetMembers([]oid.ID{*oidtest.ID(), *oidtest.ID()})
return x return x
} }

View file

@ -55,7 +55,7 @@ func (t *Tombstone) SetSplitID(v *SplitID) {
} }
// Members returns list of objects to be deleted. // Members returns list of objects to be deleted.
func (t *Tombstone) Members() []*oid.ID { func (t *Tombstone) Members() []oid.ID {
msV2 := (*tombstone.Tombstone)(t). msV2 := (*tombstone.Tombstone)(t).
GetMembers() GetMembers()
@ -63,18 +63,18 @@ func (t *Tombstone) Members() []*oid.ID {
return nil return nil
} }
ms := make([]*oid.ID, 0, len(msV2)) ms := make([]oid.ID, len(msV2))
for i := range msV2 { for i := range msV2 {
ms = append(ms, oid.NewIDFromV2(msV2[i])) ms[i] = *oid.NewIDFromV2(&msV2[i])
} }
return ms return ms
} }
// SetMembers sets list of objects to be deleted. // SetMembers sets list of objects to be deleted.
func (t *Tombstone) SetMembers(v []*oid.ID) { func (t *Tombstone) SetMembers(v []oid.ID) {
var ms []*refs.ObjectID var ms []refs.ObjectID
if v != nil { if v != nil {
ms = (*tombstone.Tombstone)(t). ms = (*tombstone.Tombstone)(t).
@ -83,11 +83,11 @@ func (t *Tombstone) SetMembers(v []*oid.ID) {
if ln := len(v); cap(ms) >= ln { if ln := len(v); cap(ms) >= ln {
ms = ms[:0] ms = ms[:0]
} else { } else {
ms = make([]*refs.ObjectID, 0, ln) ms = make([]refs.ObjectID, 0, ln)
} }
for i := range v { 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" "github.com/stretchr/testify/require"
) )
func generateIDList(sz int) []*oid.ID { func generateIDList(sz int) []oid.ID {
res := make([]*oid.ID, sz) res := make([]oid.ID, sz)
cs := [sha256.Size]byte{} cs := [sha256.Size]byte{}
for i := 0; i < sz; i++ { for i := 0; i < sz; i++ {
res[i] = oid.NewID() res[i] = *oid.NewID()
rand.Read(cs[:]) rand.Read(cs[:])
res[i].SetSHA256(cs) res[i].SetSHA256(cs)
} }