-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
280 lines (250 loc) · 6.2 KB
/
functions.go
File metadata and controls
280 lines (250 loc) · 6.2 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package codegenerator
import (
"fmt"
"strings"
"text/template"
"unicode"
)
func AuxilirayFunctions() template.FuncMap {
return template.FuncMap{
"quote": Quote,
"capitalCamelCase": CapitalCamelCase,
"lowerCamelCase": LowerCamelCase,
"orVoid": OrVoid,
"goType": GoType,
"goTypeWithModule": GoTypeWithModule,
"typescriptTypeWithModule": TypescriptTypeWithModule,
"uppercaseToCapitalized": UppercaseToCapitalized,
"replace": Replace,
"split": Split,
"splitIndexOf": SplitIndexOf,
"splitIndexOfNegative": SplitIndexOfNegative,
"lowerNoUnderscore": LowerNoUnderscore,
"camelCaseNoUnderscore": CamelCaseNoUnderscore,
"capitalCamelCaseNoUnderscore": CapitalCamelCaseNoUnderscore,
"upperSpaceToUnderscore": UpperSpaceToUnderscore,
"rustType": RustType,
"camelCaseToLowerSnakeCase": CamelCaseToLowerSnakeCase,
"has": Has,
"trimPrefix": TrimPrefix,
"trimSuffix": TrimSuffix,
"isBasicType": IsBasicType,
"stringSliceContains": StringSliceContains,
"cutTakeBefore": CutTakeBefore,
"cutTakeAfter": CutTakeAfter,
"stringSlicesIntersect": StringSlicesIntersect,
"stringSliceOnlyContainsEntriesFromStringSlice": StringSliceOnlyContainsEntriesFromStringSlice,
}
}
func Has(input any, field string) bool {
asMap, ok := input.(map[string]any)
if !ok {
asMap, ok := input.(map[any]any)
if !ok {
return false
}
_, has := asMap[field]
return has
}
_, has := asMap[field]
return has
}
func Quote(input string) string {
return fmt.Sprintf(`"%s"`, strings.ReplaceAll(input, `"`, `\"`))
}
func CapitalCamelCase(input string) string {
return strings.ToUpper(input[:1]) + input[1:]
}
func LowerCamelCase(input string) string {
return strings.ToLower(input[:1]) + input[1:]
}
func OrVoid(input any) string {
if input == nil {
return "void"
}
return input.(string)
}
func IsBasicType(input string) bool {
return input == "string" || input == "number" || input == "boolean" || input == "unknown"
}
func StringSliceContains(haystack []any, needle string) bool {
for _, hay := range haystack {
value, ok := hay.(string)
if !ok {
continue
}
if value == needle {
return true
}
}
return false
}
func StringSlicesIntersect(haystack []any, needle []any) bool {
for _, n := range needle {
asString, ok := n.(string)
if !ok {
return false
}
if StringSliceContains(haystack, asString) {
return true
}
}
return false
}
func StringSliceOnlyContainsEntriesFromStringSlice(haystack []any, needle []any) bool {
for _, n := range needle {
asString, ok := n.(string)
if !ok {
return false
}
if !StringSliceContains(haystack, asString) {
return false
}
}
return true
}
func goType(input string) string {
if input == "string" {
return "string"
}
if input == "number" {
return "int64"
}
if input == "boolean" {
return "bool"
}
if input == "unknown" {
return "any"
}
return ""
}
func GoType(input string) string {
converted := goType(input)
if converted != "" {
return converted
}
return input
}
func GoTypeWithModule(module, input string) string {
converted := goType(input)
if converted != "" {
return converted
}
return fmt.Sprintf("%s%s", module, input)
}
func TypescriptTypeWithModule(module, input string) string {
if input == "string" {
return "string"
}
if input == "number" {
return "number"
}
if input == "boolean" {
return "boolean"
}
if input == "unknown" {
return "unknown"
}
if input == "any" {
return "any"
}
if input == "void" {
return "void"
}
return fmt.Sprintf("%s%s", module, input)
}
func UppercaseToCapitalized(input string) string {
return strings.ToUpper(input[:1]) + strings.ToLower(input[1:])
}
func Replace(whenFound string, replaceWith string, input string) string {
return strings.ReplaceAll(input, whenFound, replaceWith)
}
func Split(seperator string, input string) []string {
return strings.Split(input, seperator)
}
func SplitIndexOf(seperator string, lookFor string, input string) int {
parts := strings.Split(input, seperator)
for i, part := range parts {
if part == lookFor {
return i
}
}
return -1
}
func SplitIndexOfNegative(seperator string, lookFor string, input string) int {
parts := strings.Split(input, seperator)
for i, part := range parts {
if part == lookFor {
return i - len(parts)
}
}
return -1000000
}
func LowerNoUnderscore(input string) string {
return strings.ToLower(strings.ReplaceAll(input, "_", ""))
}
func CapitalCamelCaseNoUnderscore(input string) string {
parts := strings.Split(input, "_")
for i, part := range parts {
parts[i] = strings.ToUpper(part[:1]) + strings.ToLower(part[1:])
}
return strings.Join(parts, "")
}
func CamelCaseNoUnderscore(input string) string {
capital := CapitalCamelCaseNoUnderscore(input)
return strings.ToLower(capital[:1]) + capital[1:]
}
func UpperSpaceToUnderscore(input string) string {
return strings.ToUpper(strings.ReplaceAll(input, " ", "_"))
}
func RustType(input string) string {
if input == "string" {
return "String"
}
if input == "number" {
return "i64"
}
if input == "boolean" {
return "bool"
}
return input
}
func CamelCaseToLowerSnakeCase(input string) string {
if len(input) == 0 {
return ""
}
result := strings.ToLower(input[:1])
for i, runeValue := range input {
if i == 0 {
continue
}
if unicode.IsUpper(runeValue) {
result += "_" + string(unicode.ToLower(runeValue))
} else {
result += string(runeValue)
}
}
return result
}
func TrimPrefix(prefix string, input string) string {
return strings.TrimPrefix(input, prefix)
}
func TrimSuffix(suffix string, input string) string {
return strings.TrimSuffix(input, suffix)
}
func CutTakeBefore(seperator string, input string) string {
before, _, ok := strings.Cut(input, seperator)
if ok {
return before
} else {
return ""
}
}
func CutTakeAfter(seperator string, input string) string {
_, after, ok := strings.Cut(input, seperator)
if ok {
return after
} else {
return ""
}
}