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

Document that `PlacementPolicy.ToV2` method return `nil`
when called on `nil`. Document that `PlacementPolicyFromV2`
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:13:31 +03:00 committed by Alex Vanin
parent df4d610fad
commit 2176cb9f2b
2 changed files with 20 additions and 0 deletions

View file

@ -13,11 +13,15 @@ func NewPlacementPolicy() *PlacementPolicy {
} }
// NewPlacementPolicyFromV2 converts v2 PlacementPolicy to PlacementPolicy. // NewPlacementPolicyFromV2 converts v2 PlacementPolicy to PlacementPolicy.
//
// Nil netmap.PlacementPolicy converts to nil.
func NewPlacementPolicyFromV2(f *netmap.PlacementPolicy) *PlacementPolicy { func NewPlacementPolicyFromV2(f *netmap.PlacementPolicy) *PlacementPolicy {
return (*PlacementPolicy)(f) return (*PlacementPolicy)(f)
} }
// ToV2 converts PlacementPolicy to v2 PlacementPolicy. // ToV2 converts PlacementPolicy to v2 PlacementPolicy.
//
// Nil PlacementPolicy converts to nil.
func (p *PlacementPolicy) ToV2() *netmap.PlacementPolicy { func (p *PlacementPolicy) ToV2() *netmap.PlacementPolicy {
return (*netmap.PlacementPolicy)(p) return (*netmap.PlacementPolicy)(p)
} }

View file

@ -144,3 +144,19 @@ func TestPlacementPolicyEncoding(t *testing.T) {
require.Equal(t, p, p2) require.Equal(t, p, p2)
}) })
} }
func TestNewPlacementPolicy(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *PlacementPolicy
require.Nil(t, x.ToV2())
})
}
func TestNewPlacementPolicyFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *netmap.PlacementPolicy
require.Nil(t, NewPlacementPolicyFromV2(x))
})
}