Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding a UUID format * Removing indirect (hopefully build passes now) Co-authored-by: Chrusty <>
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type UUID string | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
func (u UUID) MarshalJSON() ([]byte, error) { | ||
if _, err := uuid.Parse(string(u)); err != nil { | ||
return nil, errors.New("uuid: failed to pass validation") | ||
} | ||
return json.Marshal(string(u)) | ||
} | ||
|
||
func (u *UUID) UnmarshalJSON(data []byte) error { | ||
var s string | ||
if err := json.Unmarshal(data, &s); err != nil { | ||
return err | ||
} | ||
if _, err := uuid.Parse(s); err != nil { | ||
return errors.New("uuid: failed to pass validation") | ||
} | ||
*u = UUID(s) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUUID_MarshalJSON_Fail(t *testing.T) { | ||
testUUID := "this-is-not-a-uuid" | ||
b := struct { | ||
UUIDField UUID `json:"uuid"` | ||
}{ | ||
UUIDField: UUID(testUUID), | ||
} | ||
_, err := json.Marshal(b) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestUUID_MarshalJSON_Pass(t *testing.T) { | ||
testUUID := "9cb14230-b640-11ec-b909-0242ac120002" | ||
b := struct { | ||
UUIDField UUID `json:"uuid"` | ||
}{ | ||
UUIDField: UUID(testUUID), | ||
} | ||
jsonBytes, err := json.Marshal(b) | ||
assert.NoError(t, err) | ||
assert.JSONEq(t, `{"uuid":"9cb14230-b640-11ec-b909-0242ac120002"}`, string(jsonBytes)) | ||
} | ||
|
||
func TestUUID_UnmarshalJSON_Fail(t *testing.T) { | ||
jsonStr := `{"uuid":"this-is-not-a-uuid"}` | ||
b := struct { | ||
UUIDField UUID `json:"uuid"` | ||
}{} | ||
err := json.Unmarshal([]byte(jsonStr), &b) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestUUID_UnmarshalJSON_Pass(t *testing.T) { | ||
testUUID := UUID("9cb14230-b640-11ec-b909-0242ac120002") | ||
jsonStr := `{"uuid":"9cb14230-b640-11ec-b909-0242ac120002"}` | ||
b := struct { | ||
UUIDField UUID `json:"uuid"` | ||
}{} | ||
err := json.Unmarshal([]byte(jsonStr), &b) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testUUID, b.UUIDField) | ||
} |
Wouldn't it be nicer if UUID was an alias of string rather than a 'new' type? (so
type UUID = string
instead oftype UUID string
). Then casting to/from string wouldn't be necessary