增加在json查询中支持escape字符

This commit is contained in:
Xu Chang 2023-11-30 14:41:17 +08:00
parent fbb45dbc26
commit d710bdc7d0
4 changed files with 89 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { cloneDeep, get, groupBy, set, unset, difference, intersection, pull, pick } from 'oak-domain/lib/utils/lodash';
import { cloneDeep, get, groupBy, set, unset, differenceBy, intersectionBy, pull, pick } from 'oak-domain/lib/utils/lodash';
import { assert } from 'oak-domain/lib/utils/assert';
import { DeleteAtAttribute, CreateAtAttribute, UpdateAtAttribute } from "oak-domain/lib/types/Entity";
import { EXPRESSION_PREFIX, SUB_QUERY_PREDICATE_KEYWORD } from 'oak-domain/lib/types/Demand';
@ -469,7 +469,12 @@ export default class TreeStore extends CascadeStore {
const array = value instanceof Array ? value : [value];
return (row) => {
const data = get(row, path);
return difference(array, data).length === 0 || obscurePass(data, option);
return differenceBy(array, data, (value) => {
if (typeof value === 'object') {
return JSON.stringify(value);
}
return value;
}).length === 0 || obscurePass(data, option);
};
}
case '$overlaps': {
@ -477,7 +482,12 @@ export default class TreeStore extends CascadeStore {
const array = value instanceof Array ? value : [value];
return (row) => {
const data = get(row, path);
return intersection(array, data).length > 0 || obscurePass(data, option);
return intersectionBy(array, data, (value) => {
if (typeof value === 'object') {
return JSON.stringify(value);
}
return value;
}).length > 0 || obscurePass(data, option);
};
}
default: {
@ -523,7 +533,8 @@ export default class TreeStore extends CascadeStore {
fns2.push(this.translatePredicate(path, attr, p[attr]));
}
else {
const path2 = path ? `${path}.${attr}` : attr;
const attr2 = attr.startsWith('.') ? attr.slice(1) : attr;
const path2 = path ? `${path}.${attr2}` : attr2;
if (typeof p[attr] !== 'object') {
fns2.push(this.translatePredicate(path2, '$eq', filter[attr]));
}

View File

@ -471,7 +471,12 @@ class TreeStore extends CascadeStore_1.CascadeStore {
const array = value instanceof Array ? value : [value];
return (row) => {
const data = (0, lodash_1.get)(row, path);
return (0, lodash_1.difference)(array, data).length === 0 || obscurePass(data, option);
return (0, lodash_1.differenceBy)(array, data, (value) => {
if (typeof value === 'object') {
return JSON.stringify(value);
}
return value;
}).length === 0 || obscurePass(data, option);
};
}
case '$overlaps': {
@ -479,7 +484,12 @@ class TreeStore extends CascadeStore_1.CascadeStore {
const array = value instanceof Array ? value : [value];
return (row) => {
const data = (0, lodash_1.get)(row, path);
return (0, lodash_1.intersection)(array, data).length > 0 || obscurePass(data, option);
return (0, lodash_1.intersectionBy)(array, data, (value) => {
if (typeof value === 'object') {
return JSON.stringify(value);
}
return value;
}).length > 0 || obscurePass(data, option);
};
}
default: {
@ -525,7 +535,8 @@ class TreeStore extends CascadeStore_1.CascadeStore {
fns2.push(this.translatePredicate(path, attr, p[attr]));
}
else {
const path2 = path ? `${path}.${attr}` : attr;
const attr2 = attr.startsWith('.') ? attr.slice(1) : attr;
const path2 = path ? `${path}.${attr2}` : attr2;
if (typeof p[attr] !== 'object') {
fns2.push(this.translatePredicate(path2, '$eq', filter[attr]));
}

View File

@ -1,6 +1,6 @@
import {
cloneDeep, get, groupBy, set, unset,
difference, intersection, pull, pick
differenceBy, intersectionBy, pull, pick
} from 'oak-domain/lib/utils/lodash';
import { assert } from 'oak-domain/lib/utils/assert';
import {
@ -603,7 +603,12 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
const array = value instanceof Array ? value : [value];
return (row) => {
const data = get(row, path);
return difference(array, data).length === 0 || obscurePass(data, option);
return differenceBy(array, data, (value: any) => {
if (typeof value === 'object') {
return JSON.stringify(value);
}
return value;
}).length === 0 || obscurePass(data, option);
};
}
case '$overlaps': {
@ -611,7 +616,12 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
const array = value instanceof Array ? value : [value];
return (row) => {
const data = get(row, path);
return intersection(array, data).length > 0 || obscurePass(data, option);
return intersectionBy(array, data, (value: any) => {
if (typeof value === 'object') {
return JSON.stringify(value);
}
return value;
}).length > 0 || obscurePass(data, option);
};
}
default: {
@ -668,7 +678,8 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
);
}
else {
const path2 = path ? `${path}.${attr}` : attr;
const attr2 = attr.startsWith('.') ? attr.slice(1) : attr;
const path2 = path ? `${path}.${attr2}` : attr2;
if (typeof p[attr] !== 'object') {
fns2.push(this.translatePredicate(path2, '$eq', filter[attr]));
}

View File

@ -1251,6 +1251,51 @@ describe('基础测试', function () {
assert(row9.length === 1);
// console.log(JSON.stringify(row7));
});
it('[1.13]json escapes', () => {
const store = new FrontendStore(storageSchema);
const context = new FrontendRuntimeContext(store);
context.begin();
const id = generateNewId();
store.operate('oper', {
id: generateNewId(),
action: 'create',
data: {
id,
action: 'test',
data: {
$or: [{
name: 'xc',
}, {
name: {
$includes: 'xc',
}
}],
},
targetEntity: 'bbb',
}
}, context, {});
const rows1 = store.select('oper', {
data: {
id: 1,
},
filter: {
id,
data: {
'.$or': {
$contains: {
name: 'xc',
},
},
},
},
}, context, {});
assert(rows1.length === 1);
context.commit();
});
});