Skip to content

Commit 71aa099

Browse files
author
Thomas G.
committed
feat: allow json validation caching
1 parent 0c50b65 commit 71aa099

6 files changed

Lines changed: 53 additions & 37 deletions

File tree

src/common-utils/_configure-feature.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ for package in package composer; do
9292

9393
# Post merge normalize package.json
9494
zz_log i "Post-merge normalize {U $package.json}"
95-
normalize-json -s -a -i -t ${tabSize:-4} $package.json
95+
normalize-json -c -w -a -i -t ${tabSize:-4} $package.json
9696
done
9797

9898
# Call all configure-xxx.sh scripts

src/common-utils/_normalize-json.sh

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,32 @@ set -e
88
# Function to print help and manage arguments
99
eval $(
1010
zz_args "Normalize JSON according to schema" $0 "$@" <<-help
11-
s - save save normalized json to original file
11+
w - write write normalized json to original file
1212
t tabSize tabSize tab size for indentation
13+
c - cache allow caching of schema validation map
1314
a - allow allow additional properties at root level
1415
d - debug debug output
1516
f fallback fallback fallback schema to use if none found locally
1617
l local local infer schema in <local> folder from json file name (x.y.json => <local>/y.schema.json)
1718
i - import infer on schema store if nothing found locally (x.y.json => "y" on schema store)
18-
- json json json to normalize
19-
+ schema schema schema to use for normalization
19+
s schema schema schema to use for normalization
20+
+ files files jsons to normalize
21+
2022
help
2123
)
2224

23-
# Validate JSON
24-
zz_log i "Normalizing JSON..."
25-
list=$(validate-json ${allow:+-a} ${debug:+-d} ${fallback:+-f "$fallback"} ${local:+-l "$local"} ${import:+-i} "$json" "$schema")
25+
for file in $files; do
26+
27+
# Validate JSON
28+
zz_log i "Normalizing {U $file}..."
29+
list=$(validate-json ${allow:+-a} ${cache:+-c} ${debug:+-d} ${fallback:+-f "$fallback"} ${local:+-l "$local"} ${import:+-i} ${schema:+-s"$schema"} $file)
2630

27-
if test -z "$list"; then
28-
zz_log e "JSON {U $json} not valid, cannot normalize" && exit 1
29-
fi
31+
if test -z "$list"; then
32+
zz_log e "JSON {U $file} not valid, cannot normalize" && exit 1
33+
fi
3034

31-
# Normalize JSON
32-
zz_json $json | jq -r --arg list "$list" '
35+
# Normalize JSON
36+
zz_json $file | jq -r --arg list "$list" '
3337
def transform($lst):
3438
$lst | split("\n")
3539
| map(select(length > 0))
@@ -54,17 +58,18 @@ zz_json $json | jq -r --arg list "$list" '
5458
5559
traverse(transform($list))' >/tmp/$$.json
5660

57-
# Handle output
58-
if test -s /tmp/$$.json; then
59-
if test -z "$save"; then
60-
jq -C --indent ${tabSize:-2} . /tmp/$$.json
61+
# Handle output
62+
if test -s /tmp/$$.json; then
63+
if test -z "$write"; then
64+
jq -C --indent ${tabSize:-2} . /tmp/$$.json
65+
else
66+
jq -M --indent ${tabSize:-4} . /tmp/$$.json >$file
67+
fi
68+
zz_log s "File {U $file} normalized"
6169
else
62-
jq -M --indent ${tabSize:-4} . /tmp/$$.json >$json
70+
zz_log e "File {U $file} not normalized"
6371
fi
64-
zz_log s "File {U $json} normalized"
65-
else
66-
zz_log e "File {U $json} not normalized"
67-
fi
6872

69-
# Clean up
70-
rm -f /tmp/$$.*
73+
# Clean up
74+
rm -f /tmp/$$.*
75+
done

src/common-utils/_validate-json.sh

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ eval $(
88
zz_args "Validate JSON according to schema" $0 "$@" <<-help
99
a - allow allow additional properties at root level
1010
d - debug debug output
11+
c - cache allow caching
1112
f fallback fallback fallback schema to use if none found locally
1213
l local local infer schema in <local> folder from json file name (x.y.json => <local>/y.schema.json). Use "true" to use script folder
1314
i - import infer on schema store if nothing found locally (x.y.json => "y" on schema store)
14-
- json json json to normalize
15-
+ schema schema schema to use for normalization
15+
s schema schema schema to use to validate json
16+
- json json json to validate
1617
help
1718
)
1819

@@ -455,7 +456,7 @@ validate() {
455456
}
456457

457458
# if local flag is set, get schema from json file name
458-
if [ -n "$local" ]; then
459+
if [ -n "$local" ] && [ -z "$schema" ]; then
459460

460461
# Identify package type from file name just before json extension
461462
type=$(basename -s .json $json | sed -E 's/.*\.(.*)/\1/')
@@ -472,7 +473,7 @@ if [ -n "$local" ]; then
472473
fi
473474

474475
# Check if schema file exists, and if not and import allowed, download it from schema store
475-
if [ -n "$import" ] && [ ! -f "$schema" ]; then
476+
if [ -n "$import" ] && [ -z "$schema" ]; then
476477

477478
search=$(basename -s .json $json | sed -E 's/.*\.(.*)/\1/').json
478479

@@ -521,9 +522,19 @@ if test -n "$allow"; then
521522
schema=$(echo "$schema" | jq '. + {"additionalProperties": true}' -)
522523
fi
523524

524-
# Validate JSON according to schema and display valid json paths
525-
if validate "$json" "$schema"; then
526-
zz_log s "File {U $json} valid"
525+
# is cache flag is set, cache schema
526+
hash=$(echo "$schema$json" | md5sum | awk '{print $1}')
527+
map=~/.cache/$hash.schema.map
528+
zz_log i "Hash is {B $hash}"
529+
530+
if test -n "$cache" && test -s $map; then
531+
zz_log i "Using cached validation map"
532+
cat $map
527533
else
528-
zz_log e "File {U $json} empty or invalid" && exit 1
529-
fi | sed -n -e 's/^.//g' -e '/^$/d' -e 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
534+
# Validate JSON according to schema and display valid json paths
535+
if validate "$json" "$schema"; then
536+
zz_log s "File {U $json} valid"
537+
else
538+
zz_log e "File {U $json} empty or invalid" && exit 1
539+
fi | sed -n -e 's/^.//g' -e '/^$/d' -e 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | tee $map
540+
fi

src/common-utils/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Common Utils",
3-
"version": "3.4.3",
3+
"version": "3.5.0",
44
"description": "Common utils for tomgrv/devcontainer-features",
55
"dependsOn": {
66
"ghcr.io/devcontainers/features/common-utils": {}

src/githooks/_lint-staged.package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"npx --yes prettier --write"
1111
],
1212
"*.json": [
13-
"normalize-json -s -a -i -t 4 -l true"
13+
"normalize-json -c -w -a -i -t 4 -l true"
1414
]
1515
}
16-
}
16+
}

src/githooks/devcontainer-feature.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "githooks",
33
"name": "Git Hooks",
44
"description": "A feature to add useful Git hooks to your project",
5-
"version": "5.6.0",
5+
"version": "5.7.0",
66
"dependsOn": {
77
"ghcr.io/devcontainers/features/node": {},
88
"ghcr.io/tomgrv/devcontainer-features/common-utils:3": {},
@@ -27,4 +27,4 @@
2727
"postCreateCommand": {
2828
"config": "configure-feature githooks"
2929
}
30-
}
30+
}

0 commit comments

Comments
 (0)