mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Parse jsonPath properly
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
parent
9cc342f82e
commit
1df25ffe2b
@ -13,6 +13,7 @@ import { crdStore } from "./crd.store";
|
|||||||
import { KubeObjectMeta } from "../kube-object/kube-object-meta";
|
import { KubeObjectMeta } from "../kube-object/kube-object-meta";
|
||||||
import { Input } from "../input";
|
import { Input } from "../input";
|
||||||
import { AdditionalPrinterColumnsV1, CustomResourceDefinition } from "../../api/endpoints/crd.api";
|
import { AdditionalPrinterColumnsV1, CustomResourceDefinition } from "../../api/endpoints/crd.api";
|
||||||
|
import { parseJsonPath } from "../../utils/jsonPath";
|
||||||
|
|
||||||
interface Props extends KubeObjectDetailsProps<CustomResourceDefinition> {
|
interface Props extends KubeObjectDetailsProps<CustomResourceDefinition> {
|
||||||
}
|
}
|
||||||
@ -46,7 +47,7 @@ export class CrdResourceDetails extends React.Component<Props> {
|
|||||||
renderAdditionalColumns(crd: CustomResourceDefinition, columns: AdditionalPrinterColumnsV1[]) {
|
renderAdditionalColumns(crd: CustomResourceDefinition, columns: AdditionalPrinterColumnsV1[]) {
|
||||||
return columns.map(({ name, jsonPath: jp }) => (
|
return columns.map(({ name, jsonPath: jp }) => (
|
||||||
<DrawerItem key={name} name={name} renderBoolean>
|
<DrawerItem key={name} name={name} renderBoolean>
|
||||||
{convertSpecValue(jsonPath.value(crd, `$..["${jp.slice(1).replace(/\\/g, "")}"]`))}
|
{convertSpecValue(jsonPath.value(crd, parseJsonPath(jp.slice(1))))}
|
||||||
</DrawerItem>
|
</DrawerItem>
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import { autorun, computed } from "mobx";
|
|||||||
import { crdStore } from "./crd.store";
|
import { crdStore } from "./crd.store";
|
||||||
import { TableSortCallback } from "../table";
|
import { TableSortCallback } from "../table";
|
||||||
import { apiManager } from "../../api/api-manager";
|
import { apiManager } from "../../api/api-manager";
|
||||||
|
import { parseJsonPath } from "../../utils/jsonPath";
|
||||||
|
|
||||||
interface Props extends RouteComponentProps<ICRDRouteParams> {
|
interface Props extends RouteComponentProps<ICRDRouteParams> {
|
||||||
}
|
}
|
||||||
@ -61,7 +62,7 @@ export class CrdResources extends React.Component<Props> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
extraColumns.forEach(column => {
|
extraColumns.forEach(column => {
|
||||||
sortingCallbacks[column.name] = (item: KubeObject) => jsonPath.value(item, `$..["${column.jsonPath.slice(1).replace(/\\/g, "")}"]`);
|
sortingCallbacks[column.name] = (item: KubeObject) => jsonPath.value(item, parseJsonPath(column.jsonPath.slice(1)));
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -91,10 +92,17 @@ export class CrdResources extends React.Component<Props> {
|
|||||||
renderTableContents={(crdInstance: KubeObject) => [
|
renderTableContents={(crdInstance: KubeObject) => [
|
||||||
crdInstance.getName(),
|
crdInstance.getName(),
|
||||||
isNamespaced && crdInstance.getNs(),
|
isNamespaced && crdInstance.getNs(),
|
||||||
...extraColumns.map(column => ({
|
...extraColumns.map((column) => {
|
||||||
renderBoolean: true,
|
const pathExpression = parseJsonPath(column.jsonPath.slice(1));
|
||||||
children: jsonPath.value(crdInstance, `$..["${column.jsonPath.slice(1).replace(/\\/g, "")}"]`),
|
|
||||||
})),
|
console.log(pathExpression);
|
||||||
|
|
||||||
|
return {
|
||||||
|
renderBoolean: true,
|
||||||
|
children: jsonPath.value(crdInstance, pathExpression.replace(/\\/g, "")),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
),
|
||||||
crdInstance.getAge(),
|
crdInstance.getAge(),
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|||||||
22
src/renderer/utils/__tests__/jsonPath.test.tsx
Normal file
22
src/renderer/utils/__tests__/jsonPath.test.tsx
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { parseJsonPath } from "../jsonPath";
|
||||||
|
|
||||||
|
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']");
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
test("strips '\' away from the result", () => {
|
||||||
|
const res = parseJsonPath(".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");
|
||||||
|
});
|
||||||
|
});
|
||||||
21
src/renderer/utils/jsonPath.ts
Normal file
21
src/renderer/utils/jsonPath.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
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)\./);
|
||||||
|
|
||||||
|
columnArr.forEach((value, index) => {
|
||||||
|
if (index == 0) {
|
||||||
|
pathExpression = `${value}`;
|
||||||
|
} else {
|
||||||
|
pathExpression += `['${value}']`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// strip '\' characters from the result
|
||||||
|
return pathExpression.replace(/\\/g, "");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user