chore: add support for contains clauses in filter store

This commit is contained in:
Randall Morey committed 2021-12-16 16:02:00 -05:00
1 parent a79823d3b1
commit 2c998bbb2b
2 files changed
+22 -4

No files matched your search

@@ -8,10 +8,11 @@ import { inject as service } from '@ember/service';
* @example
* const filter = new ResourceFilter({
* type: 'oidc',
* status: ['active', 'pending']
* status: ['active', 'pending'],
* authorized_actions: [{ contains: 'read' }]
* });
* log(filter.queryExpression);
* // ("/item/type" == "oidc") and ("/item/status" == "active" or "/item/status" == "pending")
* // ("/item/type" == "oidc") and ("/item/status" == "active" or "/item/status" == "pending") and ("read" in "/item/authorized_actions")
*/
export class ResourceFilter {
// =attributes
@@ -30,7 +31,14 @@ export class ResourceFilter {
const value = this.#filterObject[key];
const valuesArray = Array.isArray(value) ? value : [value];
return this.parenthetical(
this.or(valuesArray.map((value) => this.equals(key, value)))
this.or(
valuesArray.map((value) => {
if (value && value.contains) {
return this.in(value.contains, key);
}
return this.equals(key, value);
})
)
);
})
.flat()
@@ -56,6 +64,15 @@ export class ResourceFilter {
return value !== null ? `"/item/${key}" == "${value}"` : null;
}
/**
* @param {string} key
* @param value
* @return {string}
*/
in(value, key) {
return value !== null ? `"${value}" in "/item/${key}"` : null;
}
/**
* @param {string[]} clauses
* @return {string}
@@ -10,9 +10,10 @@ module('Unit | Service | resource-filter-store', function (hooks) {
const filter = new ResourceFilter({
type: 'oidc',
status: ['active', 'pending'],
authorized_actions: [{ contains: 'read' }],
});
const expected =
'("/item/type" == "oidc") and ("/item/status" == "active" or "/item/status" == "pending")';
'("/item/type" == "oidc") and ("/item/status" == "active" or "/item/status" == "pending") and ("read" in "/item/authorized_actions")';
assert.equal(filter.queryExpression, expected);
});
});