From 35b6629e1c0aaeb1e683668044015c7790f39efb Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 21:02:30 +0300 Subject: [PATCH] [#302] pkg/checksum: Convert nil `Checksum` to nil message Document that `Checksum.ToV2` method return `nil` when called on `nil`. Document that `NewChecksumFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/checksum.go | 5 +++++ pkg/checksum_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/checksum.go b/pkg/checksum.go index 59ece9d1..d8a91b7b 100644 --- a/pkg/checksum.go +++ b/pkg/checksum.go @@ -28,6 +28,8 @@ const ( ) // NewChecksumFromV2 wraps v2 Checksum message to Checksum. +// +// Nil refs.Checksum converts to nil. func NewChecksumFromV2(cV2 *refs.Checksum) *Checksum { return (*Checksum)(cV2) } @@ -72,6 +74,9 @@ func (c *Checksum) SetTillichZemor(v [64]byte) { checksum.SetSum(v[:]) } +// ToV2 converts Checksum to v2 Checksum message. +// +// Nil Checksum converts to nil. func (c *Checksum) ToV2() *refs.Checksum { return (*refs.Checksum)(c) } diff --git a/pkg/checksum_test.go b/pkg/checksum_test.go index e636f403..04343a92 100644 --- a/pkg/checksum_test.go +++ b/pkg/checksum_test.go @@ -99,3 +99,19 @@ func TestChecksumEncoding(t *testing.T) { require.Equal(t, cs, cs2) }) } + +func TestNewChecksumFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *refs.Checksum + + require.Nil(t, NewChecksumFromV2(x)) + }) +} + +func TestChecksum_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *Checksum + + require.Nil(t, x.ToV2()) + }) +}