-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathenum.go
64 lines (53 loc) · 1.23 KB
/
enum.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
64
package golang
import (
"strings"
"unicode"
)
type Constant struct {
Name string
Type string
Value string
}
type Enum struct {
Name string
Comment string
Constants []Constant
NameTags map[string]string
ValidTags map[string]string
}
func (e Enum) NameTag() string {
return TagsToString(e.NameTags)
}
func (e Enum) ValidTag() string {
return TagsToString(e.ValidTags)
}
func enumReplacer(r rune) rune {
if strings.ContainsRune("-/:_", r) {
return '_'
} else if (r >= 'a' && r <= 'z') ||
(r >= 'A' && r <= 'Z') ||
(r >= '0' && r <= '9') {
return r
} else {
return -1
}
}
// EnumReplace removes all non ident symbols (all but letters, numbers and
// underscore) and returns valid ident name for provided name.
func EnumReplace(value string) string {
return strings.Map(enumReplacer, value)
}
// EnumValueName removes all non ident symbols (all but letters, numbers and
// underscore) and converts snake case ident to camel case.
func EnumValueName(value string) string {
parts := strings.Split(EnumReplace(value), "_")
for i, part := range parts {
parts[i] = titleFirst(part)
}
return strings.Join(parts, "")
}
func titleFirst(s string) string {
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
return string(r)
}