2019-11-18 05:01:13 +00:00
|
|
|
package arn
|
|
|
|
|
2019-11-19 02:39:20 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2022-06-29 03:12:13 +00:00
|
|
|
"github.com/teris-io/shortid"
|
2019-11-19 02:39:20 +00:00
|
|
|
)
|
|
|
|
|
2019-11-18 05:01:13 +00:00
|
|
|
// ID is used for object identification and is simply a string.
|
|
|
|
type ID = string
|
2019-11-19 02:39:20 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
}
|