[#182] sdk/object: Refactor a way to add filters by properties

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-10-27 21:11:58 +03:00 committed by Alex Vanin
parent 6b54019148
commit ac38d13f04
4 changed files with 47 additions and 79 deletions

View file

@ -54,6 +54,8 @@ type filterKey struct {
// enumeration of reserved filter keys. // enumeration of reserved filter keys.
type filterKeyType int type filterKeyType int
type boolStringer bool
type SearchFilters []SearchFilter type SearchFilters []SearchFilter
const ( const (
@ -67,6 +69,9 @@ const (
fKeyType fKeyType
fKeyHomomorphicHash fKeyHomomorphicHash
fKeyParent fKeyParent
fKeyPropRoot
fKeyPropLeaf
fKeyPropChildfree
) )
func (k filterKey) String() string { func (k filterKey) String() string {
@ -91,6 +96,12 @@ func (k filterKey) String() string {
return v2object.FilterHeaderHomomorphicHash return v2object.FilterHeaderHomomorphicHash
case fKeyParent: case fKeyParent:
return v2object.FilterHeaderParent return v2object.FilterHeaderParent
case fKeyPropRoot:
return v2object.FilterPropertyRoot
case fKeyPropLeaf:
return v2object.FilterPropertyLeaf
case fKeyPropChildfree:
return v2object.FilterPropertyChildfree
} }
} }
@ -98,6 +109,14 @@ func (s staticStringer) String() string {
return string(s) return string(s)
} }
func (s boolStringer) String() string {
if s {
return v2object.BooleanPropertyValueTrue
}
return v2object.BooleanPropertyValueFalse
}
func (f *SearchFilter) Header() string { func (f *SearchFilter) Header() string {
return f.header.String() return f.header.String()
} }
@ -180,26 +199,38 @@ func (f SearchFilters) ToV2() []*v2object.SearchFilter {
return result return result
} }
func (f *SearchFilters) addRootFilter(val string) { func (f *SearchFilters) addRootFilter(val bool) {
f.AddFilter(KeyRoot, val, MatchStringEqual) f.addReservedFilter(MatchStringEqual, fKeyPropRoot, boolStringer(val))
} }
func (f *SearchFilters) AddRootFilter() { func (f *SearchFilters) AddRootFilter() {
f.addRootFilter(ValRoot) f.addRootFilter(true)
} }
func (f *SearchFilters) AddNonRootFilter() { func (f *SearchFilters) AddNonRootFilter() {
f.addRootFilter(ValNonRoot) f.addRootFilter(false)
} }
func (f *SearchFilters) addLeafFilter(val string) { func (f *SearchFilters) addLeafFilter(val bool) {
f.AddFilter(KeyLeaf, val, MatchStringEqual) f.addReservedFilter(MatchStringEqual, fKeyPropLeaf, boolStringer(val))
} }
func (f *SearchFilters) AddLeafFilter() { func (f *SearchFilters) AddLeafFilter() {
f.addLeafFilter(ValLeaf) f.addLeafFilter(true)
} }
func (f *SearchFilters) AddNonLeafFilter() { func (f *SearchFilters) AddNonLeafFilter() {
f.addLeafFilter(ValNonLeaf) f.addLeafFilter(false)
}
func (f *SearchFilters) addChildFreeFilter(val bool) {
f.addReservedFilter(MatchStringEqual, fKeyPropChildfree, boolStringer(val))
}
func (f *SearchFilters) AddChildfreeFilter() {
f.addChildFreeFilter(true)
}
func (f *SearchFilters) AddNonChildfreeFilter() {
f.addChildFreeFilter(false)
} }

View file

