From 3fcea732fe38625ae3b2a6c2a614c4907ebe5e03 Mon Sep 17 00:00:00 2001 From: Sorenson Joshua Date: Sat, 2 Jul 2022 07:39:50 -0700 Subject: [PATCH 1/2] Add brackets to extended string completions --- bin/jq-paths | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/jq-paths b/bin/jq-paths index 3d0d51a..6afa041 100755 --- a/bin/jq-paths +++ b/bin/jq-paths @@ -3,7 +3,10 @@ JQ_REPL_JQ="${JQ_REPL_JQ:-jq}" $JQ_REPL_JQ -r ' [ - path(..) | map(select(type == "string") // "[]") + path(..) | map( + if type == "string" and test("[^A-z0-9_]") then + gsub("^(?.*)$";"[\"\(.match)\"]") + else "[]" end) | join(".") ] | sort | unique | .[] | split(".[]") | join("[]") | "." + . ' From 42f4617b0004a39ef971c39834173ab97e76edd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Reegn?= Date: Mon, 11 Jul 2022 21:59:39 +0200 Subject: [PATCH 2/2] Adjust jq-paths to use Generic Object Index only if necessary --- bin/jq-paths | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bin/jq-paths b/bin/jq-paths index 6afa041..605e73a 100755 --- a/bin/jq-paths +++ b/bin/jq-paths @@ -3,10 +3,14 @@ JQ_REPL_JQ="${JQ_REPL_JQ:-jq}" $JQ_REPL_JQ -r ' [ - path(..) | map( - if type == "string" and test("[^A-z0-9_]") then - gsub("^(?.*)$";"[\"\(.match)\"]") - else "[]" end) - | join(".") -] | sort | unique | .[] | split(".[]") | join("[]") | "." + . + path(..) | + map( + # use generic object index syntax if key contains non-alphanumeric characters or starts with a digit + select(type == "string" and (test("[^a-zA-Z0-9_]") or test("^[0-9]"))) |= "[\"" + . + "\"]" + ) | + map( + # numbers are assumed to be array indexes only + select(type == "number") |= "[]" + ) | join(".") +] | sort | unique | .[] | split(".[") | join("[") | "." + . '