-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathid.go
43 lines (35 loc) · 1.07 KB
/
id.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
package ocm
import (
"fmt"
"github.com/rs/xid"
)
const (
// MaxClusterNameLength - defines maximum length of an OSD cluster name
MaxClusterNameLength = 15
)
// NOTE: the current mock generation exports to a _test file, if in the future this should be made public, consider
// moving the type into a ocmtest package.
// IDGenerator interface for string ID generators.
//
//go:generate moq -out idgenerator_moq.go . IDGenerator
type IDGenerator interface {
// Generate create a new string ID.
Generate() string
}
var _ IDGenerator = idGenerator{}
// idGenerator internal implementation of IDGenerator.
type idGenerator struct {
// prefix a string to prefix to any generated ID.
prefix string
}
// NewIDGenerator create a new default implementation of IDGenerator.
func NewIDGenerator(prefix string) IDGenerator {
return idGenerator{
prefix: prefix,
}
}
// It is not allowed for the cluster name to be longer than 15 characters, hence
// the truncation
func (i idGenerator) Generate() string {
return fmt.Sprintf("%s%s", i.prefix, xid.New().String())[0:MaxClusterNameLength]
}