-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
55 lines (50 loc) · 1.26 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
package cfg
import (
"os"
"reflect"
"strings"
"time"
)
// stringSlice converts a Go slice represented as a string
// into an actual slice. The enclosing square brackets
// are not necessary.
// fields should be separated by a comma.
//
// "[1,2,3]" ---> []string{"1", "2", "3"}
// " foo , bar" ---> []string{" foo ", " bar"}
func stringSlice(s string) []string {
s = strings.TrimSuffix(strings.TrimPrefix(s, "["), "]")
return strings.Split(s, ",")
}
// fileExists returns true if the file exists and is not a
// directory.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// isStructPtr reports whether i is a pointer to a struct.
func isStructPtr(i interface{}) bool {
v := reflect.ValueOf(i)
return v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct
}
// isZero reports whether v is its zero value for its type.
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
return v.IsNil()
case reflect.Slice, reflect.Array:
return v.Len() == 0
case reflect.Struct:
if t, ok := v.Interface().(time.Time); ok {
return t.IsZero()
}
return false
case reflect.Invalid:
return true
default:
return v.IsZero()
}
}