Skip to content

Commit

Permalink
fix: escape keban case in jsonata expression
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskari committed Sep 16, 2024
1 parent f32d3aa commit df6781c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/components/Extensibility/ExtensibilityDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const ExtensibilityDetailsCore = ({
resource,
});

console.log(schema);
console.log(resMetaData);

const jsonata = useJsonata({});

const description = useCreateResourceDescription(resourceDescription);
Expand Down
10 changes: 9 additions & 1 deletion src/components/Extensibility/helpers/jsonataWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
/(?<!`)(?<!["'0-9a-zA-Z_])([a-zA-Z0-9_]+(?:-[a-zA-Z0-9_]+)+)(?!["'`0-9])/g,
'`$1`',
);
};

export function jsonataWrapper(expression: string) {
const exp = jsonata(expression);
const escapedExpression = escapeKebabCase(expression);
const exp = jsonata(escapedExpression);

exp.registerFunction('matchByLabelSelector', (pod, labels) => {
if (!pod.metadata?.labels || !labels) return false;
Expand Down
84 changes: 84 additions & 0 deletions src/components/Extensibility/helpers/tests/escapeKebabCase.test.js
Original file line number Diff line number Diff line change
@@ -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`',
);
});
});

0 comments on commit df6781c

Please sign in to comment.