forked from sodafoundation/strato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
41 lines (36 loc) · 975 Bytes
/
helper.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
package helper
import (
"math/rand"
"reflect"
)
// mimic `?:` operator
// Need type assertion to convert output to expected type
func Ternary(IF bool, THEN interface{}, ELSE interface{}) interface{} {
if IF {
return THEN
} else {
return ELSE
}
}
// Get keys of a map, i.e.
// map[string]interface{} -> []string
// Note that some type checks are omitted for efficiency, you need to ensure them yourself,
// otherwise your program should panic
func Keys(v interface{}) []string {
rv := reflect.ValueOf(v)
result := make([]string, 0, rv.Len())
for _, kv := range rv.MapKeys() {
result = append(result, kv.String())
}
return result
}
// Static alphaNumeric table used for generating unique request ids
var alphaNumericTable = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
func GenerateRandomId() []byte {
alpha := make([]byte, 16, 16)
for i := 0; i < 16; i++ {
n := rand.Intn(len(alphaNumericTable))
alpha[i] = alphaNumericTable[n]
}
return alpha
}