-
Notifications
You must be signed in to change notification settings - Fork 0
/
resultmap.go
123 lines (103 loc) · 3.16 KB
/
resultmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package reviewdog
import (
"errors"
"fmt"
"sync"
"github.com/reviewdog/reviewdog/filter"
"github.com/reviewdog/reviewdog/proto/rdf"
)
// ResultMap represents a concurrent-safe map to store Diagnostics generated by concurrent jobs.
type ResultMap struct {
sm sync.Map
}
type Result struct {
Name string
Level string
Diagnostics []*rdf.Diagnostic
// Optional. Report an error of the command execution.
// Non-nil CmdErr doesn't mean failure and Diagnostics still may have
// results.
// It is common that a linter fails with non-zero exit code when it finds
// lint errors.
CmdErr error
}
// CheckUnexpectedFailure returns error on unexpected failure, if any.
func (r *Result) CheckUnexpectedFailure() error {
if r.CmdErr != nil && len(r.Diagnostics) == 0 {
return fmt.Errorf("%s failed with zero findings: The command itself "+
"failed (%v) or reviewdog cannot parse the results", r.Name, r.CmdErr)
}
return nil
}
// Store saves a new *Result into ResultMap.
func (rm *ResultMap) Store(key string, r *Result) {
rm.sm.Store(key, r)
}
// Load fetches *Result from ResultMap
func (rm *ResultMap) Load(key string) (*Result, error) {
v, ok := rm.sm.Load(key)
if !ok {
return nil, fmt.Errorf("fail to get the value of key %q from results", key)
}
t, ok := v.(*Result)
if !ok {
return nil, errors.New("stored type in ResultMap is invalid")
}
return t, nil
}
// Range retrieves `key` and `values` from ResultMap iteratively.
func (rm *ResultMap) Range(f func(key string, val *Result)) {
rm.sm.Range(func(k, v interface{}) bool {
f(k.(string), v.(*Result))
return true
})
}
// Len returns the length of ResultMap count. Len() is not yet officially not supported by Go. (ref: https://github.com/golang/go/issues/20680)
func (rm *ResultMap) Len() int {
l := 0
rm.sm.Range(func(_, _ interface{}) bool {
l++
return true
})
return l
}
type FilteredResult struct {
Level string
FilteredDiagnostic []*filter.FilteredDiagnostic
}
// FilteredResultMap represents a concurrent-safe map to store Diagnostics generated by concurrent jobs.
type FilteredResultMap struct {
sm sync.Map
}
// Store saves a new []*FilteredCheckFilteredResult into FilteredResultMap.
func (rm *FilteredResultMap) Store(key string, r *FilteredResult) {
rm.sm.Store(key, r)
}
// Load fetches FilteredResult from FilteredResultMap
func (rm *FilteredResultMap) Load(key string) (*FilteredResult, error) {
v, ok := rm.sm.Load(key)
if !ok {
return nil, fmt.Errorf("fail to get the value of key %q from results", key)
}
t, ok := v.(*FilteredResult)
if !ok {
return nil, errors.New("stored type in FilteredResultMap is invalid")
}
return t, nil
}
// Range retrieves `key` and `values` from FilteredResultMap iteratively.
func (rm *FilteredResultMap) Range(f func(key string, val *FilteredResult)) {
rm.sm.Range(func(k, v interface{}) bool {
f(k.(string), v.(*FilteredResult))
return true
})
}
// Len returns the length of FilteredResultMap count. Len() is not yet officially not supported by Go. (ref: https://github.com/golang/go/issues/20680)
func (rm *FilteredResultMap) Len() int {
l := 0
rm.sm.Range(func(_, _ interface{}) bool {
l++
return true
})
return l
}