From eb96fffa5cabf73c12934d0069ac476b3dda9660 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 5 Nov 2020 13:19:21 +0300 Subject: [PATCH] [#189] sdk/netmap: Refactor Selector type Replace alias to v2 type Selector with v2-compatible implementation. Signed-off-by: Leonard Lyubich --- pkg/netmap/policy.go | 1 - pkg/netmap/selector.go | 80 +++++++++++++++++++++++++++++++++++++ pkg/netmap/selector_test.go | 58 +++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/pkg/netmap/policy.go b/pkg/netmap/policy.go index 31ced83f..b0e46d6e 100644 --- a/pkg/netmap/policy.go +++ b/pkg/netmap/policy.go @@ -6,7 +6,6 @@ import ( // fixme: make types instead of aliases to v2 structures type PlacementPolicy = netmap.PlacementPolicy -type Selector = netmap.Selector type Replica = netmap.Replica func PlacementPolicyToJSON(p *PlacementPolicy) ([]byte, error) { diff --git a/pkg/netmap/selector.go b/pkg/netmap/selector.go index 30985ac9..99fb9034 100644 --- a/pkg/netmap/selector.go +++ b/pkg/netmap/selector.go @@ -8,6 +8,9 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/netmap" ) +// Selector represents v2-compatible netmap selector. +type Selector netmap.Selector + // processSelectors processes selectors and returns error is any of them is invalid. func (c *Context) processSelectors(p *netmap.PlacementPolicy) error { for _, s := range p.GetSelectors() { @@ -118,3 +121,80 @@ func (c *Context) getSelectionBase(s *netmap.Selector) []nodeAttrPair { } return result } + +// NewSelector creates and returns new Selector instance. +func NewSelector() *Selector { + return NewSelectorFromV2(new(netmap.Selector)) +} + +// NewSelectorFromV2 converts v2 Selector to Selector. +func NewSelectorFromV2(f *netmap.Selector) *Selector { + return (*Selector)(f) +} + +// ToV2 converts Selector to v2 Selector. +func (s *Selector) ToV2() *netmap.Selector { + return (*netmap.Selector)(s) +} + +// Name returns selector name. +func (s *Selector) Name() string { + return (*netmap.Selector)(s). + GetName() +} + +// SetName sets selector name. +func (s *Selector) SetName(name string) { + (*netmap.Selector)(s). + SetName(name) +} + +// Count returns count of nodes to select from bucket. +func (s *Selector) Count() uint32 { + return (*netmap.Selector)(s). + GetCount() +} + +// SetCount sets count of nodes to select from bucket. +func (s *Selector) SetCount(c uint32) { + (*netmap.Selector)(s). + SetCount(c) +} + +// Clause returns modifier showing how to form a bucket. +func (s *Selector) Clause() Clause { + return ClauseFromV2( + (*netmap.Selector)(s). + GetClause(), + ) +} + +// SetClause sets modifier showing how to form a bucket. +func (s *Selector) SetClause(c Clause) { + (*netmap.Selector)(s). + SetClause(c.ToV2()) +} + +// Attribute returns attribute bucket to select from. +func (s *Selector) Attribute() string { + return (*netmap.Selector)(s). + GetAttribute() +} + +// SetAttribute sets attribute bucket to select from. +func (s *Selector) SetAttribute(a string) { + (*netmap.Selector)(s). + SetAttribute(a) +} + +// Filter returns filter reference to select from. +func (s *Selector) Filter() string { + return (*netmap.Selector)(s). + GetFilter() +} + +// SetFilter sets filter reference to select from. +func (s *Selector) SetFilter(f string) { + (*netmap.Selector)(s). + SetFilter(f) +} diff --git a/pkg/netmap/selector_test.go b/pkg/netmap/selector_test.go index cf581d0f..ee31c75f 100644 --- a/pkg/netmap/selector_test.go +++ b/pkg/netmap/selector_test.go @@ -238,3 +238,61 @@ func TestPlacementPolicy_ProcessSelectorsInvalid(t *testing.T) { }) } } + +func TestSelectorFromV2(t *testing.T) { + sV2 := new(netmap.Selector) + sV2.SetName("name") + sV2.SetCount(3) + sV2.SetClause(netmap.Distinct) + sV2.SetAttribute("attribute") + sV2.SetFilter("filter") + + s := NewSelectorFromV2(sV2) + + require.Equal(t, sV2, s.ToV2()) +} + +func TestSelector_Name(t *testing.T) { + s := NewSelector() + name := "some name" + + s.SetName(name) + + require.Equal(t, name, s.Name()) +} + +func TestSelector_Count(t *testing.T) { + s := NewSelector() + c := uint32(3) + + s.SetCount(c) + + require.Equal(t, c, s.Count()) +} + +func TestSelector_Clause(t *testing.T) { + s := NewSelector() + c := ClauseSame + + s.SetClause(c) + + require.Equal(t, c, s.Clause()) +} + +func TestSelector_Attribute(t *testing.T) { + s := NewSelector() + a := "some attribute" + + s.SetAttribute(a) + + require.Equal(t, a, s.Attribute()) +} + +func TestSelector_Filter(t *testing.T) { + s := NewSelector() + f := "some filter" + + s.SetFilter(f) + + require.Equal(t, f, s.Filter()) +}