-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.go
113 lines (100 loc) · 1.99 KB
/
general.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
package utils
import (
"fmt"
"reflect"
"strings"
"github.com/pkg/errors"
)
// panic if err != nil
func Must(err error) {
if err != nil {
panic(err)
}
}
// Like math.Min for int
func Min(x, y int) int {
if x < y {
return x
}
return y
}
// Like math.Min for int
func Max(x, y uint16) uint16 {
if x > y {
return x
}
return y
}
// true if every string in given slice is empty
func IsEmpty(s []string) bool {
for _, x := range s {
if x != "" {
return false
}
}
return true
}
func ConvertArgsInt64(args []int64) []interface{} {
c := make([]interface{}, len(args))
for i := range args {
c[i] = args[i]
}
return c
}
func ConvertArgsString(args []string) []interface{} {
c := make([]interface{}, len(args))
for i := range args {
c[i] = args[i]
}
return c
}
func Int64InSlice(i int64, s []int64) bool {
for _, v := range s {
if v == i {
return true
}
}
return false
}
func is(slice interface{}) []interface{} {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("InterfaceSlice() given a non-slice type")
}
ret := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
ret[i] = s.Index(i).Interface()
}
return ret
}
func Pprint(l interface{}) string {
var s []string
for _, i := range is(l) {
s = append(s, fmt.Sprintf("%+v", i))
}
return strings.Join(s, "\n\t")
}
func Join(l []interface{}, separator string) string {
var ret []string
for _, v := range l {
ret = append(ret, fmt.Sprintf("%+v", v))
}
return strings.Join(ret, separator)
}
func PrintMap(m interface{}) (string, error) {
mValue := reflect.ValueOf(m)
if mValue.Kind() != reflect.Map {
return "", errors.New("Input is not map.")
}
var values []string
for _, k := range mValue.MapKeys() {
v := mValue.MapIndex(k)
vValue := reflect.ValueOf(v)
if vValue.Kind() == reflect.Slice {
values = append(values, fmt.Sprintf("%+v:[%s]", k, Join(is(v), ",")))
} else {
values = append(values, fmt.Sprintf("%+v:%+v", k, v))
}
}
return strings.Join(values, ","), nil
}