forked from bufbuild/protoc-gen-validate
/
map.go
44 lines (38 loc) · 1.24 KB
/
map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cc
const mapTpl = `
{{ $f := .Field }}{{ $r := .Rules }}
{{ if $r.GetMinPairs }}
{{ unimplemented }}
{{/*
{{ if eq $r.GetMinPairs $r.GetMaxPairs }}
if len({{ accessor . }}) != {{ $r.GetMinPairs }} {
return {{ err . "value must contain exactly " $r.GetMinPairs " pair(s)" }}
}
{{ else if $r.MaxPairs }}
if l := len({{ accessor . }}); l < {{ $r.GetMinPairs }} || l > {{ $r.GetMaxPairs }} {
return {{ err . "value must contain between " $r.GetMinPairs " and " $r.GetMaxPairs " pairs, inclusive" }}
}
{{ else }}
if len({{ accessor . }}) < {{ $r.GetMinPairs }} {
return {{ err . "value must contain at least " $r.GetMinPairs " pair(s)" }}
}
{{ end }}
{{ else if $r.MaxPairs }}
if len({{ accessor . }}) > {{ $r.GetMaxPairs }} {
return {{ err . "value must contain no more than " $r.GetMaxPairs " pair(s)" }}
}
{{ end }}
{{ if or $r.GetNoSparse (ne (.Elem "" "").Typ "none") (ne (.Key "" "").Typ "none") }}
for key, val := range {{ accessor . }} {
_ = key
{{ if $r.GetNoSparse }}
if val == nil {
return {{ errIdx . "key" "value cannot be sparse, all pairs must be non-nil" }}
}
{{ end }}
{{ render (.Key "key" "key") }}
{{ render (.Elem "val" "key") }}
}
*/}}
{{ end }}
`