-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery_parser.go
197 lines (182 loc) · 5.26 KB
/
query_parser.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"net/http"
"regexp"
"strings"
log "github.com/Sirupsen/logrus"
)
var queryRegexp = regexp.MustCompile(`^([\w]+)\((.*)\)`)
type queryParseError struct {
StatusCode int
Message string
}
func (qpe queryParseError) Error() string {
return qpe.Message
}
func parseQuery(queryStr string) (readFilters, error) {
manager := GetQPManager()
keys, parsers := manager.GetActiveParsers()
for idx, parser := range parsers {
if parser.CanParseString(queryStr) {
log.Debugf("parser.CanParseString(%s) true for %s\n", queryStr, keys[idx])
rf, err := parser.ParseString(queryStr)
if err != nil {
log.Warnf("Parser.ParseString(%s) failed with:%s", queryStr, err.Error())
return rf, queryParseError{
StatusCode: http.StatusBadRequest,
Message: err.Error(),
}
}
return rf, nil
}
log.Debugf("parser.CanParseString(%s) false for %s\n", queryStr, keys[idx])
}
log.Warnf("parseQuery - Failed to find parser for:%s. Tried %s\n", queryStr, keys)
return newReadFilters(), queryParseError{
StatusCode: http.StatusBadRequest,
Message: "No QueryFormats can parse input",
}
}
//
// func parseQuery(queryStr string) (readFilters, error) {
// query := newReadFilters()
//
// if strings.HasPrefix(queryStr, "AND") || strings.HasPrefix(queryStr, "and") ||
// strings.HasPrefix(queryStr, "OR") || strings.HasPrefix(queryStr, "or") {
// // Format: and([key, comparitor, value],[key, comparitor, value] ...)
// // or([key, comparitor, value],[key, comparitor, value] ...)
// log.Debugf("Starts with AND/OR")
// matches := queryRegexp.FindStringSubmatch(queryStr)
// if matches != nil {
// if len(matches) != 3 {
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: "Invalid query format",
// }
// }
//
// var filtersStr string
// if strings.HasPrefix(matches[2], "[[") {
// filtersStr = matches[2]
// } else if strings.HasPrefix(matches[2], "[") {
// filtersStr = "[" + matches[2] + "]"
// } else {
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: "Invalid query filter format",
// }
// }
//
// var filters []interface{}
// err := json.Unmarshal([]byte(filtersStr), &filters)
// if err != nil {
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: err.Error(),
// }
// }
//
// queryData := map[string]interface{}{
// "logical_operator": matches[1],
// "conditions": filters,
// }
//
// err = mapToReadFilters(queryData, &query)
//
// return query, err
//
// }
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: "Invalid query format",
// }
//
// } else if strings.HasPrefix(queryStr, "{") {
// // Format: {"logical_operator": "and", "conditions": [[key, comparitor, value], ...]}
// log.Debugf("Is JSON hash")
// var queryData map[string]interface{}
// err := json.Unmarshal([]byte(queryStr), &queryData)
// if err != nil {
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: err.Error(),
// }
// }
//
// if _, ok := queryData["logical_operator"]; !ok {
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: "Missing key: 'logical_operator'",
// }
// }
//
// if _, ok := queryData["conditions"]; !ok {
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: "Missing key: 'conditions'",
// }
// }
// err = mapToReadFilters(queryData, &query)
//
// return query, err
//
// } else if strings.HasPrefix(queryStr, "[[") {
// // Format: [[key, comparitor, value], ...]
// log.Debugf("Is JSON array of arrays")
// var filters []interface{}
// err := json.Unmarshal([]byte(queryStr), &filters)
// if err != nil {
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: err.Error(),
// }
// }
// queryData := map[string]interface{}{
// "logical_operator": "and",
// "conditions": filters,
// }
// err = mapToReadFilters(queryData, &query)
// return query, err
//
// }
//
// return query, queryParseError{
// StatusCode: http.StatusBadRequest,
// Message: "Invalid query format",
// }
//
// }
//
func mapToReadFilters(queryMap map[string]interface{}, query *readFilters) error {
op, ok := queryMap["logical_operator"]
if !ok {
return queryParseError{
StatusCode: http.StatusInternalServerError,
Message: "Missing key: 'logical_operator'",
}
}
conditions, ok := queryMap["conditions"]
if !ok {
return queryParseError{
StatusCode: http.StatusInternalServerError,
Message: "Missing key: 'conditions'",
}
}
query.LogicalOperator = strings.ToLower(op.(string))
log.Debugf("Conditions: %v", conditions)
for _, condition := range conditions.([]interface{}) {
filter := condition.([]interface{})
log.Debugf("Filter: %v", filter)
cond := arrayToQueryCondition(filter)
log.Debugf("Condition: %v", cond)
query.AddCondition(cond)
}
return nil
}
func arrayToQueryCondition(filter []interface{}) queryCondition {
return newQueryCondition(
filter[0].(string), // name
filter[1].(string), // relation
filter[2], // values
)
}