[#302] pkg/container: Convert nil Container to nil message

Make `Container.ToV2` method to return `nil`
when called on `nil`. Write corresponding
unit test.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 15:38:53 +03:00 committed by Alex Vanin
parent 3984353d37
commit c08b90dbbc
2 changed files with 15 additions and 0 deletions

View file

@ -44,7 +44,14 @@ func New(opts ...NewOption) *Container {
return cnr
}
// ToV2 returns the v2 Container message.
//
// Nil Container converts to nil.
func (c *Container) ToV2() *container.Container {
if c == nil {
return nil
}
return &c.v2
}

View file

@ -91,3 +91,11 @@ func TestContainer_Signature(t *testing.T) {
require.Equal(t, sig, cnr.Signature())
}
func TestContainer_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *container.Container
require.Nil(t, x.ToV2())
})
}