This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
86 lines (72 loc) · 1.91 KB
/
list.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
package maperr
import (
"go.uber.org/multierr"
)
// PairErrors holds a pair of errorPairs
type PairErrors struct {
err Error
match Error
}
// ListMapper maps not hashable or formatted errorPairs
type ListMapper struct {
errorPairs []PairErrors
}
// NewListMapper return a new ListMapper
func NewListMapper() ListMapper {
return ListMapper{}
}
// Appendf append a format to error association
func (lm ListMapper) Appendf(format string, match error) ListMapper {
return lm.Append(Errorf(format), castError(match))
}
// Append append an error to error association
func (lm ListMapper) Append(err, match error) ListMapper {
lm.errorPairs = append(lm.errorPairs,
PairErrors{
err: castError(err),
match: castError(match),
})
return lm
}
// mapErr a formatted error to an error
func (lm ListMapper) mapErr(err error) mapResult {
errorsToMap := []error{
err,
}
if errList := multierr.Errors(err); len(errList) > 0 {
errorsToMap = errList
}
for i := len(errorsToMap) - 1; i >= 0; i-- {
comparableErr := castError(errorsToMap[i])
for k := range lm.errorPairs {
if comparableErr.Equal(lm.errorPairs[k].err) {
return newAppendStrategy(err, lm.errorPairs[k].match)
}
}
}
return nil
}
// appendStrategy holds data for an ignore strategy
type appendStrategy struct {
previousErr error
lastErr error
}
// newAppendStrategy instantiates a new appendStrategy
func newAppendStrategy(previous, last error) appendStrategy {
return appendStrategy{previousErr: previous, lastErr: last}
}
// previous returns the error that we want to append to
func (as appendStrategy) previous() error {
return as.previousErr
}
// last returns the error that we are appending
func (as appendStrategy) last() error {
return as.lastErr
}
// apply the append strategy by appending previousErr to lastErr
func (as appendStrategy) apply() error {
if as.lastErr == nil {
return nil
}
return Append(as.previousErr, as.lastErr)
}