-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
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>
src/renderer/utils/jsonPath.ts
Outdated
const columnArr = jsonPath.split(/(?<=\w)\./); | ||
|
||
columnArr.forEach((value, index) => { | ||
if (index == 0) { | ||
pathExpression = `${value}`; | ||
} else { | ||
pathExpression += `['${value}']`; | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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}]`)}`; |
|
||
}); | ||
|
||
test("strips '\' away from the result", () => { | ||
const res = parseJsonPath(".metadata.labels['serving\\.knative\\.dev/configuration']"); | ||
|
||
expect(res).toBe(".metadata.labels['serving.knative.dev/configuration']"); | ||
}); | ||
|
There was a problem hiding this comment.
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']"
There was a problem hiding this comment.
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
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>
There was a problem hiding this 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'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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", () => { |
There was a problem hiding this comment.
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:
- Include one with
\\\
signifying an escaped '' or something similar. - perhaps escaping other characters too, like
\'
(which might break your index conversion)
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
* 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>
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