Skip to content

Commit

Permalink
fix(rule-engine): support allow not involve attributes should be true
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Nov 2, 2023
1 parent 757a0e5 commit 41b647d
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
55 changes: 55 additions & 0 deletions packages/rule-engine/__tests__/createSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,59 @@ describe('@zodash/rule-engine', () => {
description: true,
});
});

it('if key not in rules, should be always true', () => {
const rules: IRuleNode[] = [
{
type: 'Attr',
value: 'method',
children: [
{
type: 'Value',
value: 1,
children: [
{
type: 'Attr',
value: 'chat_id',
},
],
},
],
},
];

const runner = engine.create.sync(rules);

expect(
runner.run({
client: 1,
method: 1,
email: '[email protected]',
chat_id: '666',
is_group: false,
}),
).toEqual({
client: true,
method: true,
email: true,
chat_id: true,
is_group: true,
});

expect(
runner.run({
client: 1,
method: 2,
email: '[email protected]',
chat_id: '666',
is_group: false,
}),
).toEqual({
client: true,
method: true,
email: true,
chat_id: false,
is_group: true,
});
});
});
39 changes: 35 additions & 4 deletions packages/rule-engine/src/core/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,43 @@ export function create<DataSource>(

// real runner
async function run(dataSource: Partial<DataSource>) {
const shows: IShowData<DataSource> = Object.keys(dataSource).reduce(
(all, key) => ((all[key] = false), all),
{} as any,
);
let shows: IShowData<DataSource> = {} as any;

const allRuleKeys: Record<string, boolean> = {};

let attrNodeOfValue: IRuleAttrNode<DataSource> = null;

function traverse(rules: IRuleNode<DataSource>[]) {
for (const rule of rules) {
if (rule.type === 'Attr') {
allRuleKeys[rule.value] = true;

if (rule.children && !!rule.children.length) {
traverse(rule.children);
}
} else if (rule.type === 'Value') {
if (rule.children && !!rule.children.length) {
traverse(rule.children);
}
}
}
}

function initializeShows() {
traverse(rules);

shows = Object.keys(dataSource).reduce((all, key) => {
// 所有未参与的 key 都应该是 true
if (!allRuleKeys[key]) {
all[key] = true;
} else {
all[key] = false;
}

return all;
}, {} as any);
}

async function go(_rules: IRuleNode<DataSource>[]) {
for (const rule of _rules) {
// @1 attr show
Expand Down Expand Up @@ -83,6 +113,7 @@ export function create<DataSource>(
}
}

initializeShows();
await go(rules);

function getOnScaleTo() {
Expand Down
40 changes: 36 additions & 4 deletions packages/rule-engine/src/core/sync/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,43 @@ export function create<DataSource>(

// real runner
function run(dataSource: Partial<DataSource>) {
const shows: IShowData<DataSource> = Object.keys(dataSource).reduce(
(all, key) => ((all[key] = false), all),
{} as any,
);
let shows: IShowData<DataSource> = {} as any;

const allRuleKeys: Record<string, boolean> = {};

let attrNodeOfValue: IRuleAttrNode<DataSource> = null;

function traverse(rules: IRuleNode<DataSource>[]) {
for (const rule of rules) {
if (rule.type === 'Attr') {
allRuleKeys[rule.value] = true;

if (rule.children && !!rule.children.length) {
traverse(rule.children);
}
} else if (rule.type === 'Value') {
if (rule.children && !!rule.children.length) {
traverse(rule.children);
}
}
}
}

function initializeShows() {
traverse(rules);

shows = Object.keys(dataSource).reduce((all, key) => {
// 所有未参与的 key 都应该是 true
if (!allRuleKeys[key]) {
all[key] = true;
} else {
all[key] = false;
}

return all;
}, {} as any);
}

function go(_rules: IRuleNode<DataSource>[]) {
for (const rule of _rules) {
// @1 attr show
Expand Down Expand Up @@ -86,6 +116,8 @@ export function create<DataSource>(
return node.onHitAttr || defaultOnHitAttr;
}

//
initializeShows();
go(rules);

return shows;
Expand Down

0 comments on commit 41b647d

Please sign in to comment.