-
Notifications
You must be signed in to change notification settings - Fork 70
/
util.go
65 lines (50 loc) · 963 Bytes
/
util.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
package nebula_go
import (
"regexp"
"strconv"
)
func IndexOf(collection []string, element string) int {
for i, item := range collection {
if item == element {
return i
}
}
return -1
}
func parseTTL(s string) (string, uint, error) {
col, err := parseTTLCol(s)
if err != nil {
return "", 0, err
}
duration, err := parseTTLDuration(s)
if err != nil {
return "", 0, err
}
return col, duration, nil
}
func parseTTLCol(s string) (string, error) {
reg, err := regexp.Compile(`ttl_col = "(\w+)"`)
ss := reg.FindStringSubmatch(s)
if err != nil {
return "", err
}
if len(ss) == 2 {
return ss[1], nil
}
return "", nil
}
func parseTTLDuration(s string) (uint, error) {
reg, err := regexp.Compile(`ttl_duration = (\d+)`)
if err != nil {
return 0, err
}
ss := reg.FindStringSubmatch(s)
if len(ss) == 2 {
ttl, err := strconv.Atoi(ss[1])
if err != nil {
return 0, err
}
return uint(ttl), nil
}
return 0, nil
}