Improved anime view on characters
This commit is contained in:
31
arn/ID.go
31
arn/ID.go
@ -1,4 +1,35 @@
|
||||
package arn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
shortid "github.com/ventu-io/go-shortid"
|
||||
)
|
||||
|
||||
// ID is used for object identification and is simply a string.
|
||||
type ID = string
|
||||
|
||||
// GenerateID generates a unique ID for a given collection.
|
||||
func GenerateID(collection string) ID {
|
||||
id, _ := shortid.Generate()
|
||||
|
||||
// Retry until we find an unused ID
|
||||
retry := 0
|
||||
|
||||
for {
|
||||
_, err := DB.Get(collection, id)
|
||||
|
||||
if err != nil && strings.Contains(err.Error(), "not found") {
|
||||
return id
|
||||
}
|
||||
|
||||
retry++
|
||||
|
||||
if retry > 10 {
|
||||
panic(errors.New("Can't generate unique ID"))
|
||||
}
|
||||
|
||||
id, _ = shortid.Generate()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user