Skip to content

Commit

Permalink
chore: remove useless interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 13, 2024
1 parent 905f4b7 commit f84c4a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
53 changes: 22 additions & 31 deletions pkg/result/processors/sort_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const (
var _ Processor = (*SortResults)(nil)

type SortResults struct {
cmps map[string]comparator
cmps map[string]*comparator

cfg *config.Output
}

func NewSortResults(cfg *config.Config) *SortResults {
return &SortResults{
cmps: map[string]comparator{
cmps: map[string]*comparator{
// For sorting we are comparing (in next order):
// file names, line numbers, position, and finally - giving up.
orderNameFile: byFileName().AddNext(byLine().AddNext(byColumn())),
Expand All @@ -55,7 +55,7 @@ func (sr SortResults) Process(issues []result.Issue) ([]result.Issue, error) {
sr.cfg.SortOrder = []string{orderNameFile}
}

var cmps []comparator
var cmps []*comparator
for _, name := range sr.cfg.SortOrder {
if c, ok := sr.cmps[name]; ok {
cmps = append(cmps, c)
Expand Down Expand Up @@ -107,30 +107,21 @@ func (c compareResult) String() string {
}
}

// comparator describes how to implement compare for two "issues".
type comparator interface {
Compare(a, b *result.Issue) compareResult
Next() comparator
AddNext(comparator) comparator
fmt.Stringer
}

var _ comparator = (*baseComparator)(nil)

type baseComparator struct {
// baseComparator describes how to implement compare for two "issues".
type comparator struct {
name string
compare func(a, b *result.Issue) compareResult
next comparator
next *comparator
}

func (cmp *baseComparator) Next() comparator { return cmp.next }
func (cmp *comparator) Next() *comparator { return cmp.next }

func (cmp *baseComparator) AddNext(c comparator) comparator {
func (cmp *comparator) AddNext(c *comparator) *comparator {
cmp.next = c
return cmp
}

func (cmp *baseComparator) String() string {
func (cmp *comparator) String() string {
s := cmp.name
if cmp.Next() != nil {
s += " > " + cmp.Next().String()
Expand All @@ -139,7 +130,7 @@ func (cmp *baseComparator) String() string {
return s
}

func (cmp *baseComparator) Compare(a, b *result.Issue) compareResult {
func (cmp *comparator) Compare(a, b *result.Issue) compareResult {
res := cmp.compare(a, b)
if !res.isNeutral() {
return res
Expand All @@ -152,52 +143,52 @@ func (cmp *baseComparator) Compare(a, b *result.Issue) compareResult {
return res
}

func byFileName() *baseComparator {
return &baseComparator{
func byFileName() *comparator {
return &comparator{
name: "byFileName",
compare: func(a, b *result.Issue) compareResult {
return compareResult(strings.Compare(a.FilePath(), b.FilePath()))
},
}
}

func byLine() *baseComparator {
return &baseComparator{
func byLine() *comparator {
return &comparator{
name: "byLine",
compare: func(a, b *result.Issue) compareResult {
return numericCompare(a.Line(), b.Line())
},
}
}

func byColumn() *baseComparator {
return &baseComparator{
func byColumn() *comparator {
return &comparator{
name: "byColumn",
compare: func(a, b *result.Issue) compareResult {
return numericCompare(a.Column(), b.Column())
},
}
}

func byLinter() *baseComparator {
return &baseComparator{
func byLinter() *comparator {
return &comparator{
name: "byLinter",
compare: func(a, b *result.Issue) compareResult {
return compareResult(strings.Compare(a.FromLinter, b.FromLinter))
},
}
}

func bySeverity() *baseComparator {
return &baseComparator{
func bySeverity() *comparator {
return &comparator{
name: "bySeverity",
compare: func(a, b *result.Issue) compareResult {
return severityCompare(a.Severity, b.Severity)
},
}
}

func mergeComparators(cmps []comparator) (comparator, error) {
func mergeComparators(cmps []*comparator) (*comparator, error) {
if len(cmps) == 0 {
return nil, errors.New("no comparator")
}
Expand All @@ -209,7 +200,7 @@ func mergeComparators(cmps []comparator) (comparator, error) {
return cmps[0], nil
}

func findComparatorTip(cmp comparator) comparator {
func findComparatorTip(cmp *comparator) *comparator {
if cmp.Next() != nil {
return findComparatorTip(cmp.Next())
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/result/processors/sort_results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type compareTestCase struct {
expected compareResult
}

func testCompareValues(t *testing.T, cmp comparator, name string, tests []compareTestCase) {
func testCompareValues(t *testing.T, cmp *comparator, name string, tests []compareTestCase) {
t.Parallel()

for i := 0; i < len(tests); i++ {
Expand Down Expand Up @@ -236,32 +236,32 @@ func TestSorting(t *testing.T) {
func Test_mergeComparators(t *testing.T) {
testCases := []struct {
desc string
cmps []comparator
cmps []*comparator
expected string
}{
{
desc: "one",
cmps: []comparator{byLinter()},
cmps: []*comparator{byLinter()},
expected: "byLinter",
},
{
desc: "two",
cmps: []comparator{byLinter(), byFileName()},
cmps: []*comparator{byLinter(), byFileName()},
expected: "byLinter > byFileName",
},
{
desc: "all",
cmps: []comparator{bySeverity(), byLinter(), byFileName(), byLine(), byColumn()},
cmps: []*comparator{bySeverity(), byLinter(), byFileName(), byLine(), byColumn()},
expected: "bySeverity > byLinter > byFileName > byLine > byColumn",
},
{
desc: "nested",
cmps: []comparator{bySeverity(), byFileName().AddNext(byLine().AddNext(byColumn())), byLinter()},
cmps: []*comparator{bySeverity(), byFileName().AddNext(byLine().AddNext(byColumn())), byLinter()},
expected: "bySeverity > byFileName > byLine > byColumn > byLinter",
},
{
desc: "all reverse",
cmps: []comparator{byColumn(), byLine(), byFileName(), byLinter(), bySeverity()},
cmps: []*comparator{byColumn(), byLine(), byFileName(), byLinter(), bySeverity()},
expected: "byColumn > byLine > byFileName > byLinter > bySeverity",
},
}
Expand Down

0 comments on commit f84c4a5

Please sign in to comment.