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(),
|
crdInstance.getName(),
|
||||||
isNamespaced && crdInstance.getNs(),
|
isNamespaced && crdInstance.getNs(),
|
||||||
...extraColumns.map((column) => {
|
...extraColumns.map((column) => {
|
||||||
const pathExpression = parseJsonPath(column.jsonPath.slice(1));
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
renderBoolean: true,
|
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", () => {
|
test("should convert \. to use indexed notation", () => {
|
||||||
const res = parseJsonPath(".metadata.labels.kubesphere\\.io/alias-name");
|
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", () => {
|
test("strips '\' away from the result", () => {
|
||||||
@ -14,9 +32,4 @@ describe("parseJsonPath", () => {
|
|||||||
expect(res).toBe(".metadata.labels['serving.knative.dev/configuration']");
|
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) {
|
export function parseJsonPath(jsonPath: string) {
|
||||||
let pathExpression = jsonPath;
|
let pathExpression = jsonPath;
|
||||||
|
|
||||||
if (!jsonPath.includes("[") && jsonPath.includes("\\")) {
|
if (jsonPath.match(/[\\-]/g)) { // search for '\' and '-'
|
||||||
// convert cases where \. is present to use indexed notation,
|
// convert cases where \. or - is present to use indexed notation,
|
||||||
// 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']
|
||||||
const columnArr = jsonPath.split(/(?<=\w)\./);
|
|
||||||
|
|
||||||
columnArr.forEach((value, index) => {
|
const [first, ...rest] = jsonPath.split(/(?<=\w)\./); // split jsonPath by '.' (\. cases are ignored)
|
||||||
if (index == 0) {
|
|
||||||
pathExpression = `${value}`;
|
pathExpression = `${convertToIndexNotation(first, true)}${rest.map(value => convertToIndexNotation(value)).join("")}`;
|
||||||
} else {
|
|
||||||
pathExpression += `['${value}']`;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip '\' characters from the result
|
// strip '\' characters from the result
|
||||||
return pathExpression.replace(/\\/g, "");
|
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