Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaults for atoms #39

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/changes/@educorvi/rita/develop_2024-10-21-08-01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@educorvi/rita",
"comment": "allow defaults for atom values",
"type": "minor"
}
],
"packageName": "@educorvi/rita"
}
6 changes: 5 additions & 1 deletion rita-core/src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ export default class Parser {
* @param jsonRuleset the atom
*/
public parseAtom(jsonRuleset: Record<string, any>): Atom {
return new Atom(jsonRuleset['path'], !!jsonRuleset['isDate']);
return new Atom(
jsonRuleset['path'],
!!jsonRuleset['isDate'],
jsonRuleset['default']
);
}

/**
Expand Down
27 changes: 22 additions & 5 deletions rita-core/src/logicElements/Atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,30 @@ export class Atom extends Formula {
*/
public path: string;
public isDate: boolean;
public defaultValue?: FormulaResults | Array<FormulaResults>;

constructor(path: string, isDate: boolean = false) {
constructor(
path: string,
isDate: boolean = false,
defaultValue?: FormulaResults | Array<FormulaResults>
) {
super();
this.path = path;
this.isDate = isDate;
this.defaultValue = defaultValue;
}

/**
* Get the value of an object property or array by a path that is passed as string
* @param object object
* @param path path
* @param defaultVal default value to return if path is not found
* @param context the context of the formula
*/
static getPropertyByString(
static async getPropertyByString(
object: any,
path: string,
defaultVal?: FormulaResults | Array<FormulaResults>,
context?: Formula
): Promise<FormulaResults | FormulaResults[]> {
path = path.replace(/\[(\w+)]/g, '.$1'); // convert indexes to properties
Expand All @@ -50,6 +58,8 @@ export class Atom extends Formula {
const k = a[i];
if (k in object) {
object = object[k];
} else if (defaultVal) {
return defaultVal;
} else {
throw new UndefinedPathError(
'Undefinded path in data: ' + path,
Expand All @@ -68,7 +78,12 @@ export class Atom extends Formula {
getPropertyByString(
object: any
): Promise<FormulaResults | FormulaResults[]> {
return Atom.getPropertyByString(object, this.path, this);
return Atom.getPropertyByString(
object,
this.path,
this.defaultValue,
this
);
}

validate(): boolean {
Expand All @@ -81,13 +96,15 @@ export class Atom extends Formula {
path: this.path,
};
if (this.isDate) at['isDate'] = true;
if (this.defaultValue) at['default'] = this.defaultValue;
return at;
}

async evaluate(
data: Record<string, any>
): Promise<FormulaResults | Array<FormulaResults>> {
const val = this.getPropertyByString(data);
): Promise<FormulaResults | FormulaResults[]> {
let val: FormulaResults | FormulaResults[] =
await this.getPropertyByString(data);

if (typeof val === 'string' && this.isDate) {
return parseDate(val, this);
Expand Down
20 changes: 20 additions & 0 deletions rita-core/src/schema/atom.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@
"type": "boolean",
"default": false,
"description": "Must be set to true if the atom should be parsed as a date"
},
"default": {
"oneOf": [
{
"type": "string"
},
{
"type": "number"
},
{
"type": "boolean"
},
{
"type": "string",
"format": "date-time"
},
{
"type": "array"
}
]
}
},
"required": ["type", "path"],
Expand Down
21 changes: 21 additions & 0 deletions rita-core/test/assets/defaultVal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "../../../rita-core/src/schema/schema.json",
"rules": [
{
"id": "r1",
"comment": "",
"rule": {
"type": "comparison",
"operation": "equal",
"arguments": [
{
"type": "atom",
"path": "fancyness",
"default": "unicorn"
},
"unicorn"
]
}
}
]
}
10 changes: 10 additions & 0 deletions rita-core/test/implementationTests/atom.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Parser } from '../../src';
// @ts-ignore
import { exampleData, ruleTemplate } from '../assets/exampleData';
// @ts-ignore
import ruleset_defaultVal from '../assets/defaultVal.json';

const p = new Parser();

Expand Down Expand Up @@ -44,3 +46,11 @@ it('second customer rated', () => {
});
expect(rule.evaluate(exampleData)).resolves.toBe(true);
});
it('use default value', () => {
const rule = p.parseRuleSet(ruleset_defaultVal)[0];
expect(rule.evaluate({})).resolves.toBe(true);
});
it('do not use default value when value is specified', () => {
const rule = p.parseRuleSet(ruleset_defaultVal)[0];
expect(rule.evaluate({ fancyness: 'very' })).resolves.toBe(false);
});
3 changes: 3 additions & 0 deletions rita-core/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import rule_qfa from './assets/quantifiers_fa.json';
// @ts-ignore
import rule_qex from './assets/quantifiers_ex.json';
// @ts-ignore
import rule_defaultValue from './assets/defaultVal.json';
// @ts-ignore
import macros from './assets/macros';
import { DateTime } from 'luxon';

Expand Down Expand Up @@ -132,6 +134,7 @@ describe('Validate Rule examples', () => {
rule_qfa,
rule_qex,
macros,
rule_defaultValue,
];
for (let i = 0; i < examples.length; i++) {
const example = examples[i];
Expand Down
Loading