@ -32,7 +32,6 @@ func TestMatch(t *testing.T) {
func TestFilter(t *testing.T) { func TestFilter(t *testing.T) {
inputs := [][]string{ inputs := [][]string{
{"user-header", "user-value"}, {"user-header", "user-value"},
{object.HdrSysNameID, "objectID"},
} }
filters := object.NewSearchFilters() filters := object.NewSearchFilters()
@ -62,8 +61,8 @@ func TestSearchFilters_AddRootFilter(t *testing.T) {
f := (*fs)[0] f := (*fs)[0]
require.Equal(t, object.MatchStringEqual, f.Operation()) require.Equal(t, object.MatchStringEqual, f.Operation())
require.Equal(t, object.KeyRoot, f.Header()) require.Equal(t, v2object.FilterPropertyRoot, f.Header())
require.Equal(t, object.ValRoot, f.Value()) require.Equal(t, v2object.BooleanPropertyValueTrue, f.Value())
} }
func TestSearchFilters_AddNonRootFilter(t *testing.T) { func TestSearchFilters_AddNonRootFilter(t *testing.T) {
@ -76,8 +75,8 @@ func TestSearchFilters_AddNonRootFilter(t *testing.T) {
f := (*fs)[0] f := (*fs)[0]
require.Equal(t, object.MatchStringEqual, f.Operation()) require.Equal(t, object.MatchStringEqual, f.Operation())
require.Equal(t, object.KeyRoot, f.Header()) require.Equal(t, v2object.FilterPropertyRoot, f.Header())
require.Equal(t, object.ValNonRoot, f.Value()) require.Equal(t, v2object.BooleanPropertyValueFalse, f.Value())
} }
func TestSearchFilters_AddLeafFilter(t *testing.T) { func TestSearchFilters_AddLeafFilter(t *testing.T) {
@ -90,8 +89,8 @@ func TestSearchFilters_AddLeafFilter(t *testing.T) {
f := (*fs)[0] f := (*fs)[0]
require.Equal(t, object.MatchStringEqual, f.Operation()) require.Equal(t, object.MatchStringEqual, f.Operation())
require.Equal(t, object.KeyLeaf, f.Header()) require.Equal(t, v2object.FilterPropertyLeaf, f.Header())
require.Equal(t, object.ValLeaf, f.Value()) require.Equal(t, v2object.BooleanPropertyValueTrue, f.Value())
} }
func TestSearchFilters_AddNonLeafFilter(t *testing.T) { func TestSearchFilters_AddNonLeafFilter(t *testing.T) {
@ -104,6 +103,6 @@ func TestSearchFilters_AddNonLeafFilter(t *testing.T) {
f := (*fs)[0] f := (*fs)[0]
require.Equal(t, object.MatchStringEqual, f.Operation()) require.Equal(t, object.MatchStringEqual, f.Operation())
require.Equal(t, object.KeyLeaf, f.Header()) require.Equal(t, v2object.FilterPropertyLeaf, f.Header())
require.Equal(t, object.ValNonLeaf, f.Value()) require.Equal(t, v2object.BooleanPropertyValueFalse, f.Value())
} }

View file

@ -1,31 +0,0 @@
package object
const (
valFalse = "false"
valTrue = "true"
)
// ReservedFilterPrefix is a reserved prefix for system search filter keys.
const ReservedFilterPrefix = "$Object:"
const (
// KeyRoot is a reserved search filter key to source objects.
KeyRoot = ReservedFilterPrefix + "ROOT"
// ValRoot is a value of root object filter.
ValRoot = valTrue
// ValNonRoot is a value of non-root object filter.
ValNonRoot = valFalse
)
const (
// KeyLeaf is a reserved search filter key to physically stored objects.
KeyLeaf = ReservedFilterPrefix + "LEAF"
// ValLeaf is a value of physically stored object filter.
ValLeaf = valTrue
// ValNonLeaf is a value of virtual object filter.
ValNonLeaf = valFalse
)

View file

@ -1,31 +0,0 @@
package object
// This file contains well-known header names for eACL filters and search
// request filters. Both of them encode header type as string. There are
// constant strings for well-known object headers.
const (
// ReservedHeaderNamePrefix used in filter names to specify well known
// headers such as container id, object id, owner id, etc.
// All names without this prefix used to lookup through user defined headers
// in object or x-headers in request.
ReservedHeaderNamePrefix = "_"
// HdrSysNameID is a name of ID field in system header of object.
HdrSysNameID = ReservedHeaderNamePrefix + "ID"
// HdrSysNameCID is a name of cid field in system header of object.
HdrSysNameCID = ReservedHeaderNamePrefix + "CID"
// HdrSysNameOwnerID is a name of OwnerID field in system header of object.
HdrSysNameOwnerID = ReservedHeaderNamePrefix + "OWNER_ID"
// HdrSysNameVersion is a name of version field in system header of object.
HdrSysNameVersion = ReservedHeaderNamePrefix + "VERSION"
// HdrSysNamePayloadLength is a name of PayloadLength field in system header of object.
HdrSysNamePayloadLength = ReservedHeaderNamePrefix + "PAYLOAD_LENGTH"
// HdrSysNameCreatedEpoch is a name of CreatedAt.Epoch field in system header of object.
HdrSysNameCreatedEpoch = ReservedHeaderNamePrefix + "CREATED_EPOCH"
)