forked from sqlc-dev/sqlc
/
enum.go
49 lines (41 loc) · 982 Bytes
/
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
package golang
import (
"regexp"
"strings"
)
var IdentPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")
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 EnumReplace(value string) string {
id := strings.Replace(value, "-", "_", -1)
id = strings.Replace(id, ":", "_", -1)
id = strings.Replace(id, "/", "_", -1)
return IdentPattern.ReplaceAllString(id, "")
}
func EnumValueName(value string) string {
name := ""
id := strings.Replace(value, "-", "_", -1)
id = strings.Replace(id, ":", "_", -1)
id = strings.Replace(id, "/", "_", -1)
id = IdentPattern.ReplaceAllString(id, "")
for _, part := range strings.Split(id, "_") {
name += strings.Title(part)
}
return name
}