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

Make `Filter.ToV2` method return `nil`
when called on `nil`. Make `NewFilterFromV2`
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 16:41:45 +03:00 committed by Alex Vanin
parent 0206bd9a5d
commit 9a7e120485
2 changed files with 16 additions and 0 deletions

View file

@ -153,11 +153,15 @@ func NewFilter() *Filter {
} }
// NewFilterFromV2 converts v2 Filter to Filter. // NewFilterFromV2 converts v2 Filter to Filter.
//
// Nil netmap.Filter converts to nil.
func NewFilterFromV2(f *netmap.Filter) *Filter { func NewFilterFromV2(f *netmap.Filter) *Filter {
return (*Filter)(f) return (*Filter)(f)
} }
// ToV2 converts Filter to v2 Filter. // ToV2 converts Filter to v2 Filter.
//
// Nil Filter converts to nil.
func (f *Filter) ToV2() *netmap.Filter { func (f *Filter) ToV2() *netmap.Filter {
return (*netmap.Filter)(f) return (*netmap.Filter)(f)
} }

View file

@ -213,6 +213,18 @@ func testFilter() *Filter {
} }
func TestFilterFromV2(t *testing.T) { func TestFilterFromV2(t *testing.T) {
t.Run("nil from V2", func(t *testing.T) {
var x *netmap.Filter
require.Nil(t, NewFilterFromV2(x))
})
t.Run("nil to V2", func(t *testing.T) {
var x *Filter
require.Nil(t, x.ToV2())
})
fV2 := new(netmap.Filter) fV2 := new(netmap.Filter)
fV2.SetOp(netmap.GE) fV2.SetOp(netmap.GE)
fV2.SetName("name") fV2.SetName("name")