-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
59 lines (54 loc) · 1.14 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
package audit_log
import (
"net/http"
"reflect"
"regexp"
"strings"
)
func contains(array interface{}, val interface{}) (index int) {
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
{
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) {
index = i
return
}
}
}
}
return
}
func toSnakeCase(str string) string {
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
func removeRepeat(slc []string) []string {
result := []string{}
tempMap := map[string]byte{}
for _, e := range slc {
l := len(tempMap)
tempMap[e] = 0
if len(tempMap) != l {
result = append(result, e)
}
}
return result
}
func convIP(header http.Header) string {
xff, ok := header["X-Forwarded-For"]
if !ok {
return "127.0.0.1"
}
if len(xff) == 0 {
return "127.0.0.1"
}
ipArray := strings.Split(xff[0], ",")
ip := ipArray[0]
return ip
}