forked from sqlc-dev/sqlc
/
enum.go
39 lines (33 loc) · 784 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
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
}
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
}