Simplify formats map

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
pull/1796/head
Kévin Commaille 3 weeks 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.
*/}}
{{/* 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 }}
{{ $formatId := "string" }}
{{ with index $schema "x-pattern-format" }}
{{ $formatId = . }}
{{ end }}
{{ $formatId := or (index $schema "x-pattern-format") "string" }}
{{/* Initialize the map for the format if it doesn't exist. */}}
{{ if not (reflect.IsSlice ($scratch.Get $formatId)) }}
{{ $scratch.Set $formatId slice }}
{{ if $formatMap.Get $formatId }}
{{ errorf "'%s' pattern format is defined more than once for the same property" $formatId }}
{{ end }}
{{ $scratch.Add $formatId (partial "property-type" $schema) }}
{{ $formatMap.Set $formatId (partial "property-type" $schema) }}
{{ end }}
{{/* First generate the type string for each format. */}}
{{ $types := slice }}
{{ range $formatId, $formatTypes := $scratch.Values }}
{{ range $formatId, $formatType := $formatMap.Values }}
{{ $formatKey := "string" }}
{{ if ne $formatId "string" }}
{{ with index site.Data "custom-formats" $formatId }}
@ -214,7 +212,7 @@
{{ end }}
{{ end }}
{{ $formatString := printf "{%s: %s}" $formatKey (delimit $formatTypes "|") }}
{{ $formatString := printf "{%s: %s}" $formatKey $formatType }}
{{ $types = $types | append $formatString }}
{{ end }}

Loading…
Cancel
Save