@@ -25,21 +25,21 @@ const (
25
25
var _ Processor = (* SortResults )(nil )
26
26
27
27
type SortResults struct {
28
- cmps map [string ][] comparator
28
+ cmps map [string ]* comparator
29
29
30
30
cfg * config.Output
31
31
}
32
32
33
33
func NewSortResults (cfg * config.Config ) * SortResults {
34
34
return & SortResults {
35
- cmps : map [string ][] comparator {
35
+ cmps : map [string ]* comparator {
36
36
// For sorting we are comparing (in next order):
37
37
// file names, line numbers, position, and finally - giving up.
38
- orderNameFile : { & byFileName {}, & byLine {}, & byColumn {}} ,
38
+ orderNameFile : byFileName (). SetNext ( byLine (). SetNext ( byColumn ())) ,
39
39
// For sorting we are comparing: linter name
40
- orderNameLinter : { & byLinter {}} ,
40
+ orderNameLinter : byLinter () ,
41
41
// For sorting we are comparing: severity
42
- orderNameSeverity : { & bySeverity {}} ,
42
+ orderNameSeverity : bySeverity () ,
43
43
},
44
44
cfg : & cfg .Output ,
45
45
}
@@ -55,10 +55,10 @@ func (sr SortResults) Process(issues []result.Issue) ([]result.Issue, error) {
55
55
sr .cfg .SortOrder = []string {orderNameFile }
56
56
}
57
57
58
- var cmps []comparator
58
+ var cmps []* comparator
59
59
for _ , name := range sr .cfg .SortOrder {
60
60
if c , ok := sr .cmps [name ]; ok {
61
- cmps = append (cmps , c ... )
61
+ cmps = append (cmps , c )
62
62
} else {
63
63
return nil , fmt .Errorf ("unsupported sort-order name %q" , name )
64
64
}
@@ -108,58 +108,30 @@ func (c compareResult) String() string {
108
108
}
109
109
110
110
// comparator describes how to implement compare for two "issues".
111
- type comparator interface {
112
- Compare (a , b * result.Issue ) compareResult
113
- Next () comparator
114
- AddNext (comparator ) comparator
115
- fmt.Stringer
111
+ type comparator struct {
112
+ name string
113
+ compare func (a , b * result.Issue ) compareResult
114
+ next * comparator
116
115
}
117
116
118
- var (
119
- _ comparator = (* byFileName )(nil )
120
- _ comparator = (* byLine )(nil )
121
- _ comparator = (* byColumn )(nil )
122
- _ comparator = (* byLinter )(nil )
123
- _ comparator = (* bySeverity )(nil )
124
- )
125
-
126
- type byFileName struct { next comparator }
127
-
128
- func (cmp * byFileName ) Next () comparator { return cmp .next }
117
+ func (cmp * comparator ) Next () * comparator { return cmp .next }
129
118
130
- func (cmp * byFileName ) AddNext (c comparator ) comparator {
119
+ func (cmp * comparator ) SetNext (c * comparator ) * comparator {
131
120
cmp .next = c
132
121
return cmp
133
122
}
134
123
135
- func (cmp * byFileName ) Compare ( a , b * result. Issue ) compareResult {
136
- res := compareResult ( strings . Compare ( a . FilePath (), b . FilePath ()))
137
- if ! res . isNeutral () {
138
- return res
124
+ func (cmp * comparator ) String () string {
125
+ s := cmp . name
126
+ if cmp . Next () != nil {
127
+ s += " > " + cmp . Next (). String ()
139
128
}
140
129
141
- if next := cmp .Next (); next != nil {
142
- return next .Compare (a , b )
143
- }
144
-
145
- return res
146
- }
147
-
148
- func (cmp * byFileName ) String () string {
149
- return comparatorToString ("byFileName" , cmp )
150
- }
151
-
152
- type byLine struct { next comparator }
153
-
154
- func (cmp * byLine ) Next () comparator { return cmp .next }
155
-
156
- func (cmp * byLine ) AddNext (c comparator ) comparator {
157
- cmp .next = c
158
- return cmp
130
+ return s
159
131
}
160
132
161
- func (cmp * byLine ) Compare (a , b * result.Issue ) compareResult {
162
- res := numericCompare ( a . Line () , b . Line () )
133
+ func (cmp * comparator ) Compare (a , b * result.Issue ) compareResult {
134
+ res := cmp . compare ( a , b )
163
135
if ! res .isNeutral () {
164
136
return res
165
137
}
@@ -171,100 +143,71 @@ func (cmp *byLine) Compare(a, b *result.Issue) compareResult {
171
143
return res
172
144
}
173
145
174
- func (cmp * byLine ) String () string {
175
- return comparatorToString ("byLine" , cmp )
176
- }
177
-
178
- type byColumn struct { next comparator }
179
-
180
- func (cmp * byColumn ) Next () comparator { return cmp .next }
181
-
182
- func (cmp * byColumn ) AddNext (c comparator ) comparator {
183
- cmp .next = c
184
- return cmp
185
- }
186
-
187
- func (cmp * byColumn ) Compare (a , b * result.Issue ) compareResult {
188
- res := numericCompare (a .Column (), b .Column ())
189
- if ! res .isNeutral () {
190
- return res
191
- }
192
-
193
- if next := cmp .Next (); next != nil {
194
- return next .Compare (a , b )
146
+ func byFileName () * comparator {
147
+ return & comparator {
148
+ name : "byFileName" ,
149
+ compare : func (a , b * result.Issue ) compareResult {
150
+ return compareResult (strings .Compare (a .FilePath (), b .FilePath ()))
151
+ },
195
152
}
196
-
197
- return res
198
- }
199
-
200
- func (cmp * byColumn ) String () string {
201
- return comparatorToString ("byColumn" , cmp )
202
- }
203
-
204
- type byLinter struct { next comparator }
205
-
206
- func (cmp * byLinter ) Next () comparator { return cmp .next }
207
-
208
- func (cmp * byLinter ) AddNext (c comparator ) comparator {
209
- cmp .next = c
210
- return cmp
211
153
}
212
154
213
- func (cmp * byLinter ) Compare (a , b * result.Issue ) compareResult {
214
- res := compareResult (strings .Compare (a .FromLinter , b .FromLinter ))
215
- if ! res .isNeutral () {
216
- return res
217
- }
218
-
219
- if next := cmp .Next (); next != nil {
220
- return next .Compare (a , b )
155
+ func byLine () * comparator {
156
+ return & comparator {
157
+ name : "byLine" ,
158
+ compare : func (a , b * result.Issue ) compareResult {
159
+ return numericCompare (a .Line (), b .Line ())
160
+ },
221
161
}
222
-
223
- return res
224
- }
225
-
226
- func (cmp * byLinter ) String () string {
227
- return comparatorToString ("byLinter" , cmp )
228
- }
229
-
230
- type bySeverity struct { next comparator }
231
-
232
- func (cmp * bySeverity ) Next () comparator { return cmp .next }
233
-
234
- func (cmp * bySeverity ) AddNext (c comparator ) comparator {
235
- cmp .next = c
236
- return cmp
237
162
}
238
163
239
- func (cmp * bySeverity ) Compare (a , b * result.Issue ) compareResult {
240
- res := severityCompare (a .Severity , b .Severity )
241
- if ! res .isNeutral () {
242
- return res
164
+ func byColumn () * comparator {
165
+ return & comparator {
166
+ name : "byColumn" ,
167
+ compare : func (a , b * result.Issue ) compareResult {
168
+ return numericCompare (a .Column (), b .Column ())
169
+ },
243
170
}
171
+ }
244
172
245
- if next := cmp .Next (); next != nil {
246
- return next .Compare (a , b )
173
+ func byLinter () * comparator {
174
+ return & comparator {
175
+ name : "byLinter" ,
176
+ compare : func (a , b * result.Issue ) compareResult {
177
+ return compareResult (strings .Compare (a .FromLinter , b .FromLinter ))
178
+ },
247
179
}
248
-
249
- return res
250
180
}
251
181
252
- func (cmp * bySeverity ) String () string {
253
- return comparatorToString ("bySeverity" , cmp )
182
+ func bySeverity () * comparator {
183
+ return & comparator {
184
+ name : "bySeverity" ,
185
+ compare : func (a , b * result.Issue ) compareResult {
186
+ return severityCompare (a .Severity , b .Severity )
187
+ },
188
+ }
254
189
}
255
190
256
- func mergeComparators (cmps []comparator ) (comparator , error ) {
191
+ func mergeComparators (cmps []* comparator ) (* comparator , error ) {
257
192
if len (cmps ) == 0 {
258
193
return nil , errors .New ("no comparator" )
259
194
}
260
195
261
196
for i := 0 ; i < len (cmps )- 1 ; i ++ {
262
- cmps [i ]. AddNext (cmps [i + 1 ])
197
+ findComparatorTip ( cmps [i ]). SetNext (cmps [i + 1 ])
263
198
}
264
199
265
200
return cmps [0 ], nil
266
201
}
267
202
203
+ func findComparatorTip (cmp * comparator ) * comparator {
204
+ if cmp .Next () != nil {
205
+ return findComparatorTip (cmp .Next ())
206
+ }
207
+
208
+ return cmp
209
+ }
210
+
268
211
func severityCompare (a , b string ) compareResult {
269
212
// The position inside the slice define the importance (lower to higher).
270
213
classic := []string {"low" , "medium" , "high" , "warning" , "error" }
@@ -313,12 +256,3 @@ func numericCompare(a, b int) compareResult {
313
256
314
257
return equal
315
258
}
316
-
317
- func comparatorToString (name string , c comparator ) string {
318
- s := name
319
- if c .Next () != nil {
320
- s += " > " + c .Next ().String ()
321
- }
322
-
323
- return s
324
- }
0 commit comments