Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape kebab case in jsonata expression #3348

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 12 additions & 1 deletion src/components/Extensibility/helpers/jsonataWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
import { jwtDecode } from 'jwt-decode';
import { AuthDataState, authDataState } from 'state/authDataAtom';

export const escapeKebabCase = (expr: string) => {
return expr.replace(
/(["'`])(?:\\.|.)*?\1|([a-zA-Z_][\w-]*)(-\w+)/g,
Fixed Show fixed Hide fixed
(match, quotedString, identifier, hyphenPart) => {
if (quotedString) return match;
return `\`${identifier}${hyphenPart}\``;
},
);
};

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { escapeKebabCase } from '../jsonataWrapper';

describe('escapeKebabCase', () => {
it('regular path', () => {
const expression = '$root.spec.myRegularPath';
expect(escapeKebabCase(expression)).toBe('$root.spec.myRegularPath');
});

it('kebab path', () => {
const expression = '$root.spec.my-kebab-path';
expect(escapeKebabCase(expression)).toBe('$root.spec.`my-kebab-path`');
});

it('kebab path and 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('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`',
);
});
});
Loading