1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Finetuning

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala 2020-12-18 13:04:21 +02:00
parent 3453c337de
commit ed2703c424
2 changed files with 6 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import { parseJsonPath } from "../jsonPath"; import { parseJsonPath } from "../jsonPath";
describe("parseJsonPath", () => { 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']");
@ -13,20 +13,20 @@ describe("parseJsonPath", () => {
expect(res).toBe(".metadata.labels['alias-name']"); expect(res).toBe(".metadata.labels['alias-name']");
}); });
test("should handle scenario when both \ and indexed notation is present", () => { test("should handle scenario when both \\. and indexed notation are present", () => {
const rest = parseJsonPath(".metadata.labels\\.serving['some.other.item']"); const rest = parseJsonPath(".metadata.labels\\.serving['some.other.item']");
expect(rest).toBe(".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", () => { test("should not touch given jsonPath if no invalid characters present", () => {
const res = parseJsonPath(".status.conditions[?(@.type=='Ready')].status"); const res = parseJsonPath(".status.conditions[?(@.type=='Ready')].status");
expect(res).toBe(".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", () => {
const res = parseJsonPath(".metadata.labels['serving\\.knative\\.dev/configuration']"); const res = parseJsonPath(".metadata.labels['serving\\.knative\\.dev/configuration']");
expect(res).toBe(".metadata.labels['serving.knative.dev/configuration']"); expect(res).toBe(".metadata.labels['serving.knative.dev/configuration']");

View File

@ -1,11 +1,10 @@
// 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']
export function parseJsonPath(jsonPath: string) { export function parseJsonPath(jsonPath: string) {
let pathExpression = jsonPath; let pathExpression = jsonPath;
if (jsonPath.match(/[\\-]/g)) { // search for '\' and '-' 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']
const [first, ...rest] = jsonPath.split(/(?<=\w)\./); // split jsonPath by '.' (\. cases are ignored) const [first, ...rest] = jsonPath.split(/(?<=\w)\./); // split jsonPath by '.' (\. cases are ignored)
pathExpression = `${convertToIndexNotation(first, true)}${rest.map(value => convertToIndexNotation(value)).join("")}`; pathExpression = `${convertToIndexNotation(first, true)}${rest.map(value => convertToIndexNotation(value)).join("")}`;