forked from huttotw/grules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule.go
39 lines (28 loc) · 955 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
package grules
import "errors"
type Rule struct {
Path string `json:"path,omitempty"`
Comparer string `json:"comparer,omitempty"`
Value interface{} `json:"value,omitempty"`
Operator Operator `json:"operator,omitempty"`
Rules []Rule `json:"rules,omitempty"`
}
func (r Rule) HasChildren() bool {
return len(r.Rules) > 0
}
func (r Rule) Validate() error {
pathIsSet := r.Path != ""
comparerIsSet := r.Comparer != ""
valueIsSet := r.Value != nil
standardRuleValid := pathIsSet && comparerIsSet && valueIsSet
operatorIsSet := r.Operator != ""
rulesIsSet := len(r.Rules) != 0
multiRuleValid := operatorIsSet && rulesIsSet
if standardRuleValid && multiRuleValid {
return errors.New("setting path, comparer, and value AS WELL AS operator and rules is not valid")
}
if !standardRuleValid && !multiRuleValid {
return errors.New("must set either path, comparer, and value OR operator and rules")
}
return nil
}