From 1c5a6d60ac2289d1b560b1f8f4569e6163fdc745 Mon Sep 17 00:00:00 2001 From: ralphg6 Date: Tue, 19 Dec 2023 18:23:56 -0300 Subject: [PATCH] feat: non dynamic values --- .../0053 - slice_add_itens copy/expected.grl | 27 +++++++++++++++++++ .../0053 - slice_add_itens copy/features.json | 1 + .../parameters.json | 1 + .../0053 - slice_add_itens copy/rules.json | 10 +++++++ lib/parser/index.js | 10 ++++++- lib/transpiler/index.js | 13 ++++++--- package.json | 2 +- 7 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 __tests__/transpiler/cases/0053 - slice_add_itens copy/expected.grl create mode 100644 __tests__/transpiler/cases/0053 - slice_add_itens copy/features.json create mode 100644 __tests__/transpiler/cases/0053 - slice_add_itens copy/parameters.json create mode 100644 __tests__/transpiler/cases/0053 - slice_add_itens copy/rules.json diff --git a/__tests__/transpiler/cases/0053 - slice_add_itens copy/expected.grl b/__tests__/transpiler/cases/0053 - slice_add_itens copy/expected.grl new file mode 100644 index 0000000..91e65b1 --- /dev/null +++ b/__tests__/transpiler/cases/0053 - slice_add_itens copy/expected.grl @@ -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\"}")); + result.Put("test", ctx.GetMap("test")); + Changed("result"); + Retract("feat_test"); +} diff --git a/__tests__/transpiler/cases/0053 - slice_add_itens copy/features.json b/__tests__/transpiler/cases/0053 - slice_add_itens copy/features.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/__tests__/transpiler/cases/0053 - slice_add_itens copy/features.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/__tests__/transpiler/cases/0053 - slice_add_itens copy/parameters.json b/__tests__/transpiler/cases/0053 - slice_add_itens copy/parameters.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/__tests__/transpiler/cases/0053 - slice_add_itens copy/parameters.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/__tests__/transpiler/cases/0053 - slice_add_itens copy/rules.json b/__tests__/transpiler/cases/0053 - slice_add_itens copy/rules.json new file mode 100644 index 0000000..94a91cb --- /dev/null +++ b/__tests__/transpiler/cases/0053 - slice_add_itens copy/rules.json @@ -0,0 +1,10 @@ +{ + "test": { + "condition": "true", + "value": { + "percent": "%percent" + }, + "dynamic": false, + "type": "object" + } +} diff --git a/lib/parser/index.js b/lib/parser/index.js index b51f054..bf9e92e 100644 --- a/lib/parser/index.js +++ b/lib/parser/index.js @@ -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", @@ -160,6 +161,7 @@ function buildRules(rulesPlain, features, parameters, saliences) { name: feat, outputType, condition, + dynamic, precedence: `${saliences[feat] || base_salience}`, expression, result, @@ -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}`; diff --git a/lib/transpiler/index.js b/lib/transpiler/index.js index 579c26b..e70fecf 100644 --- a/lib/transpiler/index.js +++ b/lib/transpiler/index.js @@ -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; @@ -232,6 +236,7 @@ const transpiler = async (dir) => { outputType: fr.outputType, parameters: data.parameters, features: data.features, + dynamic: fr.dynamic }), })); diff --git a/package.json b/package.json index 54ca3d3..d7bf247 100644 --- a/package.json +++ b/package.json @@ -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": {