[#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 <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 20:52:46 +03:00 committed by Alex Vanin
parent 3bc25f54d8
commit 00a0ea42a8
2 changed files with 17 additions and 1 deletions

View file

@ -23,7 +23,14 @@ type BearerToken struct {
token acl.BearerToken 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 return &b.token
} }
@ -100,6 +107,7 @@ func NewBearerToken() *BearerToken {
return b return b
} }
// ToV2 converts BearerToken to v2 BearerToken message.
func NewBearerTokenFromV2(v2 *acl.BearerToken) *BearerToken { func NewBearerTokenFromV2(v2 *acl.BearerToken) *BearerToken {
if v2 == nil { if v2 == nil {
v2 = new(acl.BearerToken) v2 = new(acl.BearerToken)

View file

@ -55,3 +55,11 @@ func TestFilterEncoding(t *testing.T) {
require.Equal(t, f, d2) 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())
})
}