Skip to content

Commit 905f4b7

Browse files
committed
chore: use nesting instead of slice
1 parent 6963aef commit 905f4b7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

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

+15-7
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().AddNext(byLine().AddNext(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
}
@@ -58,7 +58,7 @@ func (sr SortResults) Process(issues []result.Issue) ([]result.Issue, error) {
5858
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
}
@@ -203,12 +203,20 @@ func mergeComparators(cmps []comparator) (comparator, error) {
203203
}
204204

205205
for i := 0; i < len(cmps)-1; i++ {
206-
cmps[i].AddNext(cmps[i+1])
206+
findComparatorTip(cmps[i]).AddNext(cmps[i+1])
207207
}
208208

209209
return cmps[0], nil
210210
}
211211

212+
func findComparatorTip(cmp comparator) comparator {
213+
if cmp.Next() != nil {
214+
return findComparatorTip(cmp.Next())
215+
}
216+
217+
return cmp
218+
}
219+
212220
func severityCompare(a, b string) compareResult {
213221
// The position inside the slice define the importance (lower to higher).
214222
classic := []string{"low", "medium", "high", "warning", "error"}

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

+5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ func Test_mergeComparators(t *testing.T) {
254254
cmps: []comparator{bySeverity(), byLinter(), byFileName(), byLine(), byColumn()},
255255
expected: "bySeverity > byLinter > byFileName > byLine > byColumn",
256256
},
257+
{
258+
desc: "nested",
259+
cmps: []comparator{bySeverity(), byFileName().AddNext(byLine().AddNext(byColumn())), byLinter()},
260+
expected: "bySeverity > byFileName > byLine > byColumn > byLinter",
261+
},
257262
{
258263
desc: "all reverse",
259264
cmps: []comparator{byColumn(), byLine(), byFileName(), byLinter(), bySeverity()},

0 commit comments

Comments
 (0)