Refactor to simplify

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
pull/1787/head
Kévin Commaille 2 months ago
parent 36f0b86bca
commit 5e6871dd24
No known key found for this signature in database
GPG Key ID: 29A48C1F03620416

@ -16,45 +16,32 @@
{{ $ret := . }} {{ $ret := . }}
{{ $original := . }} {{ $original := . }}
{{ if reflect.IsSlice $ret }} {{ if reflect.IsSlice $original }}
{{/* {{/*
If it's a slice, just recurse. If it's a slice, just recurse.
*/}} */}}
{{ $result_slice := slice }} {{ $ret = slice }}
{{ range $ret }} {{ range $original }}
{{ $resolved := partial "json-schema/resolve-allof" . }} {{ $resolved := partial "json-schema/resolve-allof" . }}
{{ $result_slice = $result_slice | append $resolved }} {{ $ret = $ret | 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 }}
{{ end }} {{ end }}
{{ else if reflect.IsMap $original }}
{{ $ret = dict }}
{{/* {{/*
We special-case 'required', and accumulate the values from all the 'allOf' We special-case 'required', and accumulate the values from all the 'allOf'
entries (rather than simply overriding them). Start the accumulation here. entries (rather than simply overriding them). Start the accumulation here.
*/}} */}}
{{ $required := slice }} {{ $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 Merge each of the allOf entries.
turn.
*/}} */}}
{{ $all_of_values := dict }}
{{ range . }} {{ range . }}
{{/* {{/*
First, resolve allOf in child. First, resolve allOf in child.
@ -72,23 +59,22 @@
Note also that `merge` does a *deep* merge - nested maps are also Note also that `merge` does a *deep* merge - nested maps are also
merged. (Slices are replaced though.) merged. (Slices are replaced though.)
*/}} */}}
{{ $all_of_values = merge $all_of_values $resolved }} {{ $ret = merge $ret $resolved }}
{{ end }} {{ end }}
{{ end }}
{{/* {{/*
Finally, merge in the original, allowing the original to override allOf. Finally, merge in the original, allowing the original to override allOf.
*/}} */}}
{{ $ret = merge $all_of_values $ret }} {{ range $key, $value := $original }}
{{ if and (ne $key "allOf") (ne $key "required") }}
{{/* {{ $resolved := partial "json-schema/resolve-allof" $value }}
special-case 'required': replace it with the union of all the {{ $ret = merge $ret (dict $key $resolved) }}
'required' arrays from the original and allOf values.
*/}}
{{ with $ret.required }}
{{ $required = union $required $ret.required }}
{{ end }} {{ end }}
{{ end }}
{{ $ret = merge $ret (dict "required" $required) }} {{ with $required }}
{{ $ret = merge $ret (dict "required" .) }}
{{ end }} {{ end }}
{{ end }} {{ end }}

Loading…
Cancel
Save