-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
98 lines (91 loc) · 1.93 KB
/
utils.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
package litmus
import (
"strconv"
"strings"
"time"
"github.com/RoaringBitmap/roaring"
)
const (
layoutISO = "2006-01-02 15:04 -0700"
)
func ParseDate(dateTimeStr string) (time.Time, error) {
return time.Parse(layoutISO, dateTimeStr)
}
// ToArrayMap converts string/array into ArrayMap
func ToArrayMap(value interface{}) (map[interface{}]uint8, error) {
// data := reflect.ValueOf(value)
dataMap := make(map[interface{}]uint8)
switch value.(type) {
case string:
data := value.(string)
dataA := strings.Split(data, ",")
for _, d := range dataA {
d = strings.Trim(d, " ")
dataMap[d] = 1
}
case []string:
data := value.([]string)
for _, d := range data {
dataMap[d] = 1
}
case []int:
data := value.([]int)
for _, d := range data {
dataMap[int64(d)] = 1
}
case []int8:
data := value.([]int8)
for _, d := range data {
dataMap[int64(d)] = 1
}
case []int16:
data := value.([]int16)
for _, d := range data {
dataMap[int64(d)] = 1
}
case []int32:
data := value.([]int32)
for _, d := range data {
dataMap[int64(d)] = 1
}
case []int64:
data := value.([]int64)
for _, d := range data {
dataMap[int64(d)] = 1
}
}
return dataMap, nil
}
// ToRoarBitMap converts string into Bitmap
func ToRoarBitMap(matchStr string) (*roaring.Bitmap, error) {
rangeBitmap := roaring.NewBitmap()
if matchStr == "*" {
return rangeBitmap, nil
}
splits := strings.Split(matchStr, ",")
var err error
for _, aSplit := range splits {
ranges := strings.Split(aSplit, "-")
if len(ranges) == 1 {
hr, err := strconv.ParseUint(ranges[0], 10, 32)
if err != nil {
break
}
rangeBitmap.Add(uint32(hr))
} else {
end, err := strconv.Atoi(ranges[1])
if err != nil {
break
}
start, err := strconv.Atoi(ranges[0])
if err != nil {
break
}
if start > end {
start, end = end, start
}
rangeBitmap.AddRange(uint64(start), uint64(end+1))
}
}
return rangeBitmap, err
}