-
Notifications
You must be signed in to change notification settings - Fork 5
/
GenerateHaystackFilterImpliedVisitor.ts
220 lines (183 loc) · 5.37 KB
/
GenerateHaystackFilterImpliedVisitor.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright (c) 2021, J2 Innovations. All Rights Reserved
*/
import { Kind } from '../core/Kind'
import { HList } from '../core/HList'
import { HSymbol } from '../core/HSymbol'
import { HNamespace } from '../core/HNamespace'
import { HDict } from '../core/HDict'
import { GenerateHaystackFilterVisitor } from './GenerateHaystackFilterVisitor'
import {
HasNode,
MissingNode,
CmpNode,
IsANode,
RelationshipNode,
WildcardEqualsNode,
} from './Node'
import { TokenPaths } from './TokenPaths'
import { valueIsKind } from '../core/HVal'
import { GenerateHaystackFilterV3Visitor } from './GenerateHaystackFilterV3Visitor'
import { HFilter } from './HFilter'
import { IMPLIED_BY } from '../core/util'
/**
* Generates a Haystack Filter String by processing any implied tags rules.
*
* For instance, a haystack filter that uses implied tags can query a database
* that contains records without the implied tags present.
*
* Warning: implied tags are currently considered experimental.
*/
export class GenerateHaystackFilterImpliedVisitor extends GenerateHaystackFilterVisitor {
/**
* The readonly namespace.
*/
readonly #namespace: HNamespace
/**
* The implied tags to look up.
*
* @param namespace The namespace used to query implied tags.
*/
public constructor(namespace: HNamespace) {
super()
this.#namespace = namespace
}
public visitHas(node: HasNode): void {
let filter = node.path.toFilter()
const paths = node.path.paths
for (let i = 0; i < paths.length; ++i) {
const impliedFilter = this.replaceImpliedTags(paths, i)
if (impliedFilter) {
filter = `(${filter} or ${impliedFilter})`
}
}
this.append(filter)
}
public visitMissing(node: MissingNode): void {
let filter = `not ${node.path.toFilter()}`
const paths = node.path.paths
for (let i = 0; i < paths.length; ++i) {
const impliedFilter = this.replaceImpliedTags(paths, i, 'not ')
if (impliedFilter) {
filter = `(${filter} or ${impliedFilter})`
}
}
this.append(filter)
}
public visitCmp(node: CmpNode): void {
const paths = node.path.paths
const valuePostfix = ` ${node.cmpOp.text} ${node.val.toFilter()}`
let filter = `${node.path.toFilter()}${valuePostfix}`
for (let i = 0; i < paths.length; ++i) {
const impliedFilter = this.replaceImpliedTags(
paths,
i,
'',
valuePostfix
)
if (impliedFilter) {
filter = `(${filter} or ${impliedFilter})`
}
}
this.append(filter)
}
public visitIsA(node: IsANode): void {
const filter = node.val.toFilter()
// Convert the is a query to a large `has` query using Haystack v4 to v3 transpilation.
const v3Node = HFilter.parse(filter)
const v3Visitor = new GenerateHaystackFilterV3Visitor(this.#namespace)
v3Node.accept(v3Visitor)
const hasFilter = v3Visitor.filter
// Now convert from the large `has` query into an implied filter check.
// using an instance of this filter.
const hasNode = HFilter.parse(hasFilter)
const impliedVisitor = new GenerateHaystackFilterImpliedVisitor(
this.#namespace
)
hasNode.accept(impliedVisitor)
this.append(`(${filter} or ${impliedVisitor.filter})`)
}
public visitRelationship(node: RelationshipNode): void {
const relationship = node.rel.relationship
const valuePostfix = `${node.rel.term ? `-${node.rel.term}` : ''}?${
node.ref ? ` ${node.ref.toFilter()}` : ''
}`
let filter = node.rel.toFilter()
if (node.ref) {
filter += ` ${node.ref.toFilter()}`
}
const impliedFilter = this.replaceImpliedTags(
[relationship],
0,
'',
valuePostfix
)
if (impliedFilter) {
filter = `(${filter} or ${impliedFilter})`
}
this.append(filter)
}
public visitWildcardEquals(node: WildcardEqualsNode): void {
const id = node.id.toFilter()
const valuePostfix = ` *== ${node.ref.toFilter()}`
let filter = `${id}${valuePostfix}`
const impliedFilter = this.replaceImpliedTags([id], 0, '', valuePostfix)
if (impliedFilter) {
filter = `(${filter} or ${impliedFilter})`
}
this.append(filter)
}
/**
* Replace any implied tags with the necessary rules and values.
*
* @param paths The paths to the value being tested.
* @param index The current index of the path.
* @param prefix Any prefix to add before each path.
* @param valuePostfix Any postfix to include after the value.
* @returns The updated filter.
*/
private replaceImpliedTags(
paths: string[],
index: number,
prefix = '',
valuePostFix = ''
): string {
let filter = ''
const path = paths[index]
const impliedBy = this.getImpliedBy(this.#namespace.byName(path))
if (impliedBy.length) {
filter =
prefix +
impliedBy
?.map((symbol, i) => {
const newPaths = [...paths]
newPaths[index] = symbol
if (i > 0) {
newPaths.splice(index + 1, newPaths.length)
}
let newFilter = new TokenPaths(newPaths).toFilter()
if (i === 0) {
newFilter = `${newFilter}${valuePostFix}`
}
return newFilter
})
.join(` and ${prefix}`) ?? path
if (impliedBy.length > 1) {
filter = `(${filter})`
}
}
return filter
}
/**
* Returns a list of tags to imply the rule against.
*
* @param def The def to test.
* @returns An array of tag names.
*/
private getImpliedBy(def: HDict | undefined): string[] {
const impliedBy = def?.get(IMPLIED_BY)
return valueIsKind<HList<HSymbol>>(impliedBy, Kind.List)
? impliedBy.map((symbol) => symbol.value)
: []
}
}