From 29bdb885aad58f72570600ea2b9996746bd94377 Mon Sep 17 00:00:00 2001 From: Aleksey Savchuk Date: Mon, 16 Sep 2024 14:59:30 +0300 Subject: [PATCH] [#271] object: Add `UserAttributes` method Signed-off-by: Aleksey Savchuk --- object/object.go | 23 +++++++++++++++++++++++ object/object_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/object/object.go b/object/object.go index af16128..472e6fd 100644 --- a/object/object.go +++ b/object/object.go @@ -3,7 +3,10 @@ package object import ( "errors" "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/refs" v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session" @@ -312,6 +315,26 @@ func (o *Object) Attributes() []Attribute { 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 { + key := attr.GetKey() + + if !strings.HasPrefix(key, container.SysAttributePrefix) && + !strings.HasPrefix(key, container.SysAttributePrefixNeoFS) { + res = append(res, *NewAttributeFromV2(&attr)) + } + } + + return slices.Clip(res) +} + // SetAttributes sets object attributes. func (o *Object) SetAttributes(v ...Attribute) { attrs := make([]object.Attribute, len(v)) diff --git a/object/object_test.go b/object/object_test.go index 5e42352..f7cdc8b 100644 --- a/object/object_test.go +++ b/object/object_test.go @@ -3,9 +3,12 @@ package object_test import ( "testing" + v2container "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container" cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test" "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" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -24,3 +27,35 @@ func TestInitCreation(t *testing.T) { require.Equal(t, cnr, cID) require.Equal(t, own, o.OwnerID()) } + +func Test_Attributes(t *testing.T) { + obj := objecttest.Object() + + var ( + attr *object.Attribute + sysAttrs []object.Attribute + ) + + attr = object.NewAttribute() + attr.SetKey(v2container.SysAttributePrefix + "key") + attr.SetValue("value") + sysAttrs = append(sysAttrs, *attr) + + attr = object.NewAttribute() + attr.SetKey(v2container.SysAttributePrefixNeoFS + "key") + attr.SetValue("value") + sysAttrs = append(sysAttrs, *attr) + + userAttrs := obj.Attributes() + obj.SetAttributes(append(userAttrs, sysAttrs...)...) + + t.Run("get attributes", func(t *testing.T) { + attrs := obj.Attributes() + assert.ElementsMatch(t, attrs, append(userAttrs, sysAttrs...)) + }) + + t.Run("get user attributes", func(t *testing.T) { + attrs := obj.UserAttributes() + assert.ElementsMatch(t, attrs, userAttrs) + }) +}