[#266] eacl: Allow lossless conversion of reserved filters

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-06-08 12:16:30 +03:00 committed by LeL
parent 6ac9deabb8
commit 517d7a1e4a
2 changed files with 41 additions and 1 deletions

View file

@ -41,6 +41,7 @@ const (
fKeyObjPayloadHash fKeyObjPayloadHash
fKeyObjType fKeyObjType
fKeyObjHomomorphicHash fKeyObjHomomorphicHash
fKeyObjLast // helper, used in tests
) )
func (s staticStringer) EncodeToString() string { func (s staticStringer) EncodeToString() string {
@ -113,6 +114,31 @@ func (k filterKey) String() string {
} }
} }
func (k *filterKey) fromString(s string) {
switch s {
default:
k.typ, k.str = 0, s
case v2acl.FilterObjectVersion:
k.typ, k.str = fKeyObjVersion, ""
case v2acl.FilterObjectID:
k.typ, k.str = fKeyObjID, ""
case v2acl.FilterObjectContainerID:
k.typ, k.str = fKeyObjContainerID, ""
case v2acl.FilterObjectOwnerID:
k.typ, k.str = fKeyObjOwnerID, ""
case v2acl.FilterObjectCreationEpoch:
k.typ, k.str = fKeyObjCreationEpoch, ""
case v2acl.FilterObjectPayloadLength:
k.typ, k.str = fKeyObjPayloadLength, ""
case v2acl.FilterObjectPayloadHash:
k.typ, k.str = fKeyObjPayloadHash, ""
case v2acl.FilterObjectType:
k.typ, k.str = fKeyObjType, ""
case v2acl.FilterObjectHomomorphicHash:
k.typ, k.str = fKeyObjHomomorphicHash, ""
}
}
// NewFilter creates, initializes and returns blank Filter instance. // NewFilter creates, initializes and returns blank Filter instance.
// //
// Defaults: // Defaults:
@ -134,7 +160,7 @@ func NewFilterFromV2(filter *v2acl.HeaderFilter) *Filter {
f.from = FilterHeaderTypeFromV2(filter.GetHeaderType()) f.from = FilterHeaderTypeFromV2(filter.GetHeaderType())
f.matcher = MatchFromV2(filter.GetMatchType()) f.matcher = MatchFromV2(filter.GetMatchType())
f.key.str = filter.GetKey() f.key.fromString(filter.GetKey())
f.value = staticStringer(filter.GetValue()) f.value = staticStringer(filter.GetValue())
return f return f

View file

@ -1,6 +1,7 @@
package eacl package eacl
import ( import (
"strconv"
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/v2/acl" "github.com/nspcc-dev/neofs-api-go/v2/acl"
@ -85,4 +86,17 @@ func TestFilter_ToV2(t *testing.T) {
require.Equal(t, acl.HeaderTypeUnknown, filterV2.GetHeaderType()) require.Equal(t, acl.HeaderTypeUnknown, filterV2.GetHeaderType())
require.Equal(t, acl.MatchTypeUnknown, filterV2.GetMatchType()) require.Equal(t, acl.MatchTypeUnknown, filterV2.GetMatchType())
}) })
t.Run("reserved types", func(t *testing.T) {
r := NewRecord()
for i := filterKeyType(1); i < fKeyObjLast; i++ {
r.addObjectReservedFilter(MatchStringEqual, i, staticStringer(strconv.FormatUint(uint64(i), 16)))
}
for i := range r.filters {
fv2 := r.filters[i].ToV2()
actual := NewFilterFromV2(fv2)
require.Equal(t, actual, &r.filters[i])
}
})
} }