[#302] pkg/netmap: Convert nil Selector to nil message

Document that `Selector.ToV2` method return `nil`
when called on `nil`. Document that `NewSelectorFromV2`
function return `nil` when called on `nil`. Write
corresponding unit tests.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 18:33:38 +03:00 committed by Alex Vanin
parent af1742b48a
commit c47d1fb26b
2 changed files with 29 additions and 13 deletions

View file

@ -161,11 +161,15 @@ func NewSelector() *Selector {
} }
// NewSelectorFromV2 converts v2 Selector to Selector. // NewSelectorFromV2 converts v2 Selector to Selector.
//
// Nil netmap.Selector converts to nil.
func NewSelectorFromV2(f *netmap.Selector) *Selector { func NewSelectorFromV2(f *netmap.Selector) *Selector {
return (*Selector)(f) return (*Selector)(f)
} }
// ToV2 converts Selector to v2 Selector. // ToV2 converts Selector to v2 Selector.
//
// Nil Selector converts to nil.
func (s *Selector) ToV2() *netmap.Selector { func (s *Selector) ToV2() *netmap.Selector {
return (*netmap.Selector)(s) return (*netmap.Selector)(s)
} }

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/v2/netmap" "github.com/nspcc-dev/neofs-api-go/v2/netmap"
testv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -414,19 +415,6 @@ func testSelector() *Selector {
return s return s
} }
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) { func TestSelector_Name(t *testing.T) {
s := NewSelector() s := NewSelector()
name := "some name" name := "some name"
@ -495,3 +483,27 @@ func TestSelectorEncoding(t *testing.T) {
require.Equal(t, s, s2) require.Equal(t, s, s2)
}) })
} }
func TestSelector_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *Selector
require.Nil(t, x.ToV2())
})
}
func TestNewSelectorFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *netmap.Selector
require.Nil(t, NewSelectorFromV2(x))
})
t.Run("from non-nil", func(t *testing.T) {
sV2 := testv2.GenerateSelector(false)
s := NewSelectorFromV2(sV2)
require.Equal(t, sV2, s.ToV2())
})
}