Skip to content

Commit

Permalink
Merge pull request #185 from sirmackan/master
Browse files Browse the repository at this point in the history
fix json merge of config
  • Loading branch information
chrstnbwnkl authored Feb 18, 2025
2 parents 78e9259 + 1c9f4e9 commit a7c8383
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
33 changes: 20 additions & 13 deletions scripts/configure_valhalla.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,28 @@ if test -f "${CONFIG_FILE}"; then
# create temporary default config
valhalla_build_config > ${TMP_CONFIG_FILE} || exit 1

# for each path in the temp config (excluding array indices)
jq -r 'paths | select(map(type) | index("number") | not ) | "." + join(".")' ${TMP_CONFIG_FILE} | while read key ; do
# collect additions to make from the temp config (treating arrays as atomic values)
additions=$(jq --slurpfile A "${TMP_CONFIG_FILE}" --slurpfile B "${CONFIG_FILE}" -n '
def push(p;k): p+[k];
def missing(a;b;p):
if a|type=="object" then
reduce (a|keys_unsorted[]) as $k ([];
.+(
if b|has($k)|not then [{path:push(p;$k), dot:(push(p;$k)|join(".")), value:a[$k]}]
elif (a[$k]|type=="object") and (b[$k]|type=="object") then missing(a[$k];b[$k];push(p;$k))
else [] end))
else [] end;
missing($A[0];$B[0];[])
')

# if the key path does not exist in the existing config
jq -e "${key} | if type == \"null\" then false else true end" ${CONFIG_FILE} >/dev/null
if [ $? -eq 1 ]; then
echo "$additions" | jq -r '.[] | "added \(.dot) with value \(.value)"'

# get its value from the temp config
newval=$(jq "${key}" ${TMP_CONFIG_FILE})
echo "INFO: copied new config entry ${key}=${newval} into existing config."

# and set it on the new one
jq --argjson d "${newval}" "${key} = \$d" "${CONFIG_FILE}"| sponge "${CONFIG_FILE}"
fi
done
# add all additions
jq --argjson additions "$additions" '
reduce $additions[] as $a (.;
setpath($a.path; $a.value)
)
' "${CONFIG_FILE}" | sponge "${CONFIG_FILE}"

rm ${TMP_CONFIG_FILE}
fi
Expand Down
5 changes: 3 additions & 2 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
set -u

valhalla_image=$1
Expand Down Expand Up @@ -123,7 +124,7 @@ if [[ $? -eq 1 ]]; then
fi

line_count=$(diff -y --suppress-common-lines <(jq --sort-keys . "${custom_file_folder}/valhalla_base.json") <(jq --sort-keys . "${custom_file_folder}/valhalla.json") | wc -l)
if [[ $line_count != "0" ]]; then
if [[ $line_count -ne 0 ]]; then
echo "Valhalla config was not updated correctly. Check the generated config files."
exit 1
fi
Expand All @@ -144,7 +145,7 @@ if [[ $? -eq 0 ]]; then
fi

line_count=$(diff -y --suppress-common-lines <(jq --sort-keys . "${custom_file_folder}/valhalla_base.json") <(jq --sort-keys . "${custom_file_folder}/valhalla.json") | wc -l)
if [[ $line_count != "1" ]]; then
if [[ $line_count -ne 1 ]]; then
echo "valhalla.json should not have been updated but was"
exit 1
fi
Expand Down

0 comments on commit a7c8383

Please sign in to comment.