Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

feat: non dynamic values #5

Merged
merged 2 commits into from
Dec 20, 2023
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
27 changes: 27 additions & 0 deletions __tests__/transpiler/cases/0053 - non_dynamic_value/expected.grl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

rule RequiredParams salience 10000000 {
when
true
then

ctx.SetRequiredConfigured();
Changed("ctx");
Retract("RequiredParams");
}

rule HasRequiredParamsError salience 9999999 {
when
ctx.Has("requiredParamErrors")
then
Complete();
}

rule feat_test salience 1000 {
when
true
then
ctx.Put("test", processor.ToMap("{\"percent\":\"%percent\",\"hash\":\"#hash\",\"currency\":\"$currency\"}"));
result.Put("test", ctx.GetMap("test"));
Changed("result");
Retract("feat_test");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
12 changes: 12 additions & 0 deletions __tests__/transpiler/cases/0053 - non_dynamic_value/rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"test": {
"condition": "true",
"value": {
"percent": "%percent",
"hash": "#hash",
"currency": "$currency"
},
"dynamic": false,
"type": "object"
}
}
10 changes: 9 additions & 1 deletion lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function buildRules(rulesPlain, features, parameters, saliences) {
const condition = `${typeof expression.condition == "undefined"
? "true"
: expression.condition}`;
const dynamic = typeof expression.dynamic === 'undefined' || String(expression.dynamic).toLowerCase() === "true";
let result = true;
const feature = features.find((f) => f.name == feat) || {
type: "boolean",
Expand All @@ -160,6 +161,7 @@ function buildRules(rulesPlain, features, parameters, saliences) {
name: feat,
outputType,
condition,
dynamic,
precedence: `${saliences[feat] || base_salience}`,
expression,
result,
Expand Down Expand Up @@ -312,7 +314,13 @@ function calcPrecedences(rulesPlain) {
let rule = rulesPlain[feat];
if (typeof rule === "object") {
const condition = rule.condition;
rule = JSON.stringify(rule.value);
const dynamic = typeof rule.dynamic === 'undefined' || String(rule.dynamic).toLowerCase() === "true";

if (dynamic) {
rule = JSON.stringify(rule.value);
} else {
rule = "";
}

if (typeof condition != "undefined") {
rule += ` ${condition}`;
Expand Down
13 changes: 9 additions & 4 deletions lib/transpiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,15 @@ const transpile = (expression, options) => {
expression = JSON.stringify(expression);
}

expression = expression.replace(
/([$#%@])(\w+)((\.?\w+)*)(::(\w+))?/g,
transpileReplacer(options)
);
const dynamic = typeof options.dynamic === 'undefined' || String(options.dynamic).toLowerCase() === "true";

if (dynamic) {
expression = expression.replace(
/([$#%@])(\w+)((\.?\w+)*)(::(\w+))?/g,
transpileReplacer(options)
);
}

expression = transpileValue(options.outputType, expression);

return expression;
Expand Down Expand Up @@ -232,6 +236,7 @@ const transpiler = async (dir) => {
outputType: fr.outputType,
parameters: data.parameters,
features: data.features,
dynamic: fr.dynamic
}),
}));

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "featws-cli",
"version": "0.0.3-rc1",
"version": "0.0.4-rc1",
"description": "A FeatWS CLI to GRL",
"main": "index.js",
"scripts": {
Expand Down
Loading