forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
checks.go
64 lines (56 loc) · 1.27 KB
/
checks.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
package actions
import (
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/processors"
)
func configChecked(
constr processors.Constructor,
checks ...func(common.Config) error,
) processors.Constructor {
validator := checkAll(checks...)
return func(c common.Config) (processors.Processor, error) {
err := validator(c)
if err != nil {
return nil, fmt.Errorf("%v in %v", err.Error(), c.Path())
}
return constr(c)
}
}
func checkAll(checks ...func(common.Config) error) func(common.Config) error {
return func(c common.Config) error {
for _, check := range checks {
if err := check(c); err != nil {
return err
}
}
return nil
}
}
func requireFields(fields ...string) func(common.Config) error {
return func(c common.Config) error {
for _, field := range fields {
if !c.HasField(field) {
return fmt.Errorf("missing %v option", field)
}
}
return nil
}
}
func allowedFields(fields ...string) func(common.Config) error {
return func(c common.Config) error {
for _, field := range c.GetFields() {
found := false
for _, allowed := range fields {
if field == allowed {
found = true
break
}
}
if !found {
return fmt.Errorf("unexpected %v option", field)
}
}
return nil
}
}