github.com/StevenACoffman/golangci-lint@v1.10.1/pkg/result/processors/max_same_issues.go (about)

     1  package processors
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/golangci/golangci-lint/pkg/logutils"
     7  	"github.com/golangci/golangci-lint/pkg/result"
     8  )
     9  
    10  type textToCountMap map[string]int
    11  
    12  type MaxSameIssues struct {
    13  	tc    textToCountMap
    14  	limit int
    15  	log   logutils.Log
    16  }
    17  
    18  var _ Processor = &MaxSameIssues{}
    19  
    20  func NewMaxSameIssues(limit int, log logutils.Log) *MaxSameIssues {
    21  	return &MaxSameIssues{
    22  		tc:    textToCountMap{},
    23  		limit: limit,
    24  		log:   log,
    25  	}
    26  }
    27  
    28  func (MaxSameIssues) Name() string {
    29  	return "max_same_issues"
    30  }
    31  
    32  func (p *MaxSameIssues) Process(issues []result.Issue) ([]result.Issue, error) {
    33  	if p.limit <= 0 { // no limit
    34  		return issues, nil
    35  	}
    36  
    37  	return filterIssues(issues, func(i *result.Issue) bool {
    38  		p.tc[i.Text]++ // always inc for stat
    39  		return p.tc[i.Text] <= p.limit
    40  	}), nil
    41  }
    42  
    43  func (p MaxSameIssues) Finish() {
    44  	walkStringToIntMapSortedByValue(p.tc, func(text string, count int) {
    45  		if count > p.limit {
    46  			p.log.Infof("%d/%d issues with text %q were hidden, use --max-same-issues",
    47  				count-p.limit, count, text)
    48  		}
    49  	})
    50  }
    51  
    52  type kv struct {
    53  	Key   string
    54  	Value int
    55  }
    56  
    57  func walkStringToIntMapSortedByValue(m map[string]int, walk func(k string, v int)) {
    58  	var ss []kv
    59  	for k, v := range m {
    60  		ss = append(ss, kv{
    61  			Key:   k,
    62  			Value: v,
    63  		})
    64  	}
    65  
    66  	sort.Slice(ss, func(i, j int) bool {
    67  		return ss[i].Value > ss[j].Value
    68  	})
    69  
    70  	for _, kv := range ss {
    71  		walk(kv.Key, kv.Value)
    72  	}
    73  }