-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.go
200 lines (186 loc) · 5.06 KB
/
translate.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
198
199
200
package esevaluator
import (
"fmt"
"strings"
)
// Leaf is a terminal node of a query
// All terminal query terms implement the Translate() method
type leaf interface {
Translate() Term
}
// encodeParam encodes a parameter from a query to its corresponding leaf type
func encodeParam(concept string, param map[string]interface{}) (map[string]interface{}, error) {
o := param["operator"].(string)
var cid string
if o == "set" {
cid = "_id"
} else {
cid = param["id"].(string)
}
var conceptPath string
if concept != "" && o != "set" {
conceptPath = concept + "." + cid
} else {
conceptPath = cid
}
v := param["value"]
var l leaf
switch o {
case "set":
l = &MemberTerm{conceptPath, v}
case "eq":
l = &EqualityTerm{conceptPath, v, false}
case "-eq":
l = &EqualityTerm{conceptPath, v, true}
case "undefined":
l = &DefinitionTerm{conceptPath, false}
case "defined":
l = &DefinitionTerm{conceptPath, true}
case "one":
s, ok := v.([]string)
if !ok {
return nil, fmt.Errorf("Cannot parse 'one' query value to string slice: %v", v)
}
l = &OneTerm{conceptPath, s}
case "match":
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Cannot parse 'match' query value to string: %v", v)
}
l = &MatchTerm{conceptPath, s}
case "query":
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Cannot parse 'query' query value to string: %v", v)
}
l = &QueryTerm{conceptPath, s}
case "gt":
l = &GreaterThanTerm{conceptPath, v, false}
case "gte":
l = &GreaterThanTerm{conceptPath, v, true}
case "lt":
l = &LessThanTerm{conceptPath, v, false}
case "lte":
l = &LessThanTerm{conceptPath, v, true}
case "range":
s, ok := v.([]interface{})
if !ok {
return nil, fmt.Errorf("Cannot parse 'range' query value to interface slice: %v", v)
} else if len(s) != 2 {
return nil, fmt.Errorf("'Range' query value cannot have length different than 2: %v", s)
}
l = &RangeTerm{conceptPath, s[1], s[0]}
case "empty":
l = &EmptinessTerm{conceptPath, true}
case "nonempty":
l = &EmptinessTerm{conceptPath, false}
case "member":
l = &MemberTerm{conceptPath, v}
case "subset":
s, ok := v.([]interface{})
if !ok {
return nil, fmt.Errorf("Cannot parse 'subset' query value to interface slice: %v", v)
}
l = &SubsetTerm{conceptPath, s}
default:
return nil, fmt.Errorf("Operator %s not found", o)
}
return l.Translate(), nil
}
func encodeConcept(t Term) (Term, error) {
concept, ok := t["concept"].(string)
if !ok {
return nil, fmt.Errorf("Unable to encode concept value to string: %v", t["concept"])
}
params, ok := t["params"].([]interface{})
if !ok {
return nil, fmt.Errorf("Unable to encode concept params to interface slice: %v", t["params"])
}
var must MustTerm
for _, p := range params {
paramMap, ok := p.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Unable to encode param to map: %v", p)
}
paramTerm := Term(paramMap)
encodedParam, err := encodeParam(concept, paramTerm)
if err != nil {
return nil, fmt.Errorf("Unable to encode parameter: %v", paramTerm)
}
must.AddParam(encodedParam)
}
filterStatement := Term{
"filter": must.Encode(),
}
if strings.Contains(concept, ".") {
return Nest(filterStatement, concept), nil
}
return filterStatement, nil
}
func encodeBranch(t Term) (Term, error) {
op, ok := t["operator"].(string)
if !ok {
return nil, fmt.Errorf("Cannot encode branch operator to string: %v", t["operator"])
}
terms, ok := t["terms"].([]interface{})
if !ok {
return nil, fmt.Errorf("Cannot encode branch terms to interface slice: %v", t["terms"])
}
var b BooleanStatement
if op == "or" {
b = &ShouldTerm{}
} else if op == "and" {
b = &MustTerm{}
} else {
return nil, fmt.Errorf("Unrecognized branching operator: %s", op)
}
for _, term := range terms {
tmap, ok := term.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Cannot encode term to map: %v", term)
}
tterm := Term(tmap)
ttype := tterm["type"].(string)
if ttype == "concept" {
c, err := encodeConcept(tterm)
if err != nil {
return nil, fmt.Errorf("Unable to encode concept: %v", c)
}
b.AddParam(c)
} else if ttype == "branch" {
c, err := encodeBranch(tterm)
if err != nil {
return nil, fmt.Errorf("Unable to encode branch: %v", c)
}
b.AddParam(c)
}
}
return b.Encode(), nil
}
// Translate encodes a query into its ES equivalent
func Translate(query Term) (Term, error) {
mapterm, ok := query["term"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Cannot parse query term: %v", mapterm)
}
term := Term(mapterm)
ttype, ok := term["type"].(string)
if !ok {
return nil, fmt.Errorf("Cannot parse term type to string, invalid json: %v", term["type"])
}
switch ttype {
case "concept":
t, err := encodeConcept(term)
if err != nil {
return nil, fmt.Errorf("Unable to encode concept term %v", term)
}
return Filter(t), nil
case "branch":
t, err := encodeBranch(term)
if err != nil {
return nil, fmt.Errorf("Unable to encode branch term %v", term)
}
return Filter(t), nil
}
return nil, fmt.Errorf("Unknown term type: %s", ttype)
}