1
0
mirror of https://github.com/google/uuid.git synced 2024-11-24 08:32:23 +02:00

replace Must* funcs with a single generic Must func

The MustParse and MustNewUUID functions have been removed since they
can be replaced simply using the new function.

    uuid.Must(uuid.Parse(s))
    uuid.Must(uuid.NewUUID())

This also fixes a spurious bug in the UnmarshalJSON method that
prevented compiling the json.go file.
This commit is contained in:
Bryan Matsuo 2016-02-24 11:34:15 -08:00
parent a8b7006b7b
commit 7dd4798941
6 changed files with 11 additions and 21 deletions

View File

@ -12,10 +12,10 @@ import (
// Well known namespace IDs and UUIDs // Well known namespace IDs and UUIDs
var ( var (
NameSpace_DNS = MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") NameSpace_DNS = Must(Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))
NameSpace_URL = MustParse("6ba7b811-9dad-11d1-80b4-00c04fd430c8") NameSpace_URL = Must(Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8"))
NameSpace_OID = MustParse("6ba7b812-9dad-11d1-80b4-00c04fd430c8") NameSpace_OID = Must(Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8"))
NameSpace_X500 = MustParse("6ba7b814-9dad-11d1-80b4-00c04fd430c8") NameSpace_X500 = Must(Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8"))
NIL UUID // empty UUID, all zeros NIL UUID // empty UUID, all zeros
) )

View File

@ -10,7 +10,7 @@ import (
"testing" "testing"
) )
var testUUID = MustParse("f47ac10b-58cc-0372-8567-0e02b2c3d479") var testUUID = Must(Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479"))
func TestJSON(t *testing.T) { func TestJSON(t *testing.T) {
type S struct { type S struct {

View File

@ -42,7 +42,7 @@ func TestClockSeqRace(t *testing.T) {
select { select {
case <-done: case <-done:
return return
case ch <- MustNewUUID(): case ch <- Must(NewUUID()):
} }
} }
}() }()

View File

@ -15,7 +15,7 @@ func TestScan(t *testing.T) {
var invalidTest string = "f47ac10b-58cc-0372-8567-0e02b2c3d4" var invalidTest string = "f47ac10b-58cc-0372-8567-0e02b2c3d4"
byteTest := make([]byte, 16) byteTest := make([]byte, 16)
byteTestUUID := MustParse(stringTest) byteTestUUID := Must(Parse(stringTest))
copy(byteTest, byteTestUUID[:]) copy(byteTest, byteTestUUID[:])
// sunny day tests // sunny day tests
@ -94,7 +94,7 @@ func TestScan(t *testing.T) {
func TestValue(t *testing.T) { func TestValue(t *testing.T) {
stringTest := "f47ac10b-58cc-0372-8567-0e02b2c3d479" stringTest := "f47ac10b-58cc-0372-8567-0e02b2c3d479"
uuid := MustParse(stringTest) uuid := Must(Parse(stringTest))
val, _ := uuid.Value() val, _ := uuid.Value()
if val != stringTest { if val != stringTest {
t.Error("Value() did not return expected string") t.Error("Value() did not return expected string")

View File

@ -77,12 +77,12 @@ func ParseBytes(b []byte) (UUID, error) {
return Parse(*(*string)(unsafe.Pointer(&b))) return Parse(*(*string)(unsafe.Pointer(&b)))
} }
func MustParse(s string) UUID { // Must returns uuid if err is nil and panics otherwise.
u, err := Parse(s) func Must(uuid UUID, err error) UUID {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return u return uuid
} }
// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

View File

@ -42,13 +42,3 @@ func NewUUID() (UUID, error) {
return uuid, nil return uuid, nil
} }
// MustNewUUID returns the Verison 1 UUID from calling NewUUID, or panics
// if NewUUID fails.
func MustNewUUID() UUID {
uuid, err := NewUUID()
if err != nil {
panic(err)
}
return uuid
}