From 09e6093e64fd5d83a2cb406bc85a9dacb0ff3287 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 30 Sep 2020 18:54:07 +0300 Subject: [PATCH] [#164] sdk/object: Define filters to search root objects Signed-off-by: Leonard Lyubich --- pkg/object/search.go | 12 ++++++++++++ pkg/object/search_test.go | 28 ++++++++++++++++++++++++++++ pkg/object/wellknown_filters.go | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 pkg/object/wellknown_filters.go diff --git a/pkg/object/search.go b/pkg/object/search.go index 3cf97bea..8fea6af5 100644 --- a/pkg/object/search.go +++ b/pkg/object/search.go @@ -98,3 +98,15 @@ func (f SearchFilters) ToV2() []*v2object.SearchFilter { return result } + +func (f *SearchFilters) addRootFilter(val string) { + f.AddFilter(KeyRoot, val, MatchStringEqual) +} + +func (f *SearchFilters) AddRootFilter() { + f.addRootFilter(ValRoot) +} + +func (f *SearchFilters) AddNonRootFilter() { + f.addRootFilter(ValNonRoot) +} diff --git a/pkg/object/search_test.go b/pkg/object/search_test.go index 99fb139a..1058c5e5 100644 --- a/pkg/object/search_test.go +++ b/pkg/object/search_test.go @@ -51,3 +51,31 @@ func TestFilter(t *testing.T) { newFilters := object.NewSearchFiltersFromV2(v2) require.Equal(t, filters, newFilters) } + +func TestSearchFilters_AddRootFilter(t *testing.T) { + fs := new(object.SearchFilters) + + fs.AddRootFilter() + + require.Len(t, *fs, 1) + + f := (*fs)[0] + + require.Equal(t, object.MatchStringEqual, f.Operation()) + require.Equal(t, object.KeyRoot, f.Header()) + require.Equal(t, object.ValRoot, f.Value()) +} + +func TestSearchFilters_AddNonRootFilter(t *testing.T) { + fs := new(object.SearchFilters) + + fs.AddNonRootFilter() + + require.Len(t, *fs, 1) + + f := (*fs)[0] + + require.Equal(t, object.MatchStringEqual, f.Operation()) + require.Equal(t, object.KeyRoot, f.Header()) + require.Equal(t, object.ValNonRoot, f.Value()) +} diff --git a/pkg/object/wellknown_filters.go b/pkg/object/wellknown_filters.go new file mode 100644 index 00000000..16ccd235 --- /dev/null +++ b/pkg/object/wellknown_filters.go @@ -0,0 +1,20 @@ +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 +)