/
uuid.go
48 lines (41 loc) · 1.14 KB
/
uuid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package uuid
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/pachyderm/pachyderm/src/server/pkg/backoff"
"github.com/satori/go.uuid"
)
// New returns a new uuid.
func New() string {
var result string
backoff.RetryNotify(func() error {
uuid, err := uuid.NewV4()
if err != nil {
return err
}
result = uuid.String()
return nil
}, backoff.NewInfiniteBackOff(), func(err error, d time.Duration) error {
fmt.Printf("error from uuid.NewV4: %v", err)
return nil
})
return result
}
// NewWithoutDashes returns a new uuid without no "-".
func NewWithoutDashes() string {
return strings.Replace(New(), "-", "", -1)
}
// NewWithoutUnderscores returns a new uuid without no "_".
func NewWithoutUnderscores() string {
return strings.Replace(New(), "_", "", -1)
}
// IsUUIDWithoutDashes checks whether a string is a UUID without dashes
func IsUUIDWithoutDashes(s string) bool {
return uuidWithoutDashesRegexp.MatchString(s)
}
// Because we use UUIDv4, the 13th character is a '4'.
// Moreover, a UUID can only contain "hexadecimal" characters,
// lowercase here.
var uuidWithoutDashesRegexp = regexp.MustCompile("[0-9a-f]{12}4[0-9a-f]{19}")