From 5e6871dd2498d0b0daedfb12d560d14c22f41b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Tue, 16 Apr 2024 20:36:47 +0200 Subject: [PATCH] Refactor to simplify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kévin Commaille --- .../partials/json-schema/resolve-allof.html | 60 +++++++------------ 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/layouts/partials/json-schema/resolve-allof.html b/layouts/partials/json-schema/resolve-allof.html index a40f2e99..b8b81e7d 100644 --- a/layouts/partials/json-schema/resolve-allof.html +++ b/layouts/partials/json-schema/resolve-allof.html @@ -16,45 +16,32 @@ {{ $ret := . }} {{ $original := . }} -{{ if reflect.IsSlice $ret }} +{{ if reflect.IsSlice $original }} {{/* If it's a slice, just recurse. */}} - {{ $result_slice := slice }} + {{ $ret = slice }} - {{ range $ret }} + {{ range $original }} {{ $resolved := partial "json-schema/resolve-allof" . }} - {{ $result_slice = $result_slice | append $resolved }} - {{ end }} - - {{ $ret = $result_slice }} -{{ end }} - -{{ if reflect.IsMap $ret }} - {{/* - First, we recurse into the other keys. Doing it now avoids to recurse - twice in keys resolved by allOf values. - */}} - {{ range $key, $value := $ret }} - {{ if ne $key "allOf" }} - {{ $resolved := partial "json-schema/resolve-allof" $value }} - {{ $ret = merge $ret (dict $key $resolved) }} - {{ end }} + {{ $ret = $ret | append $resolved }} {{ end }} +{{ else if reflect.IsMap $original }} + {{ $ret = dict }} {{/* We special-case 'required', and accumulate the values from all the 'allOf' entries (rather than simply overriding them). Start the accumulation here. */}} {{ $required := slice }} + {{ with $original.required }} + {{ $required = . }} + {{ end }} - {{ with $ret.allOf }} - + {{ with $original.allOf }} {{/* - construct a new dict, with each of the allOf entries merged into it in - turn. + Merge each of the allOf entries. */}} - {{ $all_of_values := dict }} {{ range . }} {{/* First, resolve allOf in child. @@ -72,23 +59,22 @@ Note also that `merge` does a *deep* merge - nested maps are also merged. (Slices are replaced though.) */}} - {{ $all_of_values = merge $all_of_values $resolved }} + {{ $ret = merge $ret $resolved }} {{ end }} + {{ end }} - {{/* - Finally, merge in the original, allowing the original to override allOf. - */}} - {{ $ret = merge $all_of_values $ret }} - - {{/* - special-case 'required': replace it with the union of all the - 'required' arrays from the original and allOf values. - */}} - {{ with $ret.required }} - {{ $required = union $required $ret.required }} + {{/* + Finally, merge in the original, allowing the original to override allOf. + */}} + {{ range $key, $value := $original }} + {{ if and (ne $key "allOf") (ne $key "required") }} + {{ $resolved := partial "json-schema/resolve-allof" $value }} + {{ $ret = merge $ret (dict $key $resolved) }} {{ end }} + {{ end }} - {{ $ret = merge $ret (dict "required" $required) }} + {{ with $required }} + {{ $ret = merge $ret (dict "required" .) }} {{ end }} {{ end }}