-
Notifications
You must be signed in to change notification settings - Fork 157
/
names.go
49 lines (35 loc) · 1.09 KB
/
names.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
package sdk
import (
"regexp"
"strings"
)
// FieldName represents a name of credential field. It should be title-cased.
// Examples: "Password", "Token", "API Key".
type FieldName string
func (n FieldName) String() string {
return string(n)
}
// CredentialName represents a name of a credential type. It should be title-cased.
// Examples: "Personal Access Token", "API Key".
type CredentialName string
func (n CredentialName) String() string {
return string(n)
}
func (n CredentialName) ID() CredentialTypeID {
return CredentialTypeID(credentialNameToSnakeCase(n))
}
func credentialNameToSnakeCase(name CredentialName) string {
const underscore = "_"
str := name.String()
charsToReplace := regexp.MustCompile(`[-/,. ]`)
str = charsToReplace.ReplaceAllLiteralString(str, underscore)
multipleUnderscores := regexp.MustCompile(`_+`)
str = multipleUnderscores.ReplaceAllString(str, underscore)
str = strings.TrimPrefix(str, "_")
str = strings.TrimSuffix(str, "_")
return strings.ToLower(str)
}
type CredentialTypeID string
func (i CredentialTypeID) String() string {
return string(i)
}