forked from plimble/arlong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
210 lines (186 loc) · 3.76 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
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
package arlong
import (
"path"
"strconv"
"strings"
"unicode/utf8"
)
func findAt(text string) int {
size := 0
index := 0
var char rune
for size < len(text) {
char, size = utf8.DecodeRuneInString(text)
text = text[size:]
switch char {
case ' ', '/':
index += size
continue
case '@':
return index
default:
return -1
}
}
return -1
}
func getValues(text string) (string, string) {
text = strings.Replace(text, "\t", " ", -1)
fullText := text
size := 0
index := 0
var char rune
for size < len(text) {
char, size = utf8.DecodeRuneInString(text)
text = text[size:]
switch char {
case ' ':
return strings.TrimSpace(fullText[:index]), strings.TrimSpace(fullText[index:])
default:
index += size
}
}
return strings.TrimSpace(fullText), ""
}
func getValueByKey(s string, delim ...rune) map[string]string {
if len(delim) == 0 {
delim = []rune{'='}
}
result := map[string]string{}
fullText := s
size := 0
index := 0
key := ""
inQuote := false
startVal := 0
startKey := 0
var char rune
for size < len(s) {
char, size = utf8.DecodeRuneInString(s)
s = s[size:]
switch char {
case ' ', '\t':
if inQuote {
index += size
} else {
if key != "" {
result[key] = strings.TrimSpace(fullText[startVal:index])
key = ""
index += size
startKey = index
} else {
if fullText[startKey:index] != "" {
result[strings.TrimSpace(fullText[startKey:index])] = ""
startKey = index
}
index += size
}
}
case '"':
if !inQuote {
inQuote = true
index += size
startVal = index
} else {
result[key] = strings.TrimSpace(fullText[startVal:index])
inQuote = false
key = ""
index += size
startKey = index
}
case delim[0]:
key = strings.TrimSpace(fullText[startKey:index])
index += size
result[key] = ""
startVal = index
default:
index += size
}
}
if key != "" {
if !inQuote {
index += size
}
result[key] = strings.TrimSpace(fullText[startVal:index])
}
return result
}
func checkTypePtr(s string) string {
char, _ := utf8.DecodeRuneInString(s)
if char == '&' {
strs := strings.Split(s, " ")
s = strings.TrimPrefix(strs[0], "&{") + "." + strings.TrimSuffix(strs[1], "}")
}
return s
}
func getTypeFormat(val string) (string, string, bool) {
switch val {
case "string":
return "string", "", true
case "int":
return "integer", "int32", true
case "int32":
return "integer", "int32", true
case "int64":
return "integer", "int64", true
case "float32", "float64":
return "number", "float", true
case "bool":
return "boolean", "", true
case "date-time", "time.Time", "Time", "time":
return "string", "date-time", true
case "date":
return "string", "date", true
case "object":
return "object", "", true
case "array":
return "array", "", true
}
return val, "", false
}
func strToInt(val string) int {
valInt, err := strconv.Atoi(val)
if err != nil {
panic(err)
}
return valInt
}
func getValueStrings(s string) []string {
fields := strings.Fields(s)
for i := 0; i < len(fields); i++ {
fields[i] = strings.TrimSpace(fields[i])
}
return fields
}
func getValueMapStrings(s string) map[string][]string {
result := map[string][]string{}
data := getValueByKey(s)
for key, val := range data {
result[key] = strings.Split(val, ",")
}
return result
}
func getMime(s string) string {
switch s {
case "xml":
return "application/xml"
case "json":
return "application/json"
case "html":
return "text/html"
case "text":
return "text/plain"
case "form":
return "application/x-www-form-urlencoded"
case "multipart":
return "multipart/form-data"
}
return s
}
func pathMatch(pattern, name string) bool {
exist, err := path.Match(pattern, name)
if err != nil {
panic(err)
}
return exist
}