Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick & Dirty update to support upstream changes. #7

Merged
merged 2 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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