-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck.go
More file actions
42 lines (34 loc) · 989 Bytes
/
check.go
File metadata and controls
42 lines (34 loc) · 989 Bytes
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
package diffence
import "io"
// Checker checks diffs for rule violations
type Checker interface {
Check(io.Reader) (Result, error)
}
// DiffChecker checks an io.Reader for matches against the supplied ruleset
type DiffChecker struct {
Rules *[]Rule
Ignorer Matcher
}
// Check is a clean syntax but memory inefficient
// method for finding diffs that match the supplied rules
// (use an array instead of a map for better performance)
func (dc DiffChecker) Check(r io.Reader) (Result, error) {
res := Result{
Matched: false,
MatchedRules: make(map[string][]Rule),
}
diff := Diff{ignorer: dc.Ignorer}
err := SplitDiffs(r, &diff)
for _, d := range diff.Items {
for _, r := range *dc.Rules {
if r.Match(d.fPath) {
res.Matched = true
if _, ok := res.MatchedRules[d.GetHashKey()]; !ok {
res.MatchedRules[d.GetHashKey()] = []Rule{}
}
res.MatchedRules[d.GetHashKey()] = append(res.MatchedRules[d.GetHashKey()], r)
}
}
}
return res, err
}