-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.go
235 lines (210 loc) · 4.29 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
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
package utils
import (
"fmt"
"math/rand"
"reflect"
"sort"
"strings"
"time"
"github.com/pkg/errors"
)
const uidBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const lettersBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func GenerateUID(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = uidBytes[rand.Intn(len(uidBytes))]
}
return string(b)
}
func GenerateName(n int) string {
b := make([]byte, n)
b[0] = lettersBytes[rand.Intn(len(lettersBytes))]
for i := range b[1:] {
b[i+1] = uidBytes[rand.Intn(len(uidBytes))]
}
return string(b)
}
// panic if err != nil
func Must(err error) {
if err != nil {
panic(err)
}
}
// Joins two errors to one.
func JoinErrors(one error, two error) error {
if one == nil && two == nil {
return nil
}
if one != nil && two != nil {
return errors.Wrapf(two, "%s\nPrev Error", one.Error())
}
if one != nil {
return one
}
return two
}
func JoinErrorsWrap(one error, two error, twoErrorMessage string) error {
if two != nil {
if twoErrorMessage == "" {
return JoinErrors(one, two)
} else {
return JoinErrors(one, errors.Wrap(two, twoErrorMessage))
}
}
return one
}
// Like math.Min for int
func Min(x, y int) int {
if x < y {
return x
}
return y
}
// Like math.Max for int
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}
// Like math.Min for int
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}
func MinMax(x, y int) (int, int) {
if x < y {
return x, y
}
return y, x
}
// 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 && s.Kind() != reflect.Array {
panic(fmt.Sprintf("InterfaceSlice() given a non-slice type: %s", s.Kind()))
}
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
}
func IntersectSortedStringSlices(first []string, second []string) []string {
ret := []string{}
i := 0
j := 0
for i < len(first) && j < len(second) {
cmp := strings.Compare(first[i], second[j])
if cmp == 0 {
ret = append(ret, first[i])
i++
} else if cmp < 0 {
i++
} else {
j++
}
}
return ret
}
func StringMapOrderedKeys(m interface{}) []string {
mValue := reflect.ValueOf(m)
if mValue.Kind() != reflect.Map {
panic("m is not map")
}
if mValue.Type().Key().Kind() != reflect.String {
panic("m key is not string")
}
keys := make([]string, 0, len(mValue.MapKeys()))
for _, k := range mValue.MapKeys() {
keys = append(keys, k.Interface().(string))
}
sort.Strings(keys)
return keys
}
func SumAndMax(values []int) (int, int) {
var sum int = 0
var max int = 0
for _, val := range values {
if val > max {
max = val
}
sum += val
}
return sum, max
}
func Contains(list []interface{}, elem interface{}) bool {
for _, t := range list {
if t == elem {
return true
}
}
return false
}