From df6781c450f77d689a3410d304e9ce451cab9c2c Mon Sep 17 00:00:00 2001 From: chriskari Date: Mon, 16 Sep 2024 15:33:26 +0200 Subject: [PATCH] fix: escape keban case in jsonata expression --- .../Extensibility/ExtensibilityDetails.js | 3 + .../Extensibility/helpers/jsonataWrapper.ts | 10 ++- .../helpers/tests/escapeKebabCase.test.js | 84 +++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/components/Extensibility/helpers/tests/escapeKebabCase.test.js diff --git a/src/components/Extensibility/ExtensibilityDetails.js b/src/components/Extensibility/ExtensibilityDetails.js index b1d282da60..321bdb7e58 100644 --- a/src/components/Extensibility/ExtensibilityDetails.js +++ b/src/components/Extensibility/ExtensibilityDetails.js @@ -37,6 +37,9 @@ export const ExtensibilityDetailsCore = ({ resource, }); + console.log(schema); + console.log(resMetaData); + const jsonata = useJsonata({}); const description = useCreateResourceDescription(resourceDescription); diff --git a/src/components/Extensibility/helpers/jsonataWrapper.ts b/src/components/Extensibility/helpers/jsonataWrapper.ts index e6ded4d3d8..4d1b96ca9e 100644 --- a/src/components/Extensibility/helpers/jsonataWrapper.ts +++ b/src/components/Extensibility/helpers/jsonataWrapper.ts @@ -7,8 +7,16 @@ import { permissionSetsSelector } from 'state/permissionSetsSelector'; import { jwtDecode } from 'jwt-decode'; import { AuthDataState, authDataState } from 'state/authDataAtom'; +export const escapeKebabCase = (expr: string) => { + return expr.replace( + /(? { if (!pod.metadata?.labels || !labels) return false; diff --git a/src/components/Extensibility/helpers/tests/escapeKebabCase.test.js b/src/components/Extensibility/helpers/tests/escapeKebabCase.test.js new file mode 100644 index 0000000000..1c0b80bf6b --- /dev/null +++ b/src/components/Extensibility/helpers/tests/escapeKebabCase.test.js @@ -0,0 +1,84 @@ +import { escapeKebabCase } from '../jsonataWrapper'; + +describe('escapeKebabCase', () => { + it('regular path', () => { + const expression = '$root.spec.myRegularPath'; + expect(escapeKebabCase(expression)).toBe('$root.spec.myRegularPath'); + }); + + it('kebab path 1', () => { + const expression = '$root.spec.my-kebab-path'; + expect(escapeKebabCase(expression)).toBe('$root.spec.`my-kebab-path`'); + }); + + it('kebab path, regular path', () => { + const expression = '$root.spec.my-kebab-path.subPath'; + expect(escapeKebabCase(expression)).toBe( + '$root.spec.`my-kebab-path`.subPath', + ); + }); + + it('multiple kebab paths', () => { + const expression = '$root.spec.my-kebab-path.sub-path'; + expect(escapeKebabCase(expression)).toBe( + '$root.spec.`my-kebab-path`.`sub-path`', + ); + }); + + it('special path', () => { + const expression = '$root.spec.some-$special-path'; + expect(escapeKebabCase(expression)).toBe('$root.spec.`some-$special-path`'); + }); + + it('direct follow-up 1', () => { + const expression = '$root.spec.my-kebab-path.subPath!=true'; + expect(escapeKebabCase(expression)).toBe( + '$root.spec.`my-kebab-path`.subPath!=true', + ); + }); + + it('direct follow-up 2', () => { + const expression = '$root.spec.my-kebab-path.subPath != true'; + expect(escapeKebabCase(expression)).toBe( + '$root.spec.`my-kebab-path`.subPath != true', + ); + }); + + it('numeric expression 1', () => { + const expression = '4 - 6'; + expect(escapeKebabCase(expression)).toBe('4 - 6'); + }); + + it('numeric expression 2', () => { + const expression = '$item.value1 - $item.value2'; + expect(escapeKebabCase(expression)).toBe('$item.value1 - $item.value2'); + }); + + it('numeric expression 3', () => { + const expression = '$item.value-one - $item.value-two'; + expect(escapeKebabCase(expression)).toBe( + '$item.`value-one` - $item.`value-two`', + ); + }); + + it('string literal 1', () => { + const expression = "hello, 'this-is-a-string', some-path"; + expect(escapeKebabCase(expression)).toBe( + "hello, 'this-is-a-string', `some-path`", + ); + }); + + it('string literal 2', () => { + const expression = 'hello, "this-is-a-string", some-path'; + expect(escapeKebabCase(expression)).toBe( + 'hello, "this-is-a-string", `some-path`', + ); + }); + + it('string literal 3', () => { + const expression = 'hello, `this-is-a-string`, some-path'; + expect(escapeKebabCase(expression)).toBe( + 'hello, `this-is-a-string`, `some-path`', + ); + }); +});