-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
133 lines (121 loc) · 3.3 KB
/
util.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
package mars
import (
"bytes"
"io"
"io/ioutil"
"net/url"
"os"
"reflect"
"regexp"
"strings"
)
// Add some more methods to the default Template.
type ExecutableTemplate interface {
Execute(io.Writer, interface{}) error
}
// Execute a template and returns the result as a string.
func ExecuteTemplate(tmpl ExecutableTemplate, data interface{}) string {
var b bytes.Buffer
tmpl.Execute(&b, data)
return b.String()
}
// Reads the lines of the given file. Panics in the case of error.
func MustReadLines(filename string) []string {
r, err := ReadLines(filename)
if err != nil {
panic(err)
}
return r
}
// Reads the lines of the given file. Panics in the case of error.
func ReadLines(filename string) ([]string, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return strings.Split(string(bytes), "\n"), nil
}
func ContainsString(list []string, target string) bool {
for _, el := range list {
if el == target {
return true
}
}
return false
}
// Return the reflect.Method, given a Receiver type and Func value.
func FindMethod(recvType reflect.Type, funcVal reflect.Value) *reflect.Method {
// It is not possible to get the name of the method from the Func.
// Instead, compare it to each method of the Controller.
for i := 0; i < recvType.NumMethod(); i++ {
method := recvType.Method(i)
if method.Func.Pointer() == funcVal.Pointer() {
return &method
}
}
return nil
}
var (
cookieKeyValueParser = regexp.MustCompile("\x00([^:]*):([^\x00]*)\x00")
)
// Takes the raw (escaped) cookie value and parses out key values.
func ParseKeyValueCookie(val string, cb func(key, val string)) {
val, _ = url.QueryUnescape(val)
if matches := cookieKeyValueParser.FindAllStringSubmatch(val, -1); matches != nil {
for _, match := range matches {
cb(match[1], match[2])
}
}
}
// DirExists returns true if the given path exists and is a directory.
func DirExists(filename string) bool {
fileInfo, err := os.Stat(filename)
return err == nil && fileInfo.IsDir()
}
func FirstNonEmpty(strs ...string) string {
for _, str := range strs {
if len(str) > 0 {
return str
}
}
return ""
}
// Equal is a helper for comparing value equality, following these rules:
// - Values with equivalent types are compared with reflect.DeepEqual
// - int, uint, and float values are compared without regard to the type width.
// for example, Equal(int32(5), int64(5)) == true
// - strings and byte slices are converted to strings before comparison.
// - else, return false.
func Equal(a, b interface{}) bool {
if reflect.TypeOf(a) == reflect.TypeOf(b) {
return reflect.DeepEqual(a, b)
}
switch a.(type) {
case int, int8, int16, int32, int64:
switch b.(type) {
case int, int8, int16, int32, int64:
return reflect.ValueOf(a).Int() == reflect.ValueOf(b).Int()
}
case uint, uint8, uint16, uint32, uint64:
switch b.(type) {
case uint, uint8, uint16, uint32, uint64:
return reflect.ValueOf(a).Uint() == reflect.ValueOf(b).Uint()
}
case float32, float64:
switch b.(type) {
case float32, float64:
return reflect.ValueOf(a).Float() == reflect.ValueOf(b).Float()
}
case string:
switch b.(type) {
case []byte:
return a.(string) == string(b.([]byte))
}
case []byte:
switch b.(type) {
case string:
return b.(string) == string(a.([]byte))
}
}
return false
}