-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncs.go
51 lines (41 loc) · 807 Bytes
/
funcs.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
package leopards
import (
"encoding/binary"
"reflect"
)
// Bit2Uint SQL bit to int
func Bit2Uint(v []byte) uint64 {
x := make([]byte, 8)
j := 0
for i := 8 - len(v); i < 8; i++ {
x[i] = v[j]
j++
}
return binary.BigEndian.Uint64(x)
}
// Key fetch value with type from map data
func Key[K comparable](data any, key any) K {
v := reflect.ValueOf(data)
var t K
if v.Kind() == reflect.Map {
x := v.MapIndex(reflect.ValueOf(key)).Interface()
if vt, ok := x.(K); ok {
return vt
}
}
return t
}
// Pick fetch value with type from slice
func Pick[K comparable](data any, index int) K {
v := reflect.ValueOf(data)
var t K
if v.Kind() == reflect.Slice {
if index < v.Len()-1 {
vt := v.Index(index).Interface()
if vv, ok := vt.(K); ok {
return vv
}
}
}
return t
}