-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
94 lines (75 loc) · 2.07 KB
/
utils.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
package jsonstruct
import (
"fmt"
"math/big"
"path"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// GetGoName takes in a field name or file name and returns a "normalized" (CamelCase) string suitable for use as a Go
// variable name.
func GetGoName(input string) string {
var cleaned strings.Builder
if input == "." {
return "Dot" // special case
}
// remove garbage characters, replace separators with ' '
for _, r := range input {
if isAlphaNum(r) {
cleaned.WriteRune(r)
} else if isSeparator(r) {
cleaned.WriteRune(' ')
}
}
// look for initialisms to capitalize
words := strings.Split(cleaned.String(), " ")
temp := []string{}
for _, word := range words {
tmpWord := strings.ToUpper(word)
if _, ok := CommonInitialisms[tmpWord]; ok {
word = strings.ToUpper(tmpWord)
}
temp = append(temp, word)
}
// transform to something like camel-case, leaving capitalizations already present intact
caser := cases.Title(language.AmericanEnglish, cases.NoLower)
result := caser.String(strings.Join(temp, " "))
result = strings.ReplaceAll(result, " ", "")
// variable names can't start with a number
if len(result) > 0 && isNumber(rune(result[0])) {
result = "JSON" + result
}
// unnamed struct fields / struct fields of only spaces break things
if len(strings.TrimSpace(result)) == 0 {
return "Unknown"
}
return result
}
func GetFileGoName(filePath string) string {
_, fileName := path.Split(filePath)
ext := path.Ext(fileName)
return GetGoName(strings.TrimSuffix(fileName, ext))
}
func isAlphaNum(r rune) bool {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || isNumber(r)
}
func isNumber(r rune) bool {
return (r >= '0' && r <= '9')
}
func isSeparator(r rune) bool {
return r == '_' || r == '.' || r == '-' || r == ' '
}
func simpleAnyToString(input any) string {
switch val := input.(type) {
case bool:
return fmt.Sprintf("%t", val)
case float64, *big.Float:
return fmt.Sprintf("%.3f", val)
case int64:
return fmt.Sprintf("%d", val)
case string:
return fmt.Sprintf("\"%s\"", val)
}
return ""
}