Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit a7c8383

Browse files
authored
Merge pull request #185 from sirmackan/master
fix json merge of config
2 parents 78e9259 + 1c9f4e9 commit a7c8383

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

scripts/configure_valhalla.sh

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,28 @@ if test -f "${CONFIG_FILE}"; then
123123
# create temporary default config
124124
valhalla_build_config > ${TMP_CONFIG_FILE} || exit 1
125125

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

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

133-
# get its value from the temp config
134-
newval=$(jq "${key}" ${TMP_CONFIG_FILE})
135-
echo "INFO: copied new config entry ${key}=${newval} into existing config."
136-
137-
# and set it on the new one
138-
jq --argjson d "${newval}" "${key} = \$d" "${CONFIG_FILE}"| sponge "${CONFIG_FILE}"
139-
fi
140-
done
142+
# add all additions
143+
jq --argjson additions "$additions" '
144+
reduce $additions[] as $a (.;
145+
setpath($a.path; $a.value)
146+
)
147+
' "${CONFIG_FILE}" | sponge "${CONFIG_FILE}"
141148

142149
rm ${TMP_CONFIG_FILE}
143150
fi

tests/test.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/bash
12
set -u
23

34
valhalla_image=$1
@@ -123,7 +124,7 @@ if [[ $? -eq 1 ]]; then
123124
fi
124125

125126
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)
126-
if [[ $line_count != "0" ]]; then
127+
if [[ $line_count -ne 0 ]]; then
127128
echo "Valhalla config was not updated correctly. Check the generated config files."
128129
exit 1
129130
fi
@@ -144,7 +145,7 @@ if [[ $? -eq 0 ]]; then
144145
fi
145146

146147
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)
147-
if [[ $line_count != "1" ]]; then
148+
if [[ $line_count -ne 1 ]]; then
148149
echo "valhalla.json should not have been updated but was"
149150
exit 1
150151
fi

0 commit comments

Comments
 (0)