-
Notifications
You must be signed in to change notification settings - Fork 573
/
map.go
71 lines (62 loc) · 2.01 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package goshared
const mapTpl = `
{{ $f := .Field }}{{ $r := .Rules }}
{{ if $r.GetIgnoreEmpty }}
if len({{ accessor . }}) > 0 {
{{ end }}
{{ if $r.GetMinPairs }}
{{ if eq $r.GetMinPairs $r.GetMaxPairs }}
if len({{ accessor . }}) != {{ $r.GetMinPairs }} {
err := {{ err . "value must contain exactly " $r.GetMinPairs " pair(s)" }}
if !all { return err }
errors = append(errors, err)
}
{{ else if $r.MaxPairs }}
if l := len({{ accessor . }}); l < {{ $r.GetMinPairs }} || l > {{ $r.GetMaxPairs }} {
err := {{ err . "value must contain between " $r.GetMinPairs " and " $r.GetMaxPairs " pairs, inclusive" }}
if !all { return err }
errors = append(errors, err)
}
{{ else }}
if len({{ accessor . }}) < {{ $r.GetMinPairs }} {
err := {{ err . "value must contain at least " $r.GetMinPairs " pair(s)" }}
if !all { return err }
errors = append(errors, err)
}
{{ end }}
{{ else if $r.MaxPairs }}
if len({{ accessor . }}) > {{ $r.GetMaxPairs }} {
err := {{ err . "value must contain no more than " $r.GetMaxPairs " pair(s)" }}
if !all { return err }
errors = append(errors, err)
}
{{ end }}
{{ if or $r.GetNoSparse (ne (.Elem "" "").Typ "none") (ne (.Key "" "").Typ "none") }}
{{- /* Sort the keys to make the iteration order (and therefore failure output) deterministic. */ -}}
{
sorted_keys := make([]{{ (typ .Field).Key }}, len({{ accessor . }}))
i := 0
for key := range {{ accessor . }} {
sorted_keys[i] = key
i++
}
sort.Slice(sorted_keys, func (i, j int) bool { return sorted_keys[i] < sorted_keys[j] })
for _, key := range sorted_keys {
val := {{ accessor .}}[key]
_ = val
{{ if $r.GetNoSparse }}
if val == nil {
err := {{ errIdx . "key" "value cannot be sparse, all pairs must be non-nil" }}
if !all { return err }
errors = append(errors, err)
}
{{ end }}
{{ render (.Key "key" "key") }}
{{ render (.Elem "val" "key") }}
}
}
{{ end }}
{{ if $r.GetIgnoreEmpty }}
}
{{ end }}
`