Skip to content

Commit 87a9670

Browse files
committed
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.
1 parent b05d801 commit 87a9670

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

l402/identifier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
type TokenID [TokenIDSize]byte
3838

3939
// String returns the hex encoded representation of the token ID as a string.
40-
func (t *TokenID) String() string {
40+
func (t TokenID) String() string {
4141
return hex.EncodeToString(t[:])
4242
}
4343

l402/identifier_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package l402
33
import (
44
"bytes"
55
"errors"
6+
"fmt"
67
"testing"
78

89
"github.com/lightningnetwork/lnd/lntypes"
10+
"github.com/stretchr/testify/require"
911
)
1012

1113
var (
@@ -67,3 +69,36 @@ func TestIdentifierSerialization(t *testing.T) {
6769
}
6870
}
6971
}
72+
73+
// TestTokenIDString makes sure that TokenID is logged properly in Printf
74+
// function family.
75+
func TestTokenIDString(t *testing.T) {
76+
cases := []struct {
77+
token TokenID
78+
formatString string
79+
wantText string
80+
}{
81+
{
82+
token: TokenID{1, 2, 3},
83+
formatString: "client %v paid",
84+
wantText: "client 01020300000000000000000000000000000" +
85+
"00000000000000000000000000000 paid",
86+
},
87+
{
88+
token: TokenID{1, 2, 3},
89+
formatString: "client %s paid",
90+
wantText: "client 01020300000000000000000000000000000" +
91+
"00000000000000000000000000000 paid",
92+
},
93+
}
94+
95+
for _, tc := range cases {
96+
t.Run(tc.formatString, func(t *testing.T) {
97+
got := fmt.Sprintf(tc.formatString, tc.token)
98+
require.Equal(t, tc.wantText, got)
99+
100+
got = fmt.Sprintf(tc.formatString, &tc.token)
101+
require.Equal(t, tc.wantText, got)
102+
})
103+
}
104+
}

0 commit comments

Comments
 (0)