From 00a0ea42a81fdf4ad5ff515f090d504b2cb97c8f Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 20:52:46 +0300 Subject: [PATCH] [#302] pkg/token: Convert nil `BearerToken` to nil message Document that `BearerToken.ToV2` method return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/token/bearer.go | 10 +++++++++- pkg/token/bearer_test.go | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/token/bearer.go b/pkg/token/bearer.go index 812faf26..8ac333e4 100644 --- a/pkg/token/bearer.go +++ b/pkg/token/bearer.go @@ -23,7 +23,14 @@ type BearerToken struct { token acl.BearerToken } -func (b BearerToken) ToV2() *acl.BearerToken { +// ToV2 converts BearerToken to v2 BearerToken message. +// +// Nil BearerToken converts to nil. +func (b *BearerToken) ToV2() *acl.BearerToken { + if b == nil { + return nil + } + return &b.token } @@ -100,6 +107,7 @@ func NewBearerToken() *BearerToken { return b } +// ToV2 converts BearerToken to v2 BearerToken message. func NewBearerTokenFromV2(v2 *acl.BearerToken) *BearerToken { if v2 == nil { v2 = new(acl.BearerToken) diff --git a/pkg/token/bearer_test.go b/pkg/token/bearer_test.go index 38bf47b1..26dca9e5 100644 --- a/pkg/token/bearer_test.go +++ b/pkg/token/bearer_test.go @@ -55,3 +55,11 @@ func TestFilterEncoding(t *testing.T) { require.Equal(t, f, d2) }) } + +func TestBearerToken_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *token.BearerToken + + require.Nil(t, x.ToV2()) + }) +}