-
Notifications
You must be signed in to change notification settings - Fork 28
/
violations.go
191 lines (168 loc) · 4.57 KB
/
violations.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package lintutil
import (
"errors"
"fmt"
"github.com/grokify/mogo/type/stringsutil"
)
// PolicyViolationsSets is a container for `openapi3lint` policy violations.
// Common approaches to view violatiosn are to use `PolicyViolationsSets.LocationsByRule()`
// and `PolicyViolationsSets.CountsByRule()`.
type PolicyViolationsSets struct {
ByRule map[string]PolicyViolationsSet
}
func NewPolicyViolationsSets() *PolicyViolationsSets {
return &PolicyViolationsSets{
ByRule: map[string]PolicyViolationsSet{}}
}
func (sets *PolicyViolationsSets) AddViolations(violations []PolicyViolation) {
for _, vio := range violations {
sets.AddViolation(vio)
}
}
func (sets *PolicyViolationsSets) AddViolation(violation PolicyViolation) {
set, ok := sets.ByRule[violation.RuleName]
if !ok {
set = NewPolicyViolationsSet(violation.RuleName)
}
set.Violations = append(set.Violations, violation)
sets.ByRule[violation.RuleName] = set
}
func (sets *PolicyViolationsSets) AddSimple(ruleName, location, value string) {
set, ok := sets.ByRule[ruleName]
if !ok {
set = NewPolicyViolationsSet(ruleName)
}
set.Violations = append(set.Violations,
PolicyViolation{
RuleName: ruleName,
Location: location,
Value: value})
sets.ByRule[ruleName] = set
}
func (sets *PolicyViolationsSets) UpsertSet(upsertSet PolicyViolationsSet) error {
for _, vio := range upsertSet.Violations {
ruleName := vio.RuleName
if len(ruleName) == 0 {
ruleName = upsertSet.RuleName
}
if len(ruleName) == 0 {
return errors.New("violation & violationSet have no RuleName")
}
existingSet, ok := sets.ByRule[upsertSet.RuleName]
if !ok {
sets.ByRule[upsertSet.RuleName] = upsertSet
} else {
existingSet.Violations = append(
existingSet.Violations, vio)
sets.ByRule[ruleName] = existingSet
}
}
return nil
}
func (sets *PolicyViolationsSets) UpsertSets(upsertSets *PolicyViolationsSets) error {
if upsertSets == nil {
return nil
}
for ruleName, upsertSet := range upsertSets.ByRule {
if ruleName != upsertSet.RuleName {
return fmt.Errorf("set name mismatch sets[%s] set[%s]", ruleName, upsertSet.RuleName)
}
err := sets.UpsertSet(upsertSet)
if err != nil {
return err
}
}
return nil
}
func (sets *PolicyViolationsSets) LocationsByRule() ViolationLocationsByRuleSet {
locs := map[string][]string{}
for _, set := range sets.ByRule {
for _, vio := range set.Violations {
ruleName := vio.RuleName
vioLocation := vio.Location
vioValue := vio.Value
if len(vioValue) > 0 {
vioLocation += " [" + vioValue + "]"
}
if _, ok := locs[ruleName]; !ok {
locs[ruleName] = []string{}
}
locs[ruleName] = append(locs[ruleName], vioLocation)
}
}
vlrs := ViolationLocationsByRuleSet{
ViolationLocationsByRule: locs}
vlrs.Condense()
return vlrs
}
func (sets *PolicyViolationsSets) Count() uint {
count := uint(0)
for _, set := range sets.ByRule {
count += set.Count()
}
return count
}
func (sets *PolicyViolationsSets) CountsByRule() map[string]uint {
counts := map[string]uint{}
for _, set := range sets.ByRule {
counts[set.RuleName] = set.Count()
}
return counts
}
type PolicyRule struct {
Name string
StringFormat string
}
type PolicyViolationsSet struct {
RuleName string
Violations []PolicyViolation
}
func NewPolicyViolationsSet(ruleName string) PolicyViolationsSet {
return PolicyViolationsSet{
RuleName: ruleName,
Violations: []PolicyViolation{}}
}
func (set *PolicyViolationsSet) Locations() PolicyViolationLocations {
locations := PolicyViolationLocations{
RuleName: set.RuleName,
Locations: []string{}}
for _, v := range set.Violations {
locations.Locations = append(locations.Locations, v.Location)
}
locations.Finalize()
return locations
}
func (set *PolicyViolationsSet) Count() uint {
return uint(len(set.Violations))
}
type PolicyViolationLocations struct {
RuleName string
Locations []string
}
func (vl *PolicyViolationLocations) Finalize() {
vl.Locations = stringsutil.SliceCondenseSpace(vl.Locations, true, true)
}
type PolicyViolation struct {
RuleName string
RuleType string
Violation string
Value string
Location string
Data map[string]string
}
type ViolationLocationsByRuleSet struct {
ViolationLocationsByRule map[string][]string
}
func (vlrs *ViolationLocationsByRuleSet) Condense() {
for k, vals := range vlrs.ViolationLocationsByRule {
vlrs.ViolationLocationsByRule[k] =
stringsutil.SliceCondenseSpace(vals, true, true)
}
}
func (vlrs *ViolationLocationsByRuleSet) Count() uint {
count := 0
for _, vals := range vlrs.ViolationLocationsByRule {
count += len(vals)
}
return uint(count)
}