Simplify formats map

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

@ -185,26 +185,24 @@
them later to differentiate schemas according to patterns. them later to differentiate schemas according to patterns.
*/}} */}}
{{/* Use a Scratch to manipulate the map easily. */}} {{/*
{{ $scratch := newScratch }} Construct a map from format ID to the type string of the format.
*/}}
{{ $formatMap := newScratch }}
{{ range $pattern, $schema := .patternProperties }} {{ range $pattern, $schema := .patternProperties }}
{{ $formatId := "string" }} {{ $formatId := or (index $schema "x-pattern-format") "string" }}
{{ with index $schema "x-pattern-format" }}
{{ $formatId = . }}
{{ end }}
{{/* Initialize the map for the format if it doesn't exist. */}} {{ if $formatMap.Get $formatId }}
{{ if not (reflect.IsSlice ($scratch.Get $formatId)) }} {{ errorf "'%s' pattern format is defined more than once for the same property" $formatId }}
{{ $scratch.Set $formatId slice }}
{{ end }} {{ end }}
{{ $scratch.Add $formatId (partial "property-type" $schema) }} {{ $formatMap.Set $formatId (partial "property-type" $schema) }}
{{ end }} {{ end }}
{{/* First generate the type string for each format. */}} {{/* First generate the type string for each format. */}}
{{ $types := slice }} {{ $types := slice }}
{{ range $formatId, $formatTypes := $scratch.Values }} {{ range $formatId, $formatType := $formatMap.Values }}
{{ $formatKey := "string" }} {{ $formatKey := "string" }}
{{ if ne $formatId "string" }} {{ if ne $formatId "string" }}
{{ with index site.Data "custom-formats" $formatId }} {{ with index site.Data "custom-formats" $formatId }}
@ -214,7 +212,7 @@
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ $formatString := printf "{%s: %s}" $formatKey (delimit $formatTypes "|") }} {{ $formatString := printf "{%s: %s}" $formatKey $formatType }}
{{ $types = $types | append $formatString }} {{ $types = $types | append $formatString }}
{{ end }} {{ end }}

Loading…
Cancel
Save