-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
159 lines (139 loc) · 3.16 KB
/
util.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"os"
"path/filepath"
"strings"
"unicode"
"unicode/utf8"
)
type style string
const (
kebabCase style = "kebab"
underscoreCase style = "underscore"
camelCase style = "camel"
pascalCase style = "pascal"
lowerCase style = "lower"
)
// convert to underscore style, example: UserProfile > user_profile
func toUnderscoreCase(s string) string {
return toLowerCase(s, 95)
}
// convert to kebab style, example: UserProfile > user-profile
func toKebabCase(s string) string {
return toLowerCase(s, 45)
}
// convert to camel style, example: user-profile > userProfile
func toCamelCase(s string) string {
chars := make([]rune, 0, len(s))
upper := false
first := true
for i := 0; i < len(s); i++ {
switch {
case s[i] >= 65 && s[i] <= 90:
if first {
chars = append(chars, rune(s[i]+32))
} else {
chars = append(chars, rune(s[i]))
}
first = false
upper = false
case s[i] >= 97 && s[i] <= 122:
if upper && !first {
chars = append(chars, rune(s[i]-32))
} else {
chars = append(chars, rune(s[i]))
}
first = false
upper = false
case s[i] == 45:
upper = true
case s[i] == 95:
upper = true
}
}
return string(chars)
}
// convert to pascal style, example: user-profile > UserProfile
func toPascalCase(s string) string {
s = toCamelCase(s)
return strings.ToUpper(string(s[0])) + s[1:]
}
func toLowerCase(s string, c rune) string {
chars := make([]rune, 0)
for i := 0; i < len(s); i++ {
if s[i] >= 65 && s[i] <= 90 {
if i == 0 {
chars = append(chars, rune(s[i]+32))
} else {
chars = append(chars, c, rune(s[i]+32))
}
} else {
chars = append(chars, rune(s[i]))
}
}
return string(chars)
}
func toPackageName(s string) string {
chars := make([]rune, 0, len(s))
for i := 0; i < len(s); i++ {
switch {
case s[i] >= 65 && s[i] <= 90:
chars = append(chars, rune(s[i]+32))
case s[i] >= 97 && s[i] <= 122:
chars = append(chars, rune(s[i]))
}
}
return string(chars)
}
func toPackagePath(s string, style style) string {
switch style {
case kebabCase:
return toKebabCase(s)
case underscoreCase:
return toUnderscoreCase(s)
case camelCase:
return toCamelCase(s)
case pascalCase:
return toPascalCase(s)
case lowerCase:
return toPackageName(s)
default:
return toKebabCase(s)
}
}
func toFileName(s string, style style) string {
switch style {
case kebabCase:
return toKebabCase(s)
case underscoreCase:
return toUnderscoreCase(s)
case camelCase:
return toCamelCase(s)
case pascalCase:
return toPascalCase(s)
case lowerCase:
return toPackageName(s)
default:
return toUnderscoreCase(s)
}
}
func doWrite(file string, tpl string, replaces map[string]string) error {
s := os.Expand(tpl, func(s string) string {
switch {
case len(s) >= 3 && s[:3] == "Var":
return replaces[s]
case len(s) >= 6 && s[:6] == "Symbol":
return replaces[s]
default:
return "$" + s
}
})
if err := os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil {
return err
}
return os.WriteFile(file, []byte(strings.TrimPrefix(s, "\n")), os.ModePerm)
}
func isExportable(s string) bool {
r, _ := utf8.DecodeRuneInString(s)
return unicode.IsUpper(r)
}