-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_keys.go
103 lines (89 loc) · 2.08 KB
/
parse_keys.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
99
100
101
102
103
package nvault
import (
"bufio"
"errors"
"fmt"
"regexp"
"strings"
)
// ParseKeys ...
func ParseKeys(clikey string) (paths []Path, err error) {
for _, key := range strings.Split(clikey, ",") {
path, err := parseKey(key)
if err != nil {
return nil, err
}
paths = append(paths, path)
}
if len(paths) == 0 {
return paths, errors.New("empty paths")
}
return paths, nil
}
// ScanType ...
type ScanType struct {
Regexp *regexp.Regexp
Type string
}
var regs = []ScanType{
{regexp.MustCompile(`^'(.*?)'`), "string"},
{regexp.MustCompile(`^"(.*?)"`), "string"},
{regexp.MustCompile(`^/(.*?)/`), "regexp"},
{regexp.MustCompile(`^\s+`), "space"},
{regexp.MustCompile(`^\[(\d*)\]`), "number"},
{regexp.MustCompile(`^\.`), "delimiter"},
{regexp.MustCompile(`^[^\.]+`), "string"},
}
func parseKey(key string) (Path, error) {
s := bufio.NewScanner(strings.NewReader(key))
s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
Start:
for _, st := range regs {
loc := st.Regexp.FindSubmatchIndex(data)
if len(loc) > 0 {
if len(loc) > 3 {
token = data[loc[2]:loc[3]]
} else {
token = data[loc[0]:loc[1]]
}
token = append([]byte(fmt.Sprintf("%s:", st.Type)), token...)
advance = advance + loc[1]
if st.Type == "delimiter" || st.Type == "space" {
padding := loc[1] - loc[0]
if len(data) >= padding {
data = data[padding:]
goto Start
}
break
}
return
}
}
if atEOF && len(data) > advance {
return len(data), data[advance:], nil
}
return advance, nil, nil
})
var fragments []string
for s.Scan() {
fragments = append(fragments, s.Text())
}
if err := s.Err(); err != nil {
return nil, fmt.Errorf("Invalid input: %s", err)
}
if len(fragments) == 0 {
return nil, errors.New("no path fragments")
}
var path Path
for _, fragment := range fragments {
f := strings.SplitN(fragment, ":", 2)
path = append(path, PathFragment{
Type: f[0],
Fragment: f[1]},
)
}
if path[0].Fragment != "$" {
return nil, errors.New("`$` must be at first")
}
return path, nil
}