-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
uniqname.go
63 lines (47 loc) 路 1.35 KB
/
uniqname.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package uniqname
import (
"fmt"
"regexp"
"strings"
)
// Separator is a separator between release name and namespace.
const Separator = "@"
var validateRegexp = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])?")
// UniqName is an alias for string.
type UniqName string
// Generate returns uniqname for provided release name and namespace.
func Generate(name, namespace string) (UniqName, error) {
u := UniqName(fmt.Sprintf("%s%s%s", name, Separator, namespace))
return u, u.Validate()
}
// GenerateWithDefaultNamespace parses uniqname out of provided line.
// If there is no namespace in line, default namespace will be used.
func GenerateWithDefaultNamespace(line, namespace string) (UniqName, error) {
s := strings.Split(line, Separator)
name := s[0]
if len(s) > 1 && s[1] != "" {
namespace = s[1]
}
return Generate(name, namespace)
}
// Equal checks whether uniqnames are equal.
func (n UniqName) Equal(a UniqName) bool {
return n == a
}
// Validate validates this object.
func (n UniqName) Validate() error {
s := strings.Split(n.String(), Separator)
if len(s) != 2 {
return NewValidationError(n.String())
}
if !validateRegexp.MatchString(s[0]) {
return NewValidationError(n.String())
}
if !validateRegexp.MatchString(s[1]) {
return NewValidationError(n.String())
}
return nil
}
func (n UniqName) String() string {
return string(n)
}