This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
130 lines (116 loc) · 2.96 KB
/
parse.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"strings"
"unicode/utf8"
)
type wordOrJSONScanner struct {
inJSON bool
}
func (s *wordOrJSONScanner) split(data []byte, atEOF bool) (advance int, token []byte, err error) {
start := 0
if !s.inJSON {
// Skip leading spaces.
for width := 0; start < len(data); start += width {
var r rune
r, width = utf8.DecodeRune(data[start:])
if !isSpace(r) {
break
}
}
}
// Scan until space, marking end of word.
for width, i := 0, start; i < len(data); i += width {
var r rune
r, width = utf8.DecodeRune(data[i:])
if r == '{' && i == 0 && !s.inJSON {
// The first rune of the word is a {, so it's the start of a JSON
// object.
s.inJSON = true
continue
}
if !s.inJSON && isSpace(r) {
return i + width, data[start:i], nil
}
}
// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
if atEOF && len(data) > start {
return len(data), data[start:], nil
}
// Request more data.
return start, nil, nil
}
func parseCommand(line string) (command, error) {
var fields []string
var splitter wordOrJSONScanner
s := bufio.NewScanner(bytes.NewReader([]byte(line)))
s.Split(splitter.split)
for s.Scan() {
fields = append(fields, s.Text())
}
if err := s.Err(); err != nil {
return command{}, err
}
if len(fields) < 2 {
return command{}, errors.New("incomplete command")
}
// The command is the first two parts joined with a dot.
cmd := command{
Method: fields[0] + "." + fields[1],
}
// Look for key=val,key=val sequences among params and make them objects.
// Not stuff that starts with "(" though, because that might be an LDAP
// query expression.
for _, param := range fields[2:] {
if strings.HasPrefix(param, "{") {
var obj map[string]interface{}
err := json.Unmarshal([]byte(param), &obj)
if err != nil {
return command{}, err
}
cmd.Params = append(cmd.Params, obj)
} else if strings.Contains(param, "=") && !strings.HasPrefix(param, "(") {
parts := strings.Split(param, ",")
obj := map[string]string{}
for _, part := range parts {
if !strings.Contains(part, "=") {
obj[part] = ""
continue
}
kv := strings.SplitN(part, "=", 2)
obj[kv[0]] = kv[1]
}
cmd.Params = append(cmd.Params, obj)
} else {
cmd.Params = append(cmd.Params, param)
}
}
return cmd, nil
}
// isSpace reports whether the character is a Unicode white space character.
// We avoid dependency on the unicode package, but check validity of the implementation
// in the tests.
func isSpace(r rune) bool {
if r <= '\u00FF' {
// Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
switch r {
case ' ', '\t', '\n', '\v', '\f', '\r':
return true
case '\u0085', '\u00A0':
return true
}
return false
}
// High-valued ones.
if '\u2000' <= r && r <= '\u200a' {
return true
}
switch r {
case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
return true
}
return false
}