-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcustoms.go
61 lines (53 loc) · 1.23 KB
/
customs.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
package shared
import (
"fmt"
"strings"
)
type RegexFlag struct {
Regexes map[string]string
}
func (r *RegexFlag) String() string {
return fmt.Sprintf("%v", r.Regexes)
}
func (r *RegexFlag) Set(value string) error {
var err error
r.Regexes, err = ParseRegexes(value)
if err != nil {
return err
}
return nil
}
func (r *RegexFlag) Type() string {
return "regex"
}
func (r *RegexFlag) Value() map[string]string {
return r.Regexes
}
func isBetween(s string, a string, b string, index int) bool {
return strings.Count(s[:index], a) == strings.Count(s[index:], b) && strings.Count(s[:index], b) != 0
}
func ParseRegexes(stream string) (map[string]string, error) {
result := make(map[string]string)
var key, value string
prev := 0
for i, c := range stream {
if c == ',' && !isBetween(stream, "{", "}", i) {
keyValue := strings.Split(stream[prev:i], "=")
if len(keyValue) != 2 {
return nil, fmt.Errorf("invalid regexes format")
}
key = keyValue[0]
value = keyValue[1]
result[key] = value
prev = i + 1
}
}
keyValue := strings.Split(stream[prev:], "=")
if len(keyValue) != 2 {
return nil, fmt.Errorf("invalid regexes format")
}
key = keyValue[0]
value = keyValue[1]
result[key] = value
return result, nil
}