You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
matrix-spec-proposals/layouts/partials/json-schema/resolve-refs.html

66 lines
1.8 KiB
HTML

{{/*
Resolves the `$ref` JSON schema keyword, by recursively replacing
it with the object it points to.
This template uses [`Scratch`](https://gohugo.io/functions/scratch/)
rather than a normal `dict` because with `dict` you can't replace key values:
https://discourse.gohugo.io/t/how-can-i-add-set-and-delete-keys-in-a-dictionary/29661
*/}}
{{ $schema := .schema }}
{{ $path := .path}}
{{ $ret := $schema }}
{{ if reflect.IsMap $schema }}
{{ $scratch := newScratch }}
{{ $scratch.Set "result_map" dict }}
{{ $ref_value := index $schema "$ref"}}
{{ if $ref_value}}
{{ $full_path := path.Join $path $ref_value }}
{{/*
Apparently Hugo doesn't give us a nice way to split the extension off a filename.
*/}}
{{ $without_ext := replaceRE "\\.[^\\.]*$" "" $full_path }}
{{ $pieces := split $without_ext "/" }}
{{ $ref := index site.Data $pieces }}
{{ $new_path := (path.Split $full_path).Dir}}
{{ $result_map := partial "json-schema/resolve-refs" (dict "schema" $ref "path" $new_path)}}
{{ if $result_map}}
{{ $scratch.Set "result_map" $result_map }}
{{end }}
{{ end }}
{{ range $key, $value := $schema }}
{{ if ne $key "$ref" }}
{{ $resolved := partial "json-schema/resolve-refs" (dict "schema" $value "path" $path) }}
{{ $scratch.SetInMap "result_map" $key $resolved }}
{{ end }}
{{ end }}
{{ $ret = $scratch.Get "result_map" }}
{{ end }}
{{ if reflect.IsSlice $schema }}
{{ $result_slice := slice }}
{{ range $schema }}
{{ $resolved := partial "json-schema/resolve-refs" (dict "schema" . "path" $path) }}
{{ $result_slice = $result_slice | append $resolved }}
{{ end }}
{{ $ret = $result_slice }}
{{ end }}
{{ return $ret }}