[#258] Fix string encoding of identifiers

Use `EncodeToString` method to get protocol string according to type
docs.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-05-31 09:55:08 +03:00 committed by LeL
parent 6cb513c976
commit 82d762f536
9 changed files with 40 additions and 35 deletions

View file

@ -64,7 +64,7 @@ func TestID_String(t *testing.T) {
id := cidtest.ID()
var id2 cid.ID
require.NoError(t, id2.DecodeString(id.String()))
require.NoError(t, id2.DecodeString(id.EncodeToString()))
require.Equal(t, id, id2)
})

View file

@ -1,7 +1,6 @@
package eacl
import (
"fmt"
"strconv"
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
@ -15,7 +14,7 @@ type Filter struct {
from FilterHeaderType
matcher Match
key filterKey
value fmt.Stringer
value stringEncoder
}
type staticStringer string
@ -44,17 +43,17 @@ const (
fKeyObjHomomorphicHash
)
func (s staticStringer) String() string {
func (s staticStringer) EncodeToString() string {
return string(s)
}
func (u u64Stringer) String() string {
func (u u64Stringer) EncodeToString() string {
return strconv.FormatUint(uint64(u), 10)
}
// Value returns filtered string value.
func (f Filter) Value() string {
return f.value.String()
return f.value.EncodeToString()
}
// Matcher returns filter Match type.
@ -81,7 +80,7 @@ func (f *Filter) ToV2() *v2acl.HeaderFilter {
}
filter := new(v2acl.HeaderFilter)
filter.SetValue(f.value.String())
filter.SetValue(f.value.EncodeToString())
filter.SetKey(f.key.String())
filter.SetMatchType(f.matcher.ToV2())
filter.SetHeaderType(f.from.ToV2())

View file

@ -2,7 +2,6 @@ package eacl
import (
"crypto/ecdsa"
"fmt"
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
"github.com/nspcc-dev/neofs-sdk-go/checksum"
@ -74,7 +73,11 @@ func AddFormedTarget(r *Record, role Role, keys ...ecdsa.PublicKey) {
AddRecordTarget(r, t)
}
func (r *Record) addFilter(from FilterHeaderType, m Match, keyTyp filterKeyType, key string, val fmt.Stringer) {
type stringEncoder interface {
EncodeToString() string
}
func (r *Record) addFilter(from FilterHeaderType, m Match, keyTyp filterKeyType, key string, val stringEncoder) {
filter := Filter{
from: from,
key: filterKey{
@ -88,11 +91,11 @@ func (r *Record) addFilter(from FilterHeaderType, m Match, keyTyp filterKeyType,
r.filters = append(r.filters, filter)
}
func (r *Record) addObjectFilter(m Match, keyTyp filterKeyType, key string, val fmt.Stringer) {
func (r *Record) addObjectFilter(m Match, keyTyp filterKeyType, key string, val stringEncoder) {
r.addFilter(HeaderFromObject, m, keyTyp, key, val)
}
func (r *Record) addObjectReservedFilter(m Match, typ filterKeyType, val fmt.Stringer) {
func (r *Record) addObjectReservedFilter(m Match, typ filterKeyType, val stringEncoder) {
r.addObjectFilter(m, typ, "", val)
}
@ -108,7 +111,7 @@ func (r *Record) AddObjectAttributeFilter(m Match, key, value string) {
// AddObjectVersionFilter adds filter by object version.
func (r *Record) AddObjectVersionFilter(m Match, v *version.Version) {
r.addObjectReservedFilter(m, fKeyObjVersion, v)
r.addObjectReservedFilter(m, fKeyObjVersion, staticStringer(version.EncodeToString(*v)))
}
// AddObjectIDFilter adds filter by object ID.
@ -138,17 +141,17 @@ func (r *Record) AddObjectPayloadLengthFilter(m Match, size uint64) {
// AddObjectPayloadHashFilter adds filter by object payload hash value.
func (r *Record) AddObjectPayloadHashFilter(m Match, h checksum.Checksum) {
r.addObjectReservedFilter(m, fKeyObjPayloadHash, h)
r.addObjectReservedFilter(m, fKeyObjPayloadHash, staticStringer(h.String()))
}
// AddObjectTypeFilter adds filter by object type.
func (r *Record) AddObjectTypeFilter(m Match, t object.Type) {
r.addObjectReservedFilter(m, fKeyObjType, t)
r.addObjectReservedFilter(m, fKeyObjType, staticStringer(t.String()))
}
// AddObjectHomomorphicHashFilter adds filter by object payload homomorphic hash value.
func (r *Record) AddObjectHomomorphicHashFilter(m Match, h checksum.Checksum) {
r.addObjectReservedFilter(m, fKeyObjHomomorphicHash, h)
r.addObjectReservedFilter(m, fKeyObjHomomorphicHash, staticStringer(h.String()))
}
// ToV2 converts Record to v2 acl.EACLRecord message.

View file

@ -177,17 +177,17 @@ func TestReservedRecords(t *testing.T) {
{
f: func(r *Record) { r.AddObjectIDFilter(MatchStringEqual, oid) },
key: v2acl.FilterObjectID,
value: oid.String(),
value: oid.EncodeToString(),
},
{
f: func(r *Record) { r.AddObjectContainerIDFilter(MatchStringEqual, cid) },
key: v2acl.FilterObjectContainerID,
value: cid.String(),
value: cid.EncodeToString(),
},
{
f: func(r *Record) { r.AddObjectOwnerIDFilter(MatchStringEqual, ownerid) },
key: v2acl.FilterObjectOwnerID,
value: ownerid.String(),
value: ownerid.EncodeToString(),
},
{
f: func(r *Record) { r.AddObjectCreationEpoch(MatchStringEqual, 100) },

View file

@ -95,7 +95,7 @@ func RecordN(n int) *eacl.Record {
x.SetTargets(*TargetN(n))
for i := 0; i < n; i++ {
x.AddFilter(eacl.HeaderFromObject, eacl.MatchStringEqual, "", cidtest.ID().String())
x.AddFilter(eacl.HeaderFromObject, eacl.MatchStringEqual, "", cidtest.ID().EncodeToString())
}
return x

View file

@ -147,7 +147,7 @@ func TestNNS_ResolveContainerName(t *testing.T) {
t.Run("with container array element", func(t *testing.T) {
id := cidtest.ID()
arr[1] = stackitem.NewByteArray([]byte(id.String()))
arr[1] = stackitem.NewByteArray([]byte(id.EncodeToString()))
res, err := n.ResolveContainerName(testContainerName)
require.NoError(t, err)

View file

@ -105,7 +105,7 @@ func TestID_String(t *testing.T) {
var oid ID
require.NoError(t, oid.DecodeString(str))
require.Equal(t, str, oid.String())
require.Equal(t, str, oid.EncodeToString())
})
}
})

View file

@ -2,7 +2,6 @@ package object
import (
"encoding/json"
"fmt"
"strconv"
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
@ -83,9 +82,13 @@ func (m *SearchMatchType) FromString(s string) bool {
return ok
}
type stringEncoder interface {
EncodeToString() string
}
type SearchFilter struct {
header filterKey
value fmt.Stringer
value stringEncoder
op SearchMatchType
}
@ -152,7 +155,7 @@ func (k filterKey) String() string {
}
}
func (s staticStringer) String() string {
func (s staticStringer) EncodeToString() string {
return string(s)
}
@ -161,7 +164,7 @@ func (f *SearchFilter) Header() string {
}
func (f *SearchFilter) Value() string {
return f.value.String()
return f.value.EncodeToString()
}
func (f *SearchFilter) Operation() SearchMatchType {
@ -186,7 +189,7 @@ func NewSearchFiltersFromV2(v2 []v2object.SearchFilter) SearchFilters {
return filters
}
func (f *SearchFilters) addFilter(op SearchMatchType, keyTyp filterKeyType, key string, val fmt.Stringer) {
func (f *SearchFilters) addFilter(op SearchMatchType, keyTyp filterKeyType, key string, val stringEncoder) {
if *f == nil {
*f = make(SearchFilters, 0, 1)
}
@ -205,7 +208,7 @@ func (f *SearchFilters) AddFilter(header, value string, op SearchMatchType) {
f.addFilter(op, 0, header, staticStringer(value))
}
func (f *SearchFilters) addReservedFilter(op SearchMatchType, keyTyp filterKeyType, val fmt.Stringer) {
func (f *SearchFilters) addReservedFilter(op SearchMatchType, keyTyp filterKeyType, val stringEncoder) {
f.addFilter(op, keyTyp, "", val)
}
@ -216,15 +219,15 @@ func (f *SearchFilters) addFlagFilter(keyTyp filterKeyType) {
f.addFilter(MatchUnknown, keyTyp, "", staticStringer(""))
}
func (f *SearchFilters) AddObjectVersionFilter(op SearchMatchType, v *version.Version) {
f.addReservedFilter(op, fKeyVersion, v)
func (f *SearchFilters) AddObjectVersionFilter(op SearchMatchType, v version.Version) {
f.addReservedFilter(op, fKeyVersion, staticStringer(version.EncodeToString(v)))
}
func (f *SearchFilters) AddObjectContainerIDFilter(m SearchMatchType, id cid.ID) {
f.addReservedFilter(m, fKeyContainerID, id)
}
func (f *SearchFilters) AddObjectOwnerIDFilter(m SearchMatchType, id *user.ID) {
func (f *SearchFilters) AddObjectOwnerIDFilter(m SearchMatchType, id user.ID) {
f.addReservedFilter(m, fKeyOwnerID, id)
}
@ -237,7 +240,7 @@ func (f SearchFilters) ToV2() []v2object.SearchFilter {
for i := range f {
result[i].SetKey(f[i].header.String())
result[i].SetValue(f[i].value.String())
result[i].SetValue(f[i].value.EncodeToString())
result[i].SetMatchType(f[i].op.ToV2())
}
@ -271,12 +274,12 @@ func (f *SearchFilters) AddObjectIDFilter(m SearchMatchType, id oid.ID) {
}
func (f *SearchFilters) AddSplitIDFilter(m SearchMatchType, id *SplitID) {
f.addReservedFilter(m, fKeySplitID, id)
f.addReservedFilter(m, fKeySplitID, staticStringer(id.String()))
}
// AddTypeFilter adds filter by object type.
func (f *SearchFilters) AddTypeFilter(m SearchMatchType, typ Type) {
f.addReservedFilter(m, fKeyType, typ)
f.addReservedFilter(m, fKeyType, staticStringer(typ.String()))
}
// MarshalJSON encodes SearchFilters to protobuf JSON format.

View file

@ -122,7 +122,7 @@ func TestSearchFilters_AddParentIDFilter(t *testing.T) {
require.Len(t, fsV2, 1)
require.Equal(t, v2object.FilterHeaderParent, fsV2[0].GetKey())
require.Equal(t, par.String(), fsV2[0].GetValue())
require.Equal(t, par.EncodeToString(), fsV2[0].GetValue())
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
}
@ -138,7 +138,7 @@ func TestSearchFilters_AddObjectIDFilter(t *testing.T) {
require.Len(t, fsV2, 1)
require.Equal(t, v2object.FilterHeaderObjectID, fsV2[0].GetKey())
require.Equal(t, id.String(), fsV2[0].GetValue())
require.Equal(t, id.EncodeToString(), fsV2[0].GetValue())
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
})
}