/
utils.go
131 lines (113 loc) · 2.32 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
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
package xj2go
import (
"os"
"reflect"
"regexp"
"strings"
)
// https://github.com/golang/lint/blob/39d15d55e9777df34cdffde4f406ab27fd2e60c0/lint.go#L695-L731
var commonInitialisms = map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"XSRF": true,
"XSS": true,
}
func max(nodes []leafNode) int {
n := 0
for _, node := range nodes {
t := strings.Count(node.path, ".")
if n < t {
n = t
}
}
return n + 1
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
var toProperCaseRE = regexp.MustCompile(`([A-Z])([a-z]+)`)
var toProperCaseCache = make(map[string]string)
func toProperCase(str string) string {
// Check if already cached
cached, found := toProperCaseCache[str]
if found {
return cached
}
subProperCase := func(v string) string {
if commonInitialisms[strings.ToTitle(v)] {
v = strings.ToTitle(v)
} else {
v = strings.Title(v)
}
return v
}
replaced := toProperCaseRE.ReplaceAllStringFunc(str, subProperCase)
s := strings.Split(replaced, "_")
result := ""
for _, v := range s {
result += subProperCase(v)
}
// Keep in cache for future call
toProperCaseCache[str] = result
return result
}
var toProperTypeRE = regexp.MustCompile(`\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)`)
//TODO: should be optimize for time type
func toProperType(v interface{}) string {
t := reflect.TypeOf(v)
if t.Kind() == reflect.String {
if toProperTypeRE.MatchString(v.(string)) {
return "time.Time"
}
}
if t.Kind() == reflect.Struct {
//detect xmlVal struct val field
for i := 0; i < t.NumField(); i++ {
if t.Field(i).Name == "val" {
return t.Field(i).Type.String()
}
}
}
// if _, isTime := v.(time.Time); isTime {
// return "time.Time"
// }
return t.String()
}