github.com/mattbailey/reviewdog@v0.10.0/resultmap.go (about) 1 package reviewdog 2 3 import ( 4 "errors" 5 "fmt" 6 "sync" 7 ) 8 9 // ResultMap represents a concurrent-safe map to store CheckResults generated by concurrent jobs. 10 type ResultMap struct { 11 sm sync.Map 12 } 13 14 type Result struct { 15 Level string 16 CheckResults []*CheckResult 17 18 // Optional. Report an error of the command execution. 19 CmdErr error 20 } 21 22 // Store saves a new *Result into ResultMap. 23 func (rm *ResultMap) Store(key string, r *Result) { 24 rm.sm.Store(key, r) 25 } 26 27 // Load fetches *Result from ResultMap 28 func (rm *ResultMap) Load(key string) (*Result, error) { 29 v, ok := rm.sm.Load(key) 30 if !ok { 31 return nil, fmt.Errorf("fail to get the value of key %q from results", key) 32 } 33 34 t, ok := v.(*Result) 35 if !ok { 36 return nil, errors.New("stored type in ResultMap is invalid") 37 } 38 39 return t, nil 40 } 41 42 // Range retrieves `key` and `values` from ResultMap iteratively. 43 func (rm *ResultMap) Range(f func(key string, val *Result)) { 44 rm.sm.Range(func(k, v interface{}) bool { 45 f(k.(string), v.(*Result)) 46 return true 47 }) 48 } 49 50 // Len returns the length of ResultMap count. Len() is not yet officially not supported by Go. (ref: https://github.com/golang/go/issues/20680) 51 func (rm *ResultMap) Len() int { 52 l := 0 53 rm.sm.Range(func(_, _ interface{}) bool { 54 l++ 55 return true 56 }) 57 return l 58 } 59 60 type FilteredResult struct { 61 Level string 62 FilteredCheck []*FilteredCheck 63 } 64 65 // FilteredResultMap represents a concurrent-safe map to store CheckResults generated by concurrent jobs. 66 type FilteredResultMap struct { 67 sm sync.Map 68 } 69 70 // Store saves a new []*FilteredCheckFilteredResult into FilteredResultMap. 71 func (rm *FilteredResultMap) Store(key string, r *FilteredResult) { 72 rm.sm.Store(key, r) 73 } 74 75 // Load fetches FilteredResult from FilteredResultMap 76 func (rm *FilteredResultMap) Load(key string) (*FilteredResult, error) { 77 v, ok := rm.sm.Load(key) 78 if !ok { 79 return nil, fmt.Errorf("fail to get the value of key %q from results", key) 80 } 81 82 t, ok := v.(*FilteredResult) 83 if !ok { 84 return nil, errors.New("stored type in FilteredResultMap is invalid") 85 } 86 87 return t, nil 88 } 89 90 // Range retrieves `key` and `values` from FilteredResultMap iteratively. 91 func (rm *FilteredResultMap) Range(f func(key string, val *FilteredResult)) { 92 rm.sm.Range(func(k, v interface{}) bool { 93 f(k.(string), v.(*FilteredResult)) 94 return true 95 }) 96 } 97 98 // Len returns the length of FilteredResultMap count. Len() is not yet officially not supported by Go. (ref: https://github.com/golang/go/issues/20680) 99 func (rm *FilteredResultMap) Len() int { 100 l := 0 101 rm.sm.Range(func(_, _ interface{}) bool { 102 l++ 103 return true 104 }) 105 return l 106 }