Skip to content

Commit 51a963f

Browse files
authored
chore: simplify comparators (#4499)
1 parent 78d2118 commit 51a963f

File tree

2 files changed

+85
-150
lines changed

2 files changed

+85
-150
lines changed

Diff for: pkg/result/processors/sort_results.go

+62-128
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ const (
2525
var _ Processor = (*SortResults)(nil)
2626

2727
type SortResults struct {
28-
cmps map[string][]comparator
28+
cmps map[string]*comparator
2929

3030
cfg *config.Output
3131
}
3232

3333
func NewSortResults(cfg *config.Config) *SortResults {
3434
return &SortResults{
35-
cmps: map[string][]comparator{
35+
cmps: map[string]*comparator{
3636
// For sorting we are comparing (in next order):
3737
// file names, line numbers, position, and finally - giving up.
38-
orderNameFile: {&byFileName{}, &byLine{}, &byColumn{}},
38+
orderNameFile: byFileName().SetNext(byLine().SetNext(byColumn())),
3939
// For sorting we are comparing: linter name
40-
orderNameLinter: {&byLinter{}},
40+
orderNameLinter: byLinter(),
4141
// For sorting we are comparing: severity
42-
orderNameSeverity: {&bySeverity{}},
42+
orderNameSeverity: bySeverity(),
4343
},
4444
cfg: &cfg.Output,
4545
}
@@ -55,10 +55,10 @@ func (sr SortResults) Process(issues []result.Issue) ([]result.Issue, error) {
5555
sr.cfg.SortOrder = []string{orderNameFile}
5656
}
5757

58-
var cmps []comparator
58+
var cmps []*comparator
5959
for _, name := range sr.cfg.SortOrder {
6060
if c, ok := sr.cmps[name]; ok {
61-
cmps = append(cmps, c...)
61+
cmps = append(cmps, c)
6262
} else {
6363
return nil, fmt.Errorf("unsupported sort-order name %q", name)
6464
}
@@ -108,58 +108,30 @@ func (c compareResult) String() string {
108108
}
109109

110110
// 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
116115
}
117116

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 }
129118

130-
func (cmp *byFileName) AddNext(c comparator) comparator {
119+
func (cmp *comparator) SetNext(c *comparator) *comparator {
131120
cmp.next = c
132121
return cmp
133122
}
134123

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()
139128
}
140129

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
159131
}
160132

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)
163135
if !res.isNeutral() {
164136
return res
165137
}
@@ -171,100 +143,71 @@ func (cmp *byLine) Compare(a, b *result.Issue) compareResult {
171143
return res
172144
}
173145

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+
},
195152
}
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
211153
}
212154

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+
},
221161
}
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
237162
}
238163

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+
},
243170
}
171+
}
244172

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+
},
247179
}
248-
249-
return res
250180
}
251181

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+
}
254189
}
255190

256-
func mergeComparators(cmps []comparator) (comparator, error) {
191+
func mergeComparators(cmps []*comparator) (*comparator, error) {
257192
if len(cmps) == 0 {
258193
return nil, errors.New("no comparator")
259194
}
260195

261196
for i := 0; i < len(cmps)-1; i++ {
262-
cmps[i].AddNext(cmps[i+1])
197+
findComparatorTip(cmps[i]).SetNext(cmps[i+1])
263198
}
264199

265200
return cmps[0], nil
266201
}
267202

203+
func findComparatorTip(cmp *comparator) *comparator {
204+
if cmp.Next() != nil {
205+
return findComparatorTip(cmp.Next())
206+
}
207+
208+
return cmp
209+
}
210+
268211
func severityCompare(a, b string) compareResult {
269212
// The position inside the slice define the importance (lower to higher).
270213
classic := []string{"low", "medium", "high", "warning", "error"}
@@ -313,12 +256,3 @@ func numericCompare(a, b int) compareResult {
313256

314257
return equal
315258
}
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

Comments
 (0)