From 517d7a1e4a0f152204c9207944fd960ea3db94c4 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 8 Jun 2022 12:16:30 +0300 Subject: [PATCH] [#266] eacl: Allow lossless conversion of reserved filters Signed-off-by: Evgenii Stratonikov --- eacl/filter.go | 28 +++++++++++++++++++++++++++- eacl/filter_test.go | 14 ++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/eacl/filter.go b/eacl/filter.go index 1885443..0a32ba5 100644 --- a/eacl/filter.go +++ b/eacl/filter.go @@ -41,6 +41,7 @@ const ( fKeyObjPayloadHash fKeyObjType fKeyObjHomomorphicHash + fKeyObjLast // helper, used in tests ) 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. // // Defaults: @@ -134,7 +160,7 @@ func NewFilterFromV2(filter *v2acl.HeaderFilter) *Filter { f.from = FilterHeaderTypeFromV2(filter.GetHeaderType()) f.matcher = MatchFromV2(filter.GetMatchType()) - f.key.str = filter.GetKey() + f.key.fromString(filter.GetKey()) f.value = staticStringer(filter.GetValue()) return f diff --git a/eacl/filter_test.go b/eacl/filter_test.go index 9a471d0..7af8d68 100644 --- a/eacl/filter_test.go +++ b/eacl/filter_test.go @@ -1,6 +1,7 @@ package eacl import ( + "strconv" "testing" "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.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]) + } + }) }