Skip to content

Commit

Permalink
jshn.sh: Add getters for the cursor and its parent
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
  • Loading branch information
pprindeville committed Aug 4, 2024
1 parent 34cb28d commit da01288
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions sh/jshn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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() {
Expand Down

0 comments on commit da01288

Please sign in to comment.