Skip to content

Commit 8c9801f

Browse files
authored
Make UUID an alias of uuid.UUID (#571)
Instead of wrapping Google's `uuid.UUID`, and reimplementing the JSON logic, simply make it an alias instead. The current version already leaks the implementation detail, so I don't see any reason to also wrap it, and make its usage harder.
1 parent bef2a63 commit 8c9801f

File tree

2 files changed

+3
-23
lines changed

2 files changed

+3
-23
lines changed

uuid.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
11
package types
22

33
import (
4-
"encoding/json"
5-
"errors"
6-
74
"github.com/google/uuid"
85
)
96

10-
type UUID uuid.UUID
11-
12-
func (u UUID) MarshalJSON() ([]byte, error) {
13-
return json.Marshal(uuid.UUID(u).String())
14-
}
15-
16-
func (u *UUID) UnmarshalJSON(data []byte) error {
17-
var s string
18-
if err := json.Unmarshal(data, &s); err != nil {
19-
return err
20-
}
21-
parsed, err := uuid.Parse(s)
22-
if err != nil {
23-
return errors.New("uuid: failed to pass validation")
24-
}
25-
*u = UUID(parsed)
26-
return nil
27-
}
7+
type UUID = uuid.UUID

uuid_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestUUID_MarshalJSON_Pass(t *testing.T) {
2525
b := struct {
2626
UUIDField UUID `json:"uuid"`
2727
}{
28-
UUIDField: UUID(testUUID),
28+
UUIDField: testUUID,
2929
}
3030
jsonBytes, err := json.Marshal(b)
3131
assert.NoError(t, err)
@@ -42,7 +42,7 @@ func TestUUID_UnmarshalJSON_Fail(t *testing.T) {
4242
}
4343

4444
func TestUUID_UnmarshalJSON_Pass(t *testing.T) {
45-
testUUID := UUID(uuid.MustParse("9cb14230-b640-11ec-b909-0242ac120002"))
45+
testUUID := uuid.MustParse("9cb14230-b640-11ec-b909-0242ac120002")
4646
jsonStr := `{"uuid":"9cb14230-b640-11ec-b909-0242ac120002"}`
4747
b := struct {
4848
UUIDField UUID `json:"uuid"`

0 commit comments

Comments
 (0)