Skip to content

Commit

Permalink
Merge pull request #7 from myENA/master
Browse files Browse the repository at this point in the history
Quick & Dirty update to support upstream changes.
  • Loading branch information
Peter Renström authored Jan 3, 2018
2 parents d728e00 + 19d263e commit 84f2543
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 21 additions & 4 deletions shortuuid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shortuuid

import (
"fmt"
"strings"

uuid "github.com/satori/go.uuid"
Expand All @@ -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:
Expand All @@ -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)
}
6 changes: 5 additions & 1 deletion shortuuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 84f2543

Please sign in to comment.