mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Improve parsing
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
parent
cb8893722c
commit
7a83127010
@ -93,11 +93,9 @@ export class CrdResources extends React.Component<Props> {
|
||||
crdInstance.getName(),
|
||||
isNamespaced && crdInstance.getNs(),
|
||||
...extraColumns.map((column) => {
|
||||
const pathExpression = parseJsonPath(column.jsonPath.slice(1));
|
||||
|
||||
return {
|
||||
renderBoolean: true,
|
||||
children: jsonPath.value(crdInstance, pathExpression),
|
||||
children: jsonPath.value(crdInstance, parseJsonPath(column.jsonPath.slice(1))),
|
||||
};
|
||||
}
|
||||
),
|
||||
|
||||
@ -4,8 +4,26 @@ describe("parseJsonPath", () => {
|
||||
test("should convert \. to use indexed notation", () => {
|
||||
const res = parseJsonPath(".metadata.labels.kubesphere\\.io/alias-name");
|
||||
|
||||
expect(res).toBe(".metadata['labels']['kubesphere.io/alias-name']");
|
||||
expect(res).toBe(".metadata.labels['kubesphere.io/alias-name']");
|
||||
});
|
||||
|
||||
test("should convert '-' to use indexed notation", () => {
|
||||
const res = parseJsonPath(".metadata.labels.alias-name");
|
||||
|
||||
expect(res).toBe(".metadata.labels['alias-name']");
|
||||
});
|
||||
|
||||
test("should handle scenario when both \ and indexed notation is present", () => {
|
||||
const rest = parseJsonPath(".metadata.labels\\.serving['some.other.item']");
|
||||
|
||||
expect(rest).toBe(".metadata['labels.serving']['some.other.item']");
|
||||
});
|
||||
|
||||
|
||||
test("should not touch given jsonPath if no invalid characters", () => {
|
||||
const res = parseJsonPath(".status.conditions[?(@.type=='Ready')].status");
|
||||
|
||||
expect(res).toBe(".status.conditions[?(@.type=='Ready')].status");
|
||||
});
|
||||
|
||||
test("strips '\' away from the result", () => {
|
||||
@ -14,9 +32,4 @@ describe("parseJsonPath", () => {
|
||||
expect(res).toBe(".metadata.labels['serving.knative.dev/configuration']");
|
||||
});
|
||||
|
||||
test("should not touch given jsonPath if no invalid characters", () => {
|
||||
const res = parseJsonPath(".status.conditions[?(@.type=='Ready')].status");
|
||||
|
||||
expect(res).toBe(".status.conditions[?(@.type=='Ready')].status");
|
||||
});
|
||||
});
|
||||
|
||||
@ -2,20 +2,35 @@
|
||||
export function parseJsonPath(jsonPath: string) {
|
||||
let pathExpression = jsonPath;
|
||||
|
||||
if (!jsonPath.includes("[") && jsonPath.includes("\\")) {
|
||||
// convert cases where \. is present to use indexed notation,
|
||||
// for example: .metadata.labels.kubesphere\.io/alias-name -> .metadata['labels']['kubesphere\.io/alias-name']
|
||||
const columnArr = jsonPath.split(/(?<=\w)\./);
|
||||
if (jsonPath.match(/[\\-]/g)) { // search for '\' and '-'
|
||||
// convert cases where \. or - is present to use indexed notation,
|
||||
// for example: .metadata.labels.kubesphere\.io/alias-name -> .metadata.labels.['kubesphere\.io/alias-name']
|
||||
|
||||
columnArr.forEach((value, index) => {
|
||||
if (index == 0) {
|
||||
pathExpression = `${value}`;
|
||||
} else {
|
||||
pathExpression += `['${value}']`;
|
||||
}
|
||||
});
|
||||
const [first, ...rest] = jsonPath.split(/(?<=\w)\./); // split jsonPath by '.' (\. cases are ignored)
|
||||
|
||||
pathExpression = `${convertToIndexNotation(first, true)}${rest.map(value => convertToIndexNotation(value)).join("")}`;
|
||||
}
|
||||
|
||||
// strip '\' characters from the result
|
||||
return pathExpression.replace(/\\/g, "");
|
||||
}
|
||||
|
||||
function convertToIndexNotation(key: string, firstItem = false) {
|
||||
if (key.match(/[\\-]/g)) { // check if found '\' and '-' in key
|
||||
if (key.includes("[")) { // handle cases where key contains [...]
|
||||
const keyToConvert = key.match(/^.*(?=\[)/g); // get the text from the key before '['
|
||||
|
||||
if (keyToConvert && keyToConvert[0].match(/[\\-]/g)) { // check if that part contains illegal characters
|
||||
return key.replace(keyToConvert[0], `['${keyToConvert[0]}']`); // surround key with '[' and ']'
|
||||
} else {
|
||||
return `.${key}`; // otherwise return as is with leading '.'
|
||||
}
|
||||
}
|
||||
|
||||
return `['${key}']`;
|
||||
} else { // no illegal chracters found, do not touch
|
||||
const prefix = firstItem ? "" : ".";
|
||||
|
||||
return `${prefix}${key}`;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user