From 95dfa276f9a612548e3e9a87230e2532d0279a63 Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Fri, 26 May 2023 10:42:00 -0400 Subject: [PATCH 1/3] feat!: hex encode id.String() output --- namespace/id.go | 7 +------ namespace/id_test.go | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/namespace/id.go b/namespace/id.go index d5dd8e7..3e74ebc 100644 --- a/namespace/id.go +++ b/namespace/id.go @@ -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) } diff --git a/namespace/id_test.go b/namespace/id_test.go index 15919b6..249620a 100644 --- a/namespace/id_test.go +++ b/namespace/id_test.go @@ -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)) + } } From d71f15fbdb7189d8fd39d54bfc28d7f11296f1bb Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Tue, 6 Jun 2023 10:41:50 -0400 Subject: [PATCH 2/3] add note --- namespace/id.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/namespace/id.go b/namespace/id.go index 3e74ebc..27911d6 100644 --- a/namespace/id.go +++ b/namespace/id.go @@ -27,7 +27,8 @@ func (nid ID) Size() IDSize { return IDSize(len(nid)) } -// String returns the hexadecimal encoding of the nid. +// String returns the hexadecimal encoding of the nid. The output of +// nid.String() is not equivalant to string(nid). func (nid ID) String() string { return hex.EncodeToString(nid) } From 9d77e95c0dfc0c4db7a4d3e560726ffc044c135f Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Tue, 6 Jun 2023 12:08:42 -0400 Subject: [PATCH 3/3] fix lint --- namespace/id.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/namespace/id.go b/namespace/id.go index 27911d6..dde72de 100644 --- a/namespace/id.go +++ b/namespace/id.go @@ -28,7 +28,7 @@ func (nid ID) Size() IDSize { } // String returns the hexadecimal encoding of the nid. The output of -// nid.String() is not equivalant to string(nid). +// nid.String() is not equivalent to string(nid). func (nid ID) String() string { return hex.EncodeToString(nid) }