Skip to content

Commit

Permalink
Quick & Dirty update to support upstream changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 3, 2018
1 parent d728e00 commit 18fb139
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
Expand Up @@ -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
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 18fb139

Please sign in to comment.