-
Notifications
You must be signed in to change notification settings - Fork 5
/
GenerateHaystackFilterVisitor.ts
98 lines (82 loc) · 1.85 KB
/
GenerateHaystackFilterVisitor.ts
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
/*
* Copyright (c) 2019, J2 Innovations. All Rights Reserved
*/
import {
Visitor,
CondOrNode,
CondAndNode,
ParensNode,
HasNode,
MissingNode,
CmpNode,
IsANode,
RelationshipNode,
WildcardEqualsNode,
} from './Node'
/**
* Generates a Haystack Filter String.
*/
export class GenerateHaystackFilterVisitor implements Visitor {
protected $filter = ''
public get filter(): string {
return this.$filter
}
public visitCondOr(node: CondOrNode): void {
const condAnds = node.condAnds
for (let i = 0; i < condAnds.length; ++i) {
if (i > 0) {
this.append('or')
}
condAnds[i].accept(this)
}
}
public visitCondAnd(node: CondAndNode): void {
const terms = node.terms
for (let i = 0; i < terms.length; ++i) {
if (i > 0) {
this.append('and')
}
terms[i].accept(this)
}
}
public visitParens(node: ParensNode): void {
this.append('(')
node.acceptChildNodes(this)
this.$filter += ')'
}
public visitHas(node: HasNode): void {
this.append(node.path.toFilter())
}
public visitMissing(node: MissingNode): void {
this.append('not')
this.append(node.path.toFilter())
}
public visitCmp(node: CmpNode): void {
this.append(node.path.toFilter())
this.append(node.cmpOp.text)
this.append(node.val.toFilter())
}
public visitIsA(node: IsANode): void {
this.append(node.val.toFilter())
}
public visitRelationship(node: RelationshipNode): void {
this.append(node.rel.toFilter())
if (node.ref) {
this.append(node.ref.toFilter())
}
}
public visitWildcardEquals(node: WildcardEqualsNode): void {
this.append(node.id.toFilter())
this.append('*==')
this.append(node.ref.toFilter())
}
protected append(str: string): void {
if (this.$filter && this.lastChar !== '(') {
this.$filter += ' '
}
this.$filter += str
}
protected get lastChar(): string {
return this.$filter[this.$filter.length - 1]
}
}