Skip to content

Commit

Permalink
feat!: hex encode id.String() output
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Jun 1, 2023
1 parent 5466a61 commit 95dfa27
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
7 changes: 1 addition & 6 deletions namespace/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ func (nid ID) Size() IDSize {
return IDSize(len(nid))
}

// String stringifies the nid.
// String returns the hexadecimal encoding of the 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 95dfa27

Please sign in to comment.