Skip to content

Commit

Permalink
feat!: hex encode id.String() output (#202)
Browse files Browse the repository at this point in the history
This PR contains a breaking change that implements
#201 (comment).
Relevant #200
  • Loading branch information
rootulp committed Jun 6, 2023
1 parent 5466a61 commit 6854976
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
8 changes: 2 additions & 6 deletions namespace/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ func (nid ID) Size() IDSize {
return IDSize(len(nid))
}

// String stringifies the nid.
// String returns the hexadecimal encoding of the nid. The output of
// nid.String() is not equivalent to string(nid).
func (nid ID) String() string {
return string(nid)
}

// HexString returns hexadecimal encoding of nid.
func (nid ID) HexString() string {
return hex.EncodeToString(nid)
}
39 changes: 35 additions & 4 deletions namespace/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,41 @@ package namespace
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)

func TestID_HexString(t *testing.T) {
nID := ID("12345678")
require.Equal(t, "3132333435363738", nID.HexString())
// TestString verifies that id.String() returns the hexadecimal encoding of id.
func TestString(t *testing.T) {
type testCase struct {
id ID
want string
}
testCases := []testCase{
{ID(""), ""},
{ID("12345678"), "3132333435363738"},
{[]byte{0, 0, 0, 0, 0, 0, 0, 0}, "0000000000000000"},
{[]byte{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, "aaaaaaaaaaaaaaaa"},
{[]byte{1, 2, 3, 4, 5, 6, 7, 8}, "0102030405060708"},
}
for _, tc := range testCases {
assert.Equal(t, tc.want, tc.id.String())
}
}

// Test_string verifies that string(id) returns the native string representation of id.
func Test_string(t *testing.T) {
type testCase struct {
id ID
want string
}
testCases := []testCase{
{ID(""), ""},
{ID("12345678"), "12345678"},
{[]byte{0, 0, 0, 0, 0, 0, 0, 0}, "\x00\x00\x00\x00\x00\x00\x00\x00"},
{[]byte{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"},
{[]byte{1, 2, 3, 4, 5, 6, 7, 8}, "\x01\x02\x03\x04\x05\x06\a\b"},
}
for _, tc := range testCases {
assert.Equal(t, tc.want, string(tc.id))
}
}

0 comments on commit 6854976

Please sign in to comment.