diff mbox series

[1/1] jshn.sh: Add getters for the cursor and its parent

Message ID mailman.44922.1746396293.1866309.openwrt-devel@lists.openwrt.org
State New
Headers show
Series [1/1] jshn.sh: Add getters for the cursor and its parent | expand

Commit Message

Philip Prindeville May 4, 2025, 10:04 p.m. UTC
The sender domain has a DMARC Reject/Quarantine policy which disallows
sending mailing list messages using the original "From" header.

To mitigate this problem, the original message has been wrapped
automatically by the mailing list software.
From: Philip Prindeville <philipp@redfish-solutions.com>

When building JSON, the order in which it is synthesized might not
match the order in which the source information is parsed.  In those
cases, it is useful to be able to save your place in an object or
an array and come back to it later and fill it out.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
---
 sh/jshn.sh | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
diff mbox series

Patch

diff --git a/sh/jshn.sh b/sh/jshn.sh
index 1d3055711820e5a21ea59cae49e7bf3f56b626d8..5599c85835d0c81c8d47a9dfd73f32e4db2a7c9b 100644
--- a/sh/jshn.sh
+++ b/sh/jshn.sh
@@ -197,6 +197,42 @@  json_add_fields() {
 	done
 }
 
+json_get_position() {
+	local __dest="$1"
+	eval "export -- \"$__dest=\${JSON_CUR}\"; [ -n \"\${JSON_CUR+x}\" ]"
+}
+
+json_move_to() {
+	local cur="$1"
+	_json_set_var JSON_CUR "$cur"
+}
+
+json_get_parent_position() {
+	local __dest="$1" cur parent
+	_json_get_var cur JSON_CUR
+	parent="U_$cur"
+	eval "export -- \"$__dest=\${$parent}\"; [ -n \"\${$parent+x}\" ]"
+}
+
+json_get_root_position() {
+	local __dest="$1" cur="J_V"
+	eval "export -- \"$__dest=\${cur}\"; [ -n \"\${cur+x}\" ]"
+}
+
+json_get_index() {
+	local __dest="$1"
+	local cur parent seq
+	_json_get_var cur JSON_CUR
+	_json_get_var parent "U_$cur"
+	if [ "${parent%%[0-9]*}" != "J_A" ]; then
+		[ -n "$_json_no_warning" ] || \
+			echo "WARNING: Not inside an array" >&2
+		return 1
+	fi
+	seq="S_$parent"
+	eval "export -- \"$__dest=\${$seq}\"; [ -n \"\${$seq+x}\" ]"
+}
+
 # functions read access to json variables
 
 json_compact() {