-
Notifications
You must be signed in to change notification settings - Fork 1
/
rule.go
40 lines (34 loc) · 863 Bytes
/
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
package hexa
import "context"
// WrapRule wraps the Rule and returns a RuleWithContext.
func WrapRule(r Rule) RuleWithContext {
return func(_ context.Context) error {
return r()
}
}
// Rule is a rule signature.
type Rule func() error
type RuleWithContext func(ctx context.Context) error
// Validate validates the provided rules and returns first broken
// rule's error. otherwise it returns nil.
func Validate(rules ...Rule) error {
for _, r := range rules {
if err := r(); err != nil {
return err
}
}
return nil
}
// ValidateWithContext validates rules with a context.
func ValidateWithContext(ctx context.Context, rules ...RuleWithContext) error {
for _, r := range rules {
if err := r(ctx); err != nil {
return err
}
// We can check if context is done.
//if err := ctx.Err(); err != nil {
// return nil
//}
}
return nil
}