This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ruleset.go
121 lines (99 loc) · 2.76 KB
/
ruleset.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package regula
import (
"encoding/json"
"errors"
"github.com/heetch/regula/rule"
)
// A Ruleset is list of rules that must return the same type.
type Ruleset struct {
Rules []*rule.Rule `json:"rules"`
Type string `json:"type"`
}
// NewStringRuleset creates a ruleset which rules all return a string otherwise
// ErrRulesetIncoherentType is returned.
func NewStringRuleset(rules ...*rule.Rule) (*Ruleset, error) {
return newRuleset("string", rules...)
}
// NewBoolRuleset creates a ruleset which rules all return a bool otherwise
// ErrRulesetIncoherentType is returned.
func NewBoolRuleset(rules ...*rule.Rule) (*Ruleset, error) {
return newRuleset("bool", rules...)
}
// NewInt64Ruleset creates a ruleset which rules all return an int64 otherwise
// ErrRulesetIncoherentType is returned.
func NewInt64Ruleset(rules ...*rule.Rule) (*Ruleset, error) {
return newRuleset("int64", rules...)
}
// NewFloat64Ruleset creates a ruleset which rules all return an float64 otherwise
// ErrRulesetIncoherentType is returned.
func NewFloat64Ruleset(rules ...*rule.Rule) (*Ruleset, error) {
return newRuleset("float64", rules...)
}
func newRuleset(typ string, rules ...*rule.Rule) (*Ruleset, error) {
rs := Ruleset{
Rules: rules,
Type: typ,
}
err := rs.validate()
if err != nil {
return nil, err
}
return &rs, nil
}
// Eval evaluates every rule of the ruleset until one matches.
// It returns rule.ErrNoMatch if no rule matches the given context.
func (r *Ruleset) Eval(params rule.Params) (*rule.Value, error) {
for _, rl := range r.Rules {
res, err := rl.Eval(params)
if err != rule.ErrNoMatch {
return res, err
}
}
return nil, rule.ErrNoMatch
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (r *Ruleset) UnmarshalJSON(data []byte) error {
type ruleset Ruleset
if err := json.Unmarshal(data, (*ruleset)(r)); err != nil {
return err
}
if r.Type != "string" && r.Type != "bool" && r.Type != "int64" && r.Type != "float64" {
return errors.New("unsupported ruleset type")
}
return r.validate()
}
// Params returns a list of all the parameters used in all the underlying rules.
func (r *Ruleset) Params() []rule.Param {
bm := make(map[string]bool)
var params []rule.Param
for _, rl := range r.Rules {
ps := rl.Params()
for _, p := range ps {
if !bm[p.Name] {
params = append(params, p)
bm[p.Name] = true
}
}
}
return params
}
func (r *Ruleset) validate() error {
paramTypes := make(map[string]string)
for _, rl := range r.Rules {
if rl.Result.Type != r.Type {
return ErrRulesetIncoherentType
}
ps := rl.Params()
for _, p := range ps {
tp, ok := paramTypes[p.Name]
if ok {
if p.Type != tp {
return ErrRulesetIncoherentType
}
} else {
paramTypes[p.Name] = p.Type
}
}
}
return nil
}