object: Add UserAttributes
method #272
4 changed files with 48 additions and 8 deletions
|
@ -356,8 +356,7 @@ func (x Container) IterateUserAttributes(f func(key, val string)) {
|
||||||
attrs := x.v2.GetAttributes()
|
attrs := x.v2.GetAttributes()
|
||||||
for _, attr := range attrs {
|
for _, attr := range attrs {
|
||||||
key := attr.GetKey()
|
key := attr.GetKey()
|
||||||
if !strings.HasPrefix(key, container.SysAttributePrefix) &&
|
if !strings.HasPrefix(key, container.SysAttributePrefix) {
|
||||||
!strings.HasPrefix(key, container.SysAttributePrefixNeoFS) {
|
|
||||||
f(key, attr.GetValue())
|
f(key, attr.GetValue())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -417,8 +416,7 @@ func DisableHomomorphicHashing(cnr *Container) {
|
||||||
//
|
//
|
||||||
// Zero Container has enabled hashing.
|
// Zero Container has enabled hashing.
|
||||||
func IsHomomorphicHashingDisabled(cnr Container) bool {
|
func IsHomomorphicHashingDisabled(cnr Container) bool {
|
||||||
return cnr.Attribute(container.SysAttributeHomomorphicHashing) == attributeHomoHashEnabled ||
|
return cnr.Attribute(container.SysAttributeHomomorphicHashing) == attributeHomoHashEnabled
|
||||||
cnr.Attribute(container.SysAttributeHomomorphicHashingNeoFS) == attributeHomoHashEnabled
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Domain represents information about container domain registered in the NNS
|
// Domain represents information about container domain registered in the NNS
|
||||||
|
@ -467,9 +465,6 @@ func ReadDomain(cnr Container) (res Domain) {
|
||||||
if name := cnr.Attribute(container.SysAttributeName); name != "" {
|
if name := cnr.Attribute(container.SysAttributeName); name != "" {
|
||||||
res.SetName(name)
|
res.SetName(name)
|
||||||
res.SetZone(cnr.Attribute(container.SysAttributeZone))
|
res.SetZone(cnr.Attribute(container.SysAttributeZone))
|
||||||
} else if name = cnr.Attribute(container.SysAttributeNameNeoFS); name != "" {
|
|
||||||
res.SetName(name)
|
|
||||||
res.SetZone(cnr.Attribute(container.SysAttributeZoneNeoFS))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
@ -150,7 +150,7 @@ func assertContainsAttribute(t *testing.T, m v2container.Container, key, val str
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainer_Attribute(t *testing.T) {
|
func TestContainer_Attribute(t *testing.T) {
|
||||||
const attrKey1, attrKey2 = v2container.SysAttributePrefix + "key1", v2container.SysAttributePrefixNeoFS + "key2"
|
const attrKey1, attrKey2 = v2container.SysAttributePrefix + "key1", v2container.SysAttributePrefix + "key2"
|
||||||
const attrVal1, attrVal2 = "val1", "val2"
|
const attrVal1, attrVal2 = "val1", "val2"
|
||||||
|
|
||||||
val := containertest.Container()
|
val := containertest.Container()
|
||||||
|
|
|
@ -3,7 +3,10 @@ package object
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
||||||
v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
||||||
|
@ -312,6 +315,23 @@ func (o *Object) Attributes() []Attribute {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserAttributes returns object user attributes.
|
||||||
|
func (o *Object) UserAttributes() []Attribute {
|
||||||
|
attrs := (*object.Object)(o).
|
||||||
|
GetHeader().
|
||||||
|
GetAttributes()
|
||||||
|
|
||||||
|
res := make([]Attribute, 0, len(attrs))
|
||||||
|
|
||||||
|
for _, attr := range attrs {
|
||||||
|
if !strings.HasPrefix(attr.GetKey(), container.SysAttributePrefix) {
|
||||||
|
res = append(res, *NewAttributeFromV2(&attr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return slices.Clip(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, len(v))
|
attrs := make([]object.Attribute, len(v))
|
||||||
|
|
|
@ -3,8 +3,10 @@ package object_test
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
v2container "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
||||||
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
||||||
|
objecttest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/test"
|
||||||
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -24,3 +26,26 @@ func TestInitCreation(t *testing.T) {
|
||||||
require.Equal(t, cnr, cID)
|
require.Equal(t, cnr, cID)
|
||||||
require.Equal(t, own, o.OwnerID())
|
require.Equal(t, own, o.OwnerID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Attributes(t *testing.T) {
|
||||||
|
obj := objecttest.Object()
|
||||||
|
|
||||||
|
t.Run("get user attributes", func(t *testing.T) {
|
||||||
|
// See how we create a test object. It's created with two attributes.
|
||||||
|
require.Len(t, obj.UserAttributes(), 2)
|
||||||
|
})
|
||||||
|
|
||||||
|
userAttrs := obj.UserAttributes()
|
||||||
|
|
||||||
|
sysAttr := *object.NewAttribute()
|
||||||
|
sysAttr.SetKey(v2container.SysAttributePrefix + "key")
|
||||||
|
sysAttr.SetValue("value")
|
||||||
|
|
||||||
|
attr := append(userAttrs, sysAttr)
|
||||||
|
obj.SetAttributes(attr...)
|
||||||
|
|
||||||
|
t.Run("get attributes", func(t *testing.T) {
|
||||||
|
require.ElementsMatch(t, obj.UserAttributes(), userAttrs)
|
||||||
|
require.ElementsMatch(t, obj.Attributes(), attr)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue