Fix parsing

This commit is contained in:
Sage 2026-01-21 15:18:17 +00:00
parent 64f27448ea
commit 0e65426729

View file

@ -120,6 +120,34 @@ _hr_freeze() {
uv pip freeze --exclude-editable >>requirements.txt 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() { _hr_call() {
local route_arg="$1" local route_arg="$1"
shift shift
@ -147,6 +175,11 @@ _hr_call() {
project_number="322048751601" project_number="322048751601"
fi fi
if ! command -v jq >/dev/null; then
echo "Error: jq is required but not installed."
return 1
fi
local json_payload="{}" local json_payload="{}"
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
@ -158,13 +191,7 @@ _hr_call() {
fi fi
local value="$2" local value="$2"
# Use jq to safely construct JSON json_payload=$(_hr_add_json_field "$json_payload" "$key" "$value")
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
shift 2 shift 2
else else
echo "Error: Unexpected argument '$1'" echo "Error: Unexpected argument '$1'"
@ -191,6 +218,11 @@ _hr_cf() {
return 1 return 1
fi fi
if ! command -v jq >/dev/null; then
echo "Error: jq is required but not installed."
return 1
fi
local json_payload="{}" local json_payload="{}"
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
@ -202,13 +234,7 @@ _hr_cf() {
fi fi
local value="$2" local value="$2"
# Use jq to safely construct JSON json_payload=$(_hr_add_json_field "$json_payload" "$key" "$value")
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
shift 2 shift 2
else else
echo "Error: Unexpected argument '$1'" echo "Error: Unexpected argument '$1'"