diff --git a/modules/home/terminal/hr/hr.sh b/modules/home/terminal/hr/hr.sh index 6066add..32196f7 100644 --- a/modules/home/terminal/hr/hr.sh +++ b/modules/home/terminal/hr/hr.sh @@ -120,6 +120,34 @@ _hr_freeze() { uv pip freeze --exclude-editable >>requirements.txt } +_hr_add_json_field() { + local json="$1" + local key="$2" + local value="$3" + local jq_opt="--arg" + + # Check if explicit boolean + if [[ "$value" == "true" || "$value" == "false" ]]; then + jq_opt="--argjson" + # Check if number (integer or float, no leading zeros unless just 0) + elif [[ "$value" =~ ^-?(0|[1-9][0-9]*)(\.[0-9]+)?$ ]]; then + jq_opt="--argjson" + # Check if object or array + elif [[ "$value" == "["* || "$value" == "{"* ]]; then + if echo "$value" | jq empty >/dev/null 2>&1; then + jq_opt="--argjson" + else + # Warn to stderr, but proceed as string + echo "Warning: Value for '$key' looks like JSON but is invalid. Treating as string." >&2 + fi + fi + + # Apply the value at the path defined by the key (dot-notation supported) + # paths like items.0.id are converted to ["items", 0, "id"] + echo "$json" | jq --arg k "$key" $jq_opt v "$value" \ + 'setpath($k | split(".") | map(if test("^[0-9]+$") then tonumber else . end); $v)' +} + _hr_call() { local route_arg="$1" shift @@ -147,6 +175,11 @@ _hr_call() { project_number="322048751601" fi + if ! command -v jq >/dev/null; then + echo "Error: jq is required but not installed." + return 1 + fi + local json_payload="{}" while [[ $# -gt 0 ]]; do @@ -158,13 +191,7 @@ _hr_call() { fi local value="$2" - # Use jq to safely construct JSON - if command -v jq >/dev/null; then - json_payload=$(echo "$json_payload" | jq --arg k "$key" --arg v "$value" '.[$k] = $v') - else - echo "Error: jq is required but not installed." - return 1 - fi + json_payload=$(_hr_add_json_field "$json_payload" "$key" "$value") shift 2 else echo "Error: Unexpected argument '$1'" @@ -191,6 +218,11 @@ _hr_cf() { return 1 fi + if ! command -v jq >/dev/null; then + echo "Error: jq is required but not installed." + return 1 + fi + local json_payload="{}" while [[ $# -gt 0 ]]; do @@ -202,13 +234,7 @@ _hr_cf() { fi local value="$2" - # Use jq to safely construct JSON - if command -v jq >/dev/null; then - json_payload=$(echo "$json_payload" | jq --arg k "$key" --arg v "$value" '.[$k] = $v') - else - echo "Error: jq is required but not installed." - return 1 - fi + json_payload=$(_hr_add_json_field "$json_payload" "$key" "$value") shift 2 else echo "Error: Unexpected argument '$1'"