[#189] sdk/netmap: Refactor Selector type

Replace alias to v2 type Selector with v2-compatible implementation.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-05 13:19:21 +03:00 committed by Alex Vanin
parent fd0c10b832
commit eb96fffa5c
3 changed files with 138 additions and 1 deletions

View file

@ -6,7 +6,6 @@ import (
// fixme: make types instead of aliases to v2 structures // fixme: make types instead of aliases to v2 structures
type PlacementPolicy = netmap.PlacementPolicy type PlacementPolicy = netmap.PlacementPolicy
type Selector = netmap.Selector
type Replica = netmap.Replica type Replica = netmap.Replica
func PlacementPolicyToJSON(p *PlacementPolicy) ([]byte, error) { func PlacementPolicyToJSON(p *PlacementPolicy) ([]byte, error) {

View file

@ -8,6 +8,9 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/netmap" "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. // processSelectors processes selectors and returns error is any of them is invalid.
func (c *Context) processSelectors(p *netmap.PlacementPolicy) error { func (c *Context) processSelectors(p *netmap.PlacementPolicy) error {
for _, s := range p.GetSelectors() { for _, s := range p.GetSelectors() {
@ -118,3 +121,80 @@ func (c *Context) getSelectionBase(s *netmap.Selector) []nodeAttrPair {
} }
return result 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)
}

View file

@ -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())
}