diff --git a/pkg/netmap/filter.go b/pkg/netmap/filter.go index e4df979..50492ec 100644 --- a/pkg/netmap/filter.go +++ b/pkg/netmap/filter.go @@ -7,6 +7,9 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/netmap" ) +// Filter represents v2-compatible netmap filter. +type Filter netmap.Filter + // MainFilterName is a name of the filter // which points to the whole netmap. const MainFilterName = "*" @@ -127,3 +130,94 @@ func (c *Context) matchKeyValue(f *netmap.Filter, b *Node) bool { // will not happen if context was created from f (maybe panic?) return false } + +// NewFilter creates and returns new Filter instance. +func NewFilter() *Filter { + return NewFilterFromV2(new(netmap.Filter)) +} + +// NewFilterFromV2 converts v2 Filter to Filter. +func NewFilterFromV2(f *netmap.Filter) *Filter { + return (*Filter)(f) +} + +// ToV2 converts Filter to v2 Filter. +func (f *Filter) ToV2() *netmap.Filter { + return (*netmap.Filter)(f) +} + +// Key returns key to filter. +func (f *Filter) Key() string { + return (*netmap.Filter)(f). + GetKey() +} + +// SetKey sets key to filter. +func (f *Filter) SetKey(key string) { + (*netmap.Filter)(f). + SetKey(key) +} + +// Value returns value to match. +func (f *Filter) Value() string { + return (*netmap.Filter)(f). + GetValue() +} + +// SetValue sets value to match. +func (f *Filter) SetValue(val string) { + (*netmap.Filter)(f). + SetValue(val) +} + +// Name returns filter name. +func (f *Filter) Name() string { + return (*netmap.Filter)(f). + GetName() +} + +// SetName sets filter name. +func (f *Filter) SetName(name string) { + (*netmap.Filter)(f). + SetName(name) +} + +// Operation returns filtering operation. +func (f *Filter) Operation() Operation { + return OperationFromV2( + (*netmap.Filter)(f). + GetOp(), + ) +} + +// SetOperation sets filtering operation. +func (f *Filter) SetOperation(op Operation) { + (*netmap.Filter)(f). + SetOp(op.ToV2()) +} + +// InnerFilters returns list of inner filters. +func (f *Filter) InnerFilters() []*Filter { + fs := (*netmap.Filter)(f). + GetFilters() + + res := make([]*Filter, 0, len(fs)) + + for i := range fs { + res = append(res, NewFilterFromV2(fs[i])) + } + + return res +} + +// SetInnerFilters sets list of inner filters. +func (f *Filter) SetInnerFilters(fs ...*Filter) { + fsV2 := make([]*netmap.Filter, 0, len(fs)) + + for i := range fs { + fsV2 = append(fsV2, fs[i].ToV2()) + } + + (*netmap.Filter)(f). + SetFilters(fsV2) +} diff --git a/pkg/netmap/filter_test.go b/pkg/netmap/filter_test.go index 82a5445..46c3567 100644 --- a/pkg/netmap/filter_test.go +++ b/pkg/netmap/filter_test.go @@ -201,3 +201,71 @@ func TestFilter_Match(t *testing.T) { require.False(t, c.applyFilter("Main", n)) }) } + +func testFilter() *Filter { + f := NewFilter() + f.SetOperation(OpGE) + f.SetName("name") + f.SetKey("key") + f.SetValue("value") + + return f +} + +func TestFilterFromV2(t *testing.T) { + fV2 := new(netmap.Filter) + fV2.SetOp(netmap.GE) + fV2.SetName("name") + fV2.SetKey("key") + fV2.SetValue("value") + + f := NewFilterFromV2(fV2) + + require.Equal(t, fV2, f.ToV2()) +} + +func TestFilter_Key(t *testing.T) { + f := NewFilter() + key := "some key" + + f.SetKey(key) + + require.Equal(t, key, f.Key()) +} + +func TestFilter_Value(t *testing.T) { + f := NewFilter() + val := "some value" + + f.SetValue(val) + + require.Equal(t, val, f.Value()) +} + +func TestFilter_Name(t *testing.T) { + f := NewFilter() + name := "some name" + + f.SetName(name) + + require.Equal(t, name, f.Name()) +} + +func TestFilter_Operation(t *testing.T) { + f := NewFilter() + op := OpGE + + f.SetOperation(op) + + require.Equal(t, op, f.Operation()) +} + +func TestFilter_InnerFilters(t *testing.T) { + f := NewFilter() + + f1, f2 := testFilter(), testFilter() + + f.SetInnerFilters(f1, f2) + + require.Equal(t, []*Filter{f1, f2}, f.InnerFilters()) +} diff --git a/pkg/netmap/policy.go b/pkg/netmap/policy.go index 736cdff..31ced83 100644 --- a/pkg/netmap/policy.go +++ b/pkg/netmap/policy.go @@ -7,7 +7,6 @@ import ( // fixme: make types instead of aliases to v2 structures type PlacementPolicy = netmap.PlacementPolicy type Selector = netmap.Selector -type Filter = netmap.Filter type Replica = netmap.Replica func PlacementPolicyToJSON(p *PlacementPolicy) ([]byte, error) {