From 416da0232c5313f9f651e1a476c159da9d95cbb4 Mon Sep 17 00:00:00 2001 From: "ayush.jain@juspay.in" Date: Thu, 23 May 2024 16:55:45 +0530 Subject: [PATCH] feat: Add Juspay version and regex operations in jsonlogic --- Cargo.lock | 7 +++--- Cargo.toml | 2 +- clients/js/index.js | 2 +- clients/js/src/index.ts | 17 ++++++++++++--- clients/js/src/utils/operations.ts | 34 +++++++++++++++++++++++++----- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb3262530..5e9eef812 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2034,11 +2034,12 @@ dependencies = [ [[package]] name = "jsonlogic" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fde0699c6109316a5add3d42ae21ae954f207739f12b68914a0d447b9aa45e6f" +version = "0.5.2" +source = "git+https://github.com/juspay/jsonlogic_rs.git?rev=a60bded9ddbfd21e8c21e1ea114bacf43afd0c06#a60bded9ddbfd21e8c21e1ea114bacf43afd0c06" dependencies = [ + "regex", "serde_json", + "unicode-normalization", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 399e3922b..e03cfeb81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ chrono = { version = "0.4.26", features = ["serde"] } uuid = {version = "1.3.4", features = ["v4", "serde"]} reqwest = { version = "0.11.18", features = ["json"]} jsonschema = "~0.17" -jsonlogic = "0.5.1" +jsonlogic = { git = "https://github.com/juspay/jsonlogic_rs.git", rev = "a60bded9ddbfd21e8c21e1ea114bacf43afd0c06" } rs-snowflake = "0.6.0" rusoto_kms = "0.48.0" rusoto_signature = "0.48.0" diff --git a/clients/js/index.js b/clients/js/index.js index 3ca5ea833..c2056af42 100644 --- a/clients/js/index.js +++ b/clients/js/index.js @@ -1 +1 @@ -!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["Context-Aware-Config"]=t():r["Context-Aware-Config"]=t()}(self,(function(){return r={193:function(r,t,e){"use strict";var n=this&&this.__assign||function(){return n=Object.assign||function(r){for(var t,e=1,n=arguments.length;e>",l.compareSemanticIsGreater);var y=function(){function r(r){this.experiments=r}return r.prototype.getApplicableVariant=function(r,t){if(!Number.isInteger(t))throw new Error("Invalid toss, valid range: -1 to 100");for(var e=[],n=0,i=this.getSatisfiedExperiments(r);n=r*t.length))return t[Math.floor(e/r)]}},r}();t.ExperimentReader=y},472:function(r,t){"use strict";var e,n;Object.defineProperty(t,"__esModule",{value:!0}),t.ExperimentStatusType=t.VariantType=void 0,(n=t.VariantType||(t.VariantType={})).CONTROL="CONTROL",n.EXPERIMENTAL="EXPERIMENTAL",(e=t.ExperimentStatusType||(t.ExperimentStatusType={})).CREATED="CREATED",e.INPROGRESS="INPROGRESS",e.CONCLUDED="CONCLUDED"},925:function(r,t){"use strict";var e=this&&this.__spreadArray||function(r,t,e){if(e||2===arguments.length)for(var n,i=0,o=t.length;i0};t.compareSemanticIsGreater=function(r,t,n){return void 0===n?e(r,t):e(r,t)&&e(t,n)}},962:function(r,t,e){var n,i;n=function(){"use strict";Array.isArray||(Array.isArray=function(r){return"[object Array]"===Object.prototype.toString.call(r)});var r={},t={"==":function(r,t){return r==t},"===":function(r,t){return r===t},"!=":function(r,t){return r!=t},"!==":function(r,t){return r!==t},">":function(r,t){return r>t},">=":function(r,t){return r>=t},"<":function(r,t,e){return void 0===e?r=t?[]:n}};return r.is_logic=function(r){return"object"==typeof r&&null!==r&&!Array.isArray(r)&&1===Object.keys(r).length},r.truthy=function(r){return!(Array.isArray(r)&&0===r.length||!r)},r.get_operator=function(r){return Object.keys(r)[0]},r.get_values=function(t){return t[r.get_operator(t)]},r.apply=function(e,n){if(Array.isArray(e))return e.map((function(t){return r.apply(t,n)}));if(!r.is_logic(e))return e;var i,o,a,u,f,p=r.get_operator(e),l=e[p];if(Array.isArray(l)||(l=[l]),"if"===p||"?:"==p){for(i=0;i0){var c=String(p).split("."),s=t;for(i=0;i=r*e.length))return e[Math.floor(t/r)]}},r}();e.ExperimentReader=y},472:function(r,e){"use strict";var t,n;Object.defineProperty(e,"__esModule",{value:!0}),e.ExperimentStatusType=e.VariantType=void 0,(n=e.VariantType||(e.VariantType={})).CONTROL="CONTROL",n.EXPERIMENTAL="EXPERIMENTAL",(t=e.ExperimentStatusType||(e.ExperimentStatusType={})).CREATED="CREATED",t.INPROGRESS="INPROGRESS",t.CONCLUDED="CONCLUDED"},925:function(r,e){"use strict";var t=this&&this.__spreadArray||function(r,e,t){if(t||2===arguments.length)for(var n,i=0,o=e.length;i0},e.isJPVersionGreaterEqual=function(r,e){return t(r,e)>=0},e.isJPVersionLesser=function(r,e,n){return void 0===n?t(r,e)<0:t(r,e)<0&&t(e,n)<0},e.isJPVersionLesserEqual=function(r,e,n){return void 0===n?t(r,e)<=0:t(r,e)<=0&&t(e,n)<=0},e.matchRegex=function(r,e,t){var n=void 0!==t&&""!==t?"/".concat(t):"";return new RegExp(e+n).test(r)}},962:function(r,e,t){var n,i;n=function(){"use strict";Array.isArray||(Array.isArray=function(r){return"[object Array]"===Object.prototype.toString.call(r)});var r={},e={"==":function(r,e){return r==e},"===":function(r,e){return r===e},"!=":function(r,e){return r!=e},"!==":function(r,e){return r!==e},">":function(r,e){return r>e},">=":function(r,e){return r>=e},"<":function(r,e,t){return void 0===t?r=e?[]:n}};return r.is_logic=function(r){return"object"==typeof r&&null!==r&&!Array.isArray(r)&&1===Object.keys(r).length},r.truthy=function(r){return!(Array.isArray(r)&&0===r.length||!r)},r.get_operator=function(r){return Object.keys(r)[0]},r.get_values=function(e){return e[r.get_operator(e)]},r.apply=function(t,n){if(Array.isArray(t))return t.map((function(e){return r.apply(e,n)}));if(!r.is_logic(t))return t;var i,o,a,u,s,f=r.get_operator(t),p=t[f];if(Array.isArray(p)||(p=[p]),"if"===f||"?:"==f){for(i=0;i0){var l=String(f).split("."),c=e;for(i=0;i>", compareSemanticIsGreater); + jsonLogic.add_operation("match", matchRegex); + jsonLogic.add_operation("jp_ver_eq", isJPVersionEqual); + jsonLogic.add_operation("jp_ver_gt", isJPVersionGreater); + jsonLogic.add_operation("jp_ver_ge", isJPVersionGreaterEqual); + jsonLogic.add_operation("jp_ver_lt", isJPVersionLesser); + jsonLogic.add_operation("jp_ver_le", isJPVersionLesserEqual); } constructor(completeConfig: DataFromCacApi) { @@ -28,7 +33,13 @@ export class CacReader { const requiredOverrides: Array = []; for (let i = 0; i < this.contexts.length; i++) { - if (jsonLogic.apply(this.contexts[i].condition, data)) { + let logicResponse = false; + try { + logicResponse = jsonLogic.apply(this.contexts[i].condition, data); + } catch (e) { + console.error(e); + } + if (logicResponse) { requiredOverrides.push( ...this.contexts[i].override_with_keys.map(x => this.overrides[x]) ); diff --git a/clients/js/src/utils/operations.ts b/clients/js/src/utils/operations.ts index 9484e5e2b..b7f57f160 100644 --- a/clients/js/src/utils/operations.ts +++ b/clients/js/src/utils/operations.ts @@ -1,9 +1,33 @@ -const compareSemanticIsGreaterImp = function (version_a: string, version_b: string) { - return version_a.localeCompare(version_b, undefined, {numeric:true, sensitivity:'base'}) > 0; +const compareJPVersion = function (version_a: string, version_b: string): number { + return version_a.localeCompare(version_b, undefined, { numeric: true, sensitivity: 'base' }); } -export const compareSemanticIsGreater = function (version_a: string, version_b: string, version_c?: string) { +export const isJPVersionEqual = function (version_a: string, version_b: string): boolean { + return compareJPVersion(version_a, version_b) === 0; +} + +export const isJPVersionGreater = function (version_a: string, version_b: string): boolean { + return compareJPVersion(version_a, version_b) > 0; +} + +export const isJPVersionGreaterEqual = function (version_a: string, version_b: string): boolean { + return compareJPVersion(version_a, version_b) >= 0; +} + +export const isJPVersionLesser = function (version_a: string, version_b: string, version_c?: string): boolean { return (version_c === undefined ? - compareSemanticIsGreaterImp(version_a, version_b) : - (compareSemanticIsGreaterImp(version_a, version_b) && compareSemanticIsGreaterImp(version_b, version_c))); + compareJPVersion(version_a, version_b) < 0 : + (compareJPVersion(version_a, version_b) < 0 && compareJPVersion(version_b, version_c) < 0)); } + +export const isJPVersionLesserEqual = function (version_a: string, version_b: string, version_c?: string): boolean { + return (version_c === undefined ? + compareJPVersion(version_a, version_b) <= 0 : + (compareJPVersion(version_a, version_b) <= 0 && compareJPVersion(version_b, version_c) <= 0)) +} + +export const matchRegex = function (text: string, pattern: string, flag?: string): boolean { + const flagStr = flag !== undefined && flag !== "" ? `/${flag}` : ""; + const re = new RegExp(pattern + flagStr); + return re.test(text); +} \ No newline at end of file