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

Parse jsonPath expressions #1793

Merged
merged 10 commits into from
Dec 21, 2020
Merged

Conversation

nevalla
Copy link
Contributor

@nevalla nevalla commented Dec 17, 2020

jsonPath.value() method does not work well with invalid-indentifier names, so changed these to use indexed notation as suggested here. Also required to get rid of \ characters.

Fixes #503, fixes #1773

Signed-off-by: Lauri Nevala lauri.nevala@gmail.com

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
@nevalla nevalla added the bug Something isn't working label Dec 17, 2020
@nevalla nevalla requested a review from a team December 17, 2020 11:29
src/renderer/utils/__tests__/jsonPath.test.tsx Outdated Show resolved Hide resolved
Comment on lines 8 to 16
const columnArr = jsonPath.split(/(?<=\w)\./);

columnArr.forEach((value, index) => {
if (index == 0) {
pathExpression = `${value}`;
} else {
pathExpression += `['${value}']`;
}
});
Copy link
Collaborator

@Nokel81 Nokel81 Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const columnArr = jsonPath.split(/(?<=\w)\./);
columnArr.forEach((value, index) => {
if (index == 0) {
pathExpression = `${value}`;
} else {
pathExpression += `['${value}']`;
}
});
const [first, ...rest] = jsonPath.split(/(?<=\w)\./);
if (rest.length === 0) {
pathExpression = first;
}
pathExpression = `${first}${rest.map(value => `[${value}]`)}`;

Comment on lines 8 to 16

});

test("strips '\' away from the result", () => {
const res = parseJsonPath(".metadata.labels['serving\\.knative\\.dev/configuration']");

expect(res).toBe(".metadata.labels['serving.knative.dev/configuration']");
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a test for the following case: ".metadata.labels\.serving['some.other.item']"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test case for this

@jakolehm jakolehm added this to the 4.0.4 milestone Dec 17, 2020
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
Copy link
Collaborator

@Nokel81 Nokel81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I thought about the requirement to convert other "escaped" characters

@@ -0,0 +1,35 @@
// Helper to convert strings used for jsonPath where \. or - is present to use indexed notation,
// for example: .metadata.labels.kubesphere\.io/alias-name -> .metadata.labels['kubesphere\.io/alias-name']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// for example: .metadata.labels.kubesphere\.io/alias-name -> .metadata.labels['kubesphere\.io/alias-name']
// for example: .metadata.labels.kubesphere\.io/alias-name -> .metadata.labels['kubesphere.io/alias-name']

@@ -0,0 +1,35 @@
import { parseJsonPath } from "../jsonPath";

describe("parseJsonPath", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some more test cases that I have thought of:

  1. Include one with \\\ signifying an escaped '' or something similar.
  2. perhaps escaping other characters too, like \' (which might break your index conversion)

@jakolehm jakolehm modified the milestones: 4.0.4, 4.0.5 Dec 18, 2020
@jakolehm jakolehm merged commit e8f36e9 into master Dec 21, 2020
@jakolehm jakolehm deleted the fix/jsonpath_on_additional_printer_columns branch December 21, 2020 20:17
jakolehm pushed a commit that referenced this pull request Dec 22, 2020
* Fix jsonPath calls by removing \ characters and using $..[] notation

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Parse jsonPath properly

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Cleanup

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* More cleanup

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Improve parsing

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Finetuning

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Stringify children only if value is object or array

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Test other escaped characters do not cause issues

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
@jakolehm jakolehm mentioned this pull request Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

An error occurs when loading list of specific custom resources Crash on CRD "revision" of Knative
3 participants