-
Notifications
You must be signed in to change notification settings - Fork 7
/
query.go
91 lines (76 loc) · 2.04 KB
/
query.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
package fabric
import (
"fmt"
)
// Query represents a query to identify one or more triples.
type Query struct {
Source Clause `json:"source,omitempty"`
Predicate Clause `json:"predicate,omitempty"`
Target Clause `json:"target,omitempty"`
Weight Clause `json:"weight,omitempty"`
Limit int `json:"limit,omitempty"`
}
// IsAny returns true if all clauses are any clauses.
func (q Query) IsAny() bool {
return (q.Source.IsAny() && q.Predicate.IsAny() &&
q.Target.IsAny() && q.Weight.IsAny())
}
// Map returns a map version of the query with all the any clauses removed.
func (q Query) Map() map[string]Clause {
m := map[string]Clause{}
if !q.Source.IsAny() {
m["source"] = q.Source
}
if !q.Predicate.IsAny() {
m["predicate"] = q.Predicate
}
if !q.Target.IsAny() {
m["target"] = q.Target
}
if !q.Weight.IsAny() {
m["weight"] = q.Weight
}
return m
}
func (q *Query) normalize() {
q.Source.normalize()
q.Target.normalize()
q.Predicate.normalize()
q.Weight.normalize()
}
// Clause represents a query clause. Zero value of this struct will be used as
// 'Any' clause which matches any value.
type Clause struct {
// Type represents the operation that should be used. Examples include equal,
// gt, lt etc. Supported operations are dictated by store implementations.
Type string
// Value that should be used as the right operand for the operation.
Value string
}
// IsAny returns true if cl is a nil clause or both Op and Value are empty.
func (cl Clause) IsAny() bool {
return cl.Type == "" && cl.Value == ""
}
func (cl Clause) String() string {
return fmt.Sprintf("%s %s", cl.Type, cl.Value)
}
func (cl *Clause) normalize() {
normalized, found := knownTypes[cl.Type]
if found {
cl.Type = normalized
}
}
var knownTypes = map[string]string{
"equal": "eq",
"equals": "eq",
"==": "eq",
"=": "eq",
">": "gt",
"greater-than": "gt",
"<": "lt",
"lesser-than": "lt",
"<=": "lte",
">=": "gte",
"~": "like",
"~=": "like",
}