-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
433 lines (376 loc) · 10.2 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package grest
import (
"bytes"
"database/sql/driver"
"fmt"
"net/http"
"net/url"
"os"
"path"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"sort"
"strings"
"time"
"github.com/gosimple/slug"
"github.com/jinzhu/gorm"
"github.com/jinzhu/now"
"github.com/microcosm-cc/bluemonday"
)
// AppRoot app root path
var AppRoot, _ = os.Getwd()
// ContextKey defined type used for context's key
type ContextKey string
// ContextDBName db name used for context
var ContextDBName ContextKey = "ContextDB"
// HTMLSanitizer html sanitizer to avoid XSS
var HTMLSanitizer = bluemonday.UGCPolicy()
func init() {
HTMLSanitizer.AllowStandardAttributes()
if path := os.Getenv("WEB_ROOT"); path != "" {
AppRoot = path
}
}
// GOPATH return GOPATH from env
func GOPATH() []string {
paths := strings.Split(os.Getenv("GOPATH"), string(os.PathListSeparator))
if len(paths) == 0 {
fmt.Println("GOPATH doesn't exist")
}
return paths
}
// GetDBFromRequest get database from request
var GetDBFromRequest = func(req *http.Request) *gorm.DB {
db := req.Context().Value(ContextDBName)
if tx, ok := db.(*gorm.DB); ok {
return tx
}
return nil
}
// HumanizeString Humanize separates string based on capitalizd letters
// e.g. "OrderItem" -> "Order Item"
func HumanizeString(str string) string {
var human []rune
for i, l := range str {
if i > 0 && isUppercase(byte(l)) {
if (!isUppercase(str[i-1]) && str[i-1] != ' ') || (i+1 < len(str) && !isUppercase(str[i+1]) && str[i+1] != ' ' && str[i-1] != ' ') {
human = append(human, rune(' '))
}
}
human = append(human, l)
}
return strings.Title(string(human))
}
// FirstCharToLow e.g. "Order" -> "order"
func FirstCharToLow(str string) string {
b := bytes.Buffer{}
for i, l := range str {
if i == 0 && ('A' <= l && l <= 'Z') {
b.WriteByte(byte(l) + 32)
} else {
b.WriteByte(byte(l))
}
}
return b.String()
}
func isUppercase(char byte) bool {
return 'A' <= char && char <= 'Z'
}
var asicsiiRegexp = regexp.MustCompile("^(\\w|\\s|-|!)*$")
// ToParamString replaces spaces and separates words (by uppercase letters) with
// underscores in a string, also downcase it
// e.g. ToParamString -> to_param_string, To ParamString -> to_param_string
func ToParamString(str string) string {
if asicsiiRegexp.MatchString(str) {
return gorm.ToDBName(strings.Replace(str, " ", "_", -1))
}
return slug.Make(str)
}
// PatchURL updates the query part of the request url.
// PatchURL("google.com","key","value") => "google.com?key=value"
func PatchURL(originalURL string, params ...interface{}) (patchedURL string, err error) {
url, err := url.Parse(originalURL)
if err != nil {
return
}
query := url.Query()
for i := 0; i < len(params)/2; i++ {
// Check if params is key&value pair
key := fmt.Sprintf("%v", params[i*2])
value := fmt.Sprintf("%v", params[i*2+1])
if value == "" {
query.Del(key)
} else {
query.Set(key, value)
}
}
url.RawQuery = query.Encode()
patchedURL = url.String()
return
}
// JoinURL updates the path part of the request url.
// JoinURL("google.com", "admin") => "google.com/admin"
// JoinURL("google.com?q=keyword", "admin") => "google.com/admin?q=keyword"
func JoinURL(originalURL string, paths ...interface{}) (joinedURL string, err error) {
u, err := url.Parse(originalURL)
if err != nil {
return
}
var urlPaths = []string{u.Path}
for _, p := range paths {
urlPaths = append(urlPaths, fmt.Sprint(p))
}
if strings.HasSuffix(strings.Join(urlPaths, ""), "/") {
u.Path = path.Join(urlPaths...) + "/"
} else {
u.Path = path.Join(urlPaths...)
}
joinedURL = u.String()
return
}
// SetCookie set cookie for context
//func SetCookie(cookie http.Cookie, context *Context) {
// cookie.HttpOnly = true
//
// // set https cookie
// if context.Request != nil && context.Request.URL.Scheme == "https" {
// cookie.Secure = true
// }
//
// // set default path
// if cookie.Path == "" {
// cookie.Path = "/"
// }
//
// http.SetCookie(context.Writer, &cookie)
//}
// Stringify stringify any data, if it is a struct, will try to use its Name, Title, Code field, else will use its primary key
func Stringify(object interface{}) string {
if obj, ok := object.(interface {
Stringify() string
}); ok {
return obj.Stringify()
}
scope := gorm.Scope{Value: object}
for _, column := range []string{"Name", "Title", "Code"} {
if field, ok := scope.FieldByName(column); ok {
if field.Field.IsValid() {
result := field.Field.Interface()
if valuer, ok := result.(driver.Valuer); ok {
if result, err := valuer.Value(); err == nil {
return fmt.Sprint(result)
}
}
return fmt.Sprint(result)
}
}
}
if scope.PrimaryField() != nil {
if scope.PrimaryKeyZero() {
return ""
}
return fmt.Sprintf("%v#%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue())
}
return fmt.Sprint(reflect.Indirect(reflect.ValueOf(object)).Interface())
}
// ModelType get value's model type
func ModelType(value interface{}) reflect.Type {
reflectType := reflect.Indirect(reflect.ValueOf(value)).Type()
for reflectType.Kind() == reflect.Ptr || reflectType.Kind() == reflect.Slice {
reflectType = reflectType.Elem()
}
return reflectType
}
// ParseTagOption parse tag options to hash
func ParseTagOption(str string) map[string]string {
tags := strings.Split(str, ";")
setting := map[string]string{}
for _, value := range tags {
v := strings.Split(value, ":")
k := strings.TrimSpace(strings.ToUpper(v[0]))
if len(v) == 2 {
setting[k] = v[1]
} else {
setting[k] = k
}
}
return setting
}
// ExitWithMsg debug error messages and print stack
func ExitWithMsg(msg interface{}, value ...interface{}) {
fmt.Printf("\n"+filenameWithLineNum()+"\n"+fmt.Sprint(msg)+"\n", value...)
debug.PrintStack()
}
// FileServer file server that disabled file listing
func FileServer(dir http.Dir) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := path.Join(string(dir), r.URL.Path)
if f, err := os.Stat(p); err == nil && !f.IsDir() {
http.ServeFile(w, r, p)
return
}
http.NotFound(w, r)
})
}
func filenameWithLineNum() string {
var total = 10
var results []string
for i := 2; i < 15; i++ {
if _, file, line, ok := runtime.Caller(i); ok {
total--
results = append(results[:0],
append(
[]string{fmt.Sprintf("%v:%v", strings.TrimPrefix(file, os.Getenv("GOPATH")+"src/"), line)},
results[0:]...)...)
if total == 0 {
return strings.Join(results, "\n")
}
}
}
return ""
}
// GetLocale get locale from request, cookie, after get the locale, will write the locale to the cookie if possible
// Overwrite the default logic with
// utils.GetLocale = func(context *qor.Context) string {
// // ....
// }
//var GetLocale = func(context *Context) string {
// if locale := context.Request.Header.Get("Locale"); locale != "" {
// return locale
// }
//
// if locale := context.Request.URL.Query().Get("locale"); locale != "" {
// if context.Writer != nil {
// context.Request.Header.Set("Locale", locale)
// SetCookie(http.Cookie{Name: "locale", Value: locale, Expires: time.Now().AddDate(1, 0, 0)}, context)
// }
// return locale
// }
//
// if locale, err := context.Request.Cookie("locale"); err == nil {
// return locale.Value
// }
//
// return ""
//}
// ParseTime parse time from string
// Overwrite the default logic with
// utils.ParseTime = func(timeStr string, context *qor.Context) (time.Time, error) {
// // ....
// }
var ParseTime = func(timeStr string, context *Context) (time.Time, error) {
return now.Parse(timeStr)
}
// FormatTime format time to string
// Overwrite the default logic with
// utils.FormatTime = func(time time.Time, format string, context *qor.Context) string {
// // ....
// }
var FormatTime = func(date time.Time, format string, context *Context) string {
return date.Format(format)
}
var replaceIdxRegexp = regexp.MustCompile(`\[\d+\]`)
// SortFormKeys sort form keys
func SortFormKeys(strs []string) {
sort.Slice(strs, func(i, j int) bool { // true for first
str1 := strs[i]
str2 := strs[j]
matched1 := replaceIdxRegexp.FindAllStringIndex(str1, -1)
matched2 := replaceIdxRegexp.FindAllStringIndex(str2, -1)
for x := 0; x < len(matched1); x++ {
prefix1 := str1[:matched1[x][0]]
prefix2 := str2
if len(matched2) >= x+1 {
prefix2 = str2[:matched2[x][0]]
}
if prefix1 != prefix2 {
return strings.Compare(prefix1, prefix2) < 0
}
if len(matched2) < x+1 {
return false
}
number1 := str1[matched1[x][0]:matched1[x][1]]
number2 := str2[matched2[x][0]:matched2[x][1]]
if number1 != number2 {
if len(number1) != len(number2) {
return len(number1) < len(number2)
}
return strings.Compare(number1, number2) < 0
}
}
return strings.Compare(str1, str2) < 0
})
}
// GetAbsURL get absolute URL from request, refer: https://stackoverflow.com/questions/6899069/why-are-request-url-host-and-scheme-blank-in-the-development-server
func GetAbsURL(req *http.Request) url.URL {
var result url.URL
if req.URL.IsAbs() {
return *req.URL
}
if domain := req.Header.Get("Origin"); domain != "" {
parseResult, _ := url.Parse(domain)
result = *parseResult
}
result.Parse(req.RequestURI)
return result
}
// Indirect returns last value that v points to
func Indirect(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
return v
}
// SliceUniq removes duplicate values in given slice
func SliceUniq(s []string) []string {
for i := 0; i < len(s); i++ {
for i2 := i + 1; i2 < len(s); i2++ {
if s[i] == s[i2] {
// delete
s = append(s[:i2], s[i2+1:]...)
i2--
}
}
}
return s
}
// DataType is judge data type
func DataType(t string) string {
switch t {
case "uint":
return "integer"
case "uint8":
return "integer"
case "uint16":
return "integer"
case "uint32":
return "integer"
case "uint64":
return "integer"
case "int":
return "integer"
case "int8":
return "integer"
case "int16":
return "integer"
case "int32":
return "integer"
case "int64":
return "integer"
case "float32":
return "float"
case "float64":
return "float"
default:
return t
}
}
// GetStructTagJSON is return tag
func GetStructTagJSON(f *gorm.Field) string {
if string(f.Tag.Get("json")) == "" {
return f.Name
}
return string(f.Tag.Get("json"))
}