-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule.go
50 lines (45 loc) · 1.12 KB
/
rule.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
package nettrigger
import (
"fmt"
)
// A Rule defines zero or more triggers and one or more actions.
type Rule struct {
Filters []Filter
Actions []Action
}
// Match returns true if the rule's filters match the environment.
func (r Rule) Match(env Environment) bool {
for _, filter := range r.Filters {
if !filter(env) {
return false
}
}
return true
}
// BuildRule uses fb and ab to construct a rule from the specification.
func BuildRule(spec RuleSpec, fb []FilterBuilder, ab []ActionBuilder) (Rule, error) {
filters, err := BuildFilters(spec.Filters, fb...)
if err != nil {
return Rule{}, err
}
actions, err := BuildActions(spec.Actions, ab...)
if err != nil {
return Rule{}, err
}
return Rule{
Filters: filters,
Actions: actions,
}, nil
}
// BuildRules converts the given specifications into a rule list.
func BuildRules(specs []RuleSpec, fb []FilterBuilder, ab []ActionBuilder) ([]Rule, error) {
var rules []Rule
for i, r := range specs {
rule, err := BuildRule(r, fb, ab)
if err != nil {
return nil, fmt.Errorf("bad rule #%d: %v", i, err)
}
rules = append(rules, rule)
}
return rules, nil
}