Skip to content

Commit 9ca1866

Browse files
committed
libubox: Add getters for the cursor and its parent
JSON might not be generated serial if it's synthesized from another source such as UCI with a different organizational structure. In that case, being able to randomly access arrays and objects in the synthetic JSON simplifies passes over the source material. Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
1 parent 7928f17 commit 9ca1866

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

sh/jshn.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,28 @@ json_get_index() {
211211
eval "export -- \"$__dest=\${$__seq}\"; [ -n \"\${$__seq+x}\" ]"
212212
}
213213

214+
json_get_position() {
215+
local __dest="$1"
216+
eval "export -- \"$__dest=\${JSON_CUR}\"; [ -n \"\${JSON_CUR+x}\" ]"
217+
}
218+
219+
json_move_to() {
220+
local __cur="$1"
221+
_json_set_var JSON_CUR "$cur"
222+
}
223+
224+
json_get_parent_position() {
225+
local __dest="$1" __cur __parent
226+
_json_get_var __cur JSON_CUR
227+
__parent="U_$__cur"
228+
eval "export -- \"$__dest=\${$__parent}\"; [ -n \"\${$__parent+x}\" ]"
229+
}
230+
231+
json_get_root_position() {
232+
local __dest="$1" __cur="J_V"
233+
eval "export -- \"$__dest=\${__cur}\"; [ -n \"\${__cur+x}\" ]"
234+
}
235+
214236
# functions read access to json variables
215237

216238
json_compact() {

tests/shunit2/tests.sh

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,124 @@ test_jshn_get_index() {
510510
set -u
511511
}
512512

513+
test_jshn_get_root_position() {
514+
JSON_PREFIX="${JSON_PREFIX:-}"
515+
. ../../sh/jshn.sh
516+
517+
# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
518+
set +u
519+
520+
local root
521+
522+
# Test getting the root position
523+
json_init
524+
json_add_object "obj"
525+
json_add_array "arr"
526+
json_get_root_position root
527+
assertEquals "J_V" "$root"
528+
529+
set -u
530+
}
531+
532+
test_jshn_get_parent_position() {
533+
JSON_PREFIX="${JSON_PREFIX:-}"
534+
. ../../sh/jshn.sh
535+
536+
# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
537+
set +u
538+
539+
local cur
540+
541+
json_init
542+
543+
json_add_object "obj"
544+
json_add_array "arr"
545+
546+
local obj="$cur"
547+
548+
json_get_parent_position cur
549+
assertEquals "J_T1" "$cur"
550+
551+
set -u
552+
}
553+
554+
test_jshn_get_position() {
555+
JSON_PREFIX="${JSON_PREFIX:-}"
556+
. ../../sh/jshn.sh
557+
558+
# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
559+
set +u
560+
561+
local cur
562+
563+
# Test getting the root position
564+
json_init
565+
json_get_position cur
566+
assertEquals "J_V" "$cur"
567+
568+
local root="$cur"
569+
570+
# ... and the object position
571+
json_add_object "obj"
572+
json_get_position cur
573+
assertEquals "J_T1" "$cur"
574+
575+
local obj="$cur"
576+
577+
# ... and the array position
578+
json_add_array "arr"
579+
json_get_position cur
580+
assertEquals "J_A2" "$cur"
581+
582+
local arr="$cur"
583+
584+
# ... still within the array
585+
json_add_string "first" "one"
586+
json_get_position cur
587+
assertEquals "J_A2" "$cur"
588+
589+
# ... still within the array
590+
json_add_string "second" "two"
591+
json_get_position cur
592+
assertEquals "J_A2" "$cur"
593+
594+
# ... now back to the object
595+
json_select ..
596+
json_get_position cur
597+
assertEquals "$obj" "$cur"
598+
599+
# ... and at the root
600+
json_select ..
601+
json_get_position cur
602+
assertEquals "$root" "$cur"
603+
604+
set -u
605+
}
606+
607+
test_jshn_move_to() {
608+
JSON_PREFIX="${JSON_PREFIX:-}"
609+
. ../../sh/jshn.sh
610+
611+
# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
612+
set +u
613+
614+
local cur cur2
615+
616+
json_init
617+
json_add_object "obj"
618+
json_get_position cur
619+
620+
json_add_array "arr"
621+
json_add_string "first" "one"
622+
json_add_string "second" "two"
623+
624+
json_move_to "$cur"
625+
626+
json_get_position cur2
627+
628+
assertEquals "J_T1" "$cur2"
629+
630+
set -u
631+
}
632+
513633
. ./shunit2/shunit2

0 commit comments

Comments
 (0)