jshn.sh: add json_add_fields function for adding multiple fields at once
authorFelix Fietkau <nbd@nbd.name>
Tue, 27 Sep 2022 12:17:51 +0000 (14:17 +0200)
committerFelix Fietkau <nbd@nbd.name>
Tue, 27 Sep 2022 12:17:52 +0000 (14:17 +0200)
This simplifies passing extra object data as a function parameter

Signed-off-by: Felix Fietkau <nbd@nbd.name>
sh/jshn.sh

index 7b0155d0ce4c0ec5a4cd8438a1ecf7441f2c4868..c97369230a93d144fbba06b5a8fa79c9acc41238 100644 (file)
@@ -174,6 +174,29 @@ json_add_null() {
        _json_add_generic null "$1" "" "$cur"
 }
 
+json_add_fields() {
+       while [ "$#" -gt 0 ]; do
+               local field="$1"
+               shift
+
+               local name="${field%%=*}"
+               local val="${field#*=}"
+               [ "$name" != "$val" ] || val=""
+
+               local type="${name#*:}"
+               [ "$type" != "$name" ] || type=string
+               name="${name%%:*}"
+
+               case "$type" in
+                       string|int|boolean|double)
+                               local cur
+                               _json_get_var cur JSON_CUR
+                               _json_add_generic "$type" "$name" "$val" "$cur"
+                       ;;
+               esac
+       done
+}
+
 # functions read access to json variables
 
 json_load() {