From c08b90dbbc9b0b6d0b01575e2e0a64a1957e6bcc Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 15:38:53 +0300 Subject: [PATCH] [#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 --- pkg/container/container.go | 7 +++++++ pkg/container/container_test.go | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/container/container.go b/pkg/container/container.go index 264bab02..24ed218d 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -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 } diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index 1456b0ec..1a1bce39 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -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()) + }) +}