-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.go
206 lines (171 loc) · 4.09 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
package aqua
import (
"bytes"
"fmt"
"github.com/tolexo/aero/panik"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"unicode"
)
func upFirstChar(inp string) string {
if len(inp) > 0 {
u := []rune(inp)
u[0] = unicode.ToUpper(u[0])
return string(u)
}
return ""
}
func cleanUrl(pieces ...string) string {
var buffer bytes.Buffer
for _, p := range pieces {
buffer.WriteString("/")
buffer.WriteString(p)
}
url := removeMultSlashes(buffer.String())
//url = dropPrefix(url, "/")
return url
}
func dropPrefix(s string, prefix string) string {
if strings.HasPrefix(s, prefix) {
return s[len(prefix):]
}
return s
}
func getServiceId(method string, prefix string, version string, url string) string {
if version != defaults.Version {
version = "v" + version
}
return removeMultSlashes(fmt.Sprintf("%s/%s/%s%s", method, prefix, version, url))
}
var find *regexp.Regexp
func removeMultSlashes(inp string) string {
if find == nil {
find, _ = regexp.Compile("[\\/]+")
}
return find.ReplaceAllString(inp, "/")
}
func getSymbolFromType(t reflect.Type) string {
symb := ""
if t.Kind() == reflect.Ptr {
symb = "*" + getSymbolFromType(t.Elem())
} else if t.Kind() == reflect.Map {
symb = "map"
} else if t.Kind() == reflect.Struct {
symb = "st:" + t.PkgPath() + "." + t.Name()
} else if t.Kind() == reflect.Interface {
symb = "i:" + t.PkgPath() + "." + t.Name()
} else {
symb = t.Name()
}
return symb
}
func getSymbolFromObject(o interface{}) string {
return getSymbolFromType(reflect.TypeOf(o))
}
func toUrlCase(camel string) string {
var words []string
l := 0
for s := camel; s != ""; s = s[l:] {
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1
if l <= 0 {
l = len(s)
}
words = append(words, s[:l])
}
return strings.ToLower(strings.Join(words, "-"))
}
func panicIf(e error) {
if e != nil {
panic(e)
}
}
func getUrl(url string, headers map[string]string) (httpCode int, contentType string, content string) {
req, _ := http.NewRequest("GET", url, nil)
if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}
client := &http.Client{}
resp, err := client.Do(req)
panicIf(err)
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
panicIf(err)
return resp.StatusCode, resp.Header.Get("Content-Type"), string(data)
}
func postUrl(uri string, post map[string]string, headers map[string]string) (httpCode int, contentType string, content string) {
form := url.Values{}
for key, val := range post {
form.Set(key, val)
}
req, err := http.NewRequest("POST", uri, strings.NewReader(form.Encode()))
if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}
client := &http.Client{}
resp, err := client.Do(req)
panicIf(err)
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
panicIf(err)
return resp.StatusCode, resp.Header.Get("Content-Type"), string(data)
}
var portForTesting int = 8095
func getUniquePortForTestCase() int {
portForTesting++
return portForTesting
}
func getHttpMethod(field reflect.StructField) string {
var out string = ""
switch field.Type.String() {
case "aqua.GetApi", "aqua.PostApi", "aqua.PutApi", "aqua.PatchApi", "aqua.DeleteApi":
out = field.Type.String()
out = out[5 : len(out)-3]
out = strings.ToUpper(out)
}
return out
}
var muxStyle *regexp.Regexp
func extractRouteVars(url string) []string {
if muxStyle == nil {
muxStyle, _ = regexp.Compile(`{[^/]+}`)
}
matches := muxStyle.FindAllString(url, -1)
var colonPos int
for i, m := range matches {
m = m[1 : len(m)-1] // drop { and }
colonPos = strings.Index(m, ":")
if colonPos > 0 {
m = m[0:colonPos]
}
matches[i] = m
}
return matches
}
func convertToType(vars []string, typ []string) []reflect.Value {
vals := make([]reflect.Value, len(vars))
for i, v := range vars {
t := typ[i]
switch t {
case "string":
vals[i] = reflect.ValueOf(v)
case "int":
j, err := strconv.Atoi(v)
if err != nil {
panik.Do("Cannot convert [%s] to 'int'", v)
}
vals[i] = reflect.ValueOf(j)
default:
panik.Do("Type [%s] is not supported", t)
}
}
return vals
}