From 18fb139e904718c29b803eaddfd0408d698200b5 Mon Sep 17 00:00:00 2001 From: Daniel Paul Carbone Date: Wed, 3 Jan 2018 14:24:22 -0600 Subject: [PATCH] Quick & Dirty update to support upstream changes. --- shortuuid.go | 25 +++++++++++++++++++++---- shortuuid_test.go | 6 +++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/shortuuid.go b/shortuuid.go index 3bb69da..fa8ce69 100644 --- a/shortuuid.go +++ b/shortuuid.go @@ -4,6 +4,7 @@ import ( "strings" uuid "github.com/satori/go.uuid" + "fmt" ) // DefaultEncoder is the default encoder uses when generating new UUIDs, and is @@ -18,21 +19,33 @@ type Encoder interface { // New returns a new UUIDv4, encoded with base57. func New() string { - return DefaultEncoder.Encode(uuid.NewV4()) + str, err := uuid.NewV4() + if err != nil { + panic(fmt.Sprintf("Unable to create UUIDv4: %s", err)) + } + return DefaultEncoder.Encode(str) } // NewWithEncoder returns a new UUIDv4, encoded with enc. func NewWithEncoder(enc Encoder) string { - return enc.Encode(uuid.NewV4()) + str, err := uuid.NewV4() + if err != nil { + panic(fmt.Sprintf("Unable to create UUIDv4: %s", err)) + } + return enc.Encode(str) } // NewWithNamespace returns a new UUIDv5 (or v4 if name is empty), encoded with base57. func NewWithNamespace(name string) string { var u uuid.UUID + var err error switch { case name == "": - u = uuid.NewV4() + u, err = uuid.NewV4() + if err != nil { + panic(fmt.Sprintf("Unable to create UUIDv4: %s", err)) + } case strings.HasPrefix(name, "http"): u = uuid.NewV5(uuid.NamespaceURL, name) default: @@ -46,5 +59,9 @@ func NewWithNamespace(name string) string { // alternative alphabet abc. func NewWithAlphabet(abc string) string { enc := base57{newAlphabet(abc)} - return enc.Encode(uuid.NewV4()) + str, err := uuid.NewV4() + if err != nil { + panic(fmt.Sprintf("Unable to create UUIDv4: %s", err)) + } + return enc.Encode(str) } diff --git a/shortuuid_test.go b/shortuuid_test.go index 73761a8..f45dc21 100644 --- a/shortuuid_test.go +++ b/shortuuid_test.go @@ -229,7 +229,11 @@ func BenchmarkUUID(b *testing.B) { } func BenchmarkEncoding(b *testing.B) { - u := uuid.NewV4() + u, err := uuid.NewV4() + if err != nil { + b.Logf("Unable to create UUIDv4: %s", err) + b.FailNow() + } for i := 0; i < b.N; i++ { DefaultEncoder.Encode(u) }