Skip to content

Commit bef2a63

Browse files
authored
Swap UUID format to byte array (#556)
With #546 merged in, UUIDs are defined as an alias for a string. Before that implementation gets mass adoption, I wanted to propose a slightly different alternative: basing the custom type off of `uuid.UUID` over `string`. This has the advantages of: - Being convertable back to a `uuid.UUID` without having to parse the string a second time, or opening the conversion up to failure scenarios - Still being easily convertable to `string` with the `uuid.UUID`'s `String` method - Taking up less space in memory - Reducing the number of invalid representations, since only `[16]byte`s can be represented, as opposed to all strings - Not adding any dependencies, since `uuid.UUID` is already being imported
1 parent 0d23439 commit bef2a63

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

uuid.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ import (
77
"github.com/google/uuid"
88
)
99

10-
type UUID string
10+
type UUID uuid.UUID
1111

1212
func (u UUID) MarshalJSON() ([]byte, error) {
13-
if _, err := uuid.Parse(string(u)); err != nil {
14-
return nil, errors.New("uuid: failed to pass validation")
15-
}
16-
return json.Marshal(string(u))
13+
return json.Marshal(uuid.UUID(u).String())
1714
}
1815

1916
func (u *UUID) UnmarshalJSON(data []byte) error {
2017
var s string
2118
if err := json.Unmarshal(data, &s); err != nil {
2219
return err
2320
}
24-
if _, err := uuid.Parse(s); err != nil {
21+
parsed, err := uuid.Parse(s)
22+
if err != nil {
2523
return errors.New("uuid: failed to pass validation")
2624
}
27-
*u = UUID(s)
25+
*u = UUID(parsed)
2826
return nil
2927
}

uuid_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ import (
44
"encoding/json"
55
"testing"
66

7+
"github.com/google/uuid"
78
"github.com/stretchr/testify/assert"
89
)
910

10-
func TestUUID_MarshalJSON_Fail(t *testing.T) {
11-
testUUID := "this-is-not-a-uuid"
11+
func TestUUID_MarshalJSON_Zero(t *testing.T) {
12+
var testUUID UUID
1213
b := struct {
1314
UUIDField UUID `json:"uuid"`
1415
}{
15-
UUIDField: UUID(testUUID),
16+
UUIDField: testUUID,
1617
}
17-
_, err := json.Marshal(b)
18-
assert.Error(t, err)
18+
marshaled, err := json.Marshal(b)
19+
assert.NoError(t, err)
20+
assert.JSONEq(t, `{"uuid":"00000000-0000-0000-0000-000000000000"}`, string(marshaled))
1921
}
2022

2123
func TestUUID_MarshalJSON_Pass(t *testing.T) {
22-
testUUID := "9cb14230-b640-11ec-b909-0242ac120002"
24+
testUUID := uuid.MustParse("9cb14230-b640-11ec-b909-0242ac120002")
2325
b := struct {
2426
UUIDField UUID `json:"uuid"`
2527
}{
@@ -40,7 +42,7 @@ func TestUUID_UnmarshalJSON_Fail(t *testing.T) {
4042
}
4143

4244
func TestUUID_UnmarshalJSON_Pass(t *testing.T) {
43-
testUUID := UUID("9cb14230-b640-11ec-b909-0242ac120002")
45+
testUUID := UUID(uuid.MustParse("9cb14230-b640-11ec-b909-0242ac120002"))
4446
jsonStr := `{"uuid":"9cb14230-b640-11ec-b909-0242ac120002"}`
4547
b := struct {
4648
UUIDField UUID `json:"uuid"`

0 commit comments

Comments
 (0)