-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.go
105 lines (78 loc) · 1.79 KB
/
results.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package golidate
type Results []Result
func (results Results) PassesAll() bool {
for _, result := range results {
if !result.Passes() {
return false
}
}
return true
}
func (results Results) PassesAny() bool {
for _, result := range results {
if result.Passes() {
return true
}
}
return false
}
func (results Results) Failed() Results {
failed := make(Results, 0, len(results))
for _, result := range results {
if !result.Passes() {
failed = append(failed, result)
}
}
return failed
}
func (results Results) Passed() Results {
passed := make(Results, 0, len(results))
for _, result := range results {
if result.Passes() {
passed = append(passed, result)
}
}
return passed
}
func (results Results) Prefixed(prefix string) Results {
if prefix == "" {
return results
}
prefixed := make(Results, len(results))
for i, result := range results {
prefixed[i] = result.Name(prefix + "." + result.Attribute)
}
return prefixed
}
func (results Results) Messages(formatters ...Formatter) []string {
messages := make([]string, len(results))
for i, result := range results {
messages[i] = result.Message
for _, formatter := range formatters {
messages[i] = formatter(messages[i])
}
}
return messages
}
func (results Results) Translate(dictionaries ...Dictionary) Results {
res := make(Results, len(results))
for i, result := range results {
res[i] = result.Translate(dictionaries...)
}
return res
}
func (results Results) Group() Grouped {
group := make(Grouped)
for _, result := range results {
group[result.Attribute] = append(group[result.Attribute], result)
}
return group
}
func (results Results) Has(attribute string) bool {
for _, result := range results {
if result.Attribute == attribute {
return true
}
}
return false
}