l402: make TokenID.String() a value method

If a TokenID is passed by value to logging functions (such as the Printf family)
the pointer-based String() method is not used. As a result, the token is logged
as an array when using %v and as binary when using %s, which is inconvenient.
This commit is contained in:
Boris Nagaev
2025-05-10 23:17:11 -03:00
parent b05d801d8c
commit 87a9670bd3
2 changed files with 36 additions and 1 deletions

View File

@@ -37,7 +37,7 @@ var (
type TokenID [TokenIDSize]byte
// String returns the hex encoded representation of the token ID as a string.
func (t *TokenID) String() string {
func (t TokenID) String() string {
return hex.EncodeToString(t[:])
}

View File

@@ -3,9 +3,11 @@ package l402
import (
"bytes"
"errors"
"fmt"
"testing"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/stretchr/testify/require"
)
var (
@@ -67,3 +69,36 @@ func TestIdentifierSerialization(t *testing.T) {
}
}
}
// TestTokenIDString makes sure that TokenID is logged properly in Printf
// function family.
func TestTokenIDString(t *testing.T) {
cases := []struct {
token TokenID
formatString string
wantText string
}{
{
token: TokenID{1, 2, 3},
formatString: "client %v paid",
wantText: "client 01020300000000000000000000000000000" +
"00000000000000000000000000000 paid",
},
{
token: TokenID{1, 2, 3},
formatString: "client %s paid",
wantText: "client 01020300000000000000000000000000000" +
"00000000000000000000000000000 paid",
},
}
for _, tc := range cases {
t.Run(tc.formatString, func(t *testing.T) {
got := fmt.Sprintf(tc.formatString, tc.token)
require.Equal(t, tc.wantText, got)
got = fmt.Sprintf(tc.formatString, &tc.token)
require.Equal(t, tc.wantText, got)
})
}
}