-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditions.go
161 lines (146 loc) · 3.59 KB
/
conditions.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package gorev
import (
"fmt"
"regexp"
"strings"
)
type Comparison string
var NotEqual = Comparison("ne")
var Equal = Comparison("eq")
var Match = Comparison("m")
type Condition struct {
And []Condition
Or []Condition
Xor []Condition
Key string
Value interface{}
Comparison
Motive string
}
func (c *Condition) motiveFormatted() string{
if c.Motive == "" {
return ""
}
return fmt.Sprintf(" [motive: %s]", c.Motive)
}
func (c *Condition) Describe() (n string){
if c.Key != "" {
n += c.Key
if c.Value != nil {
comp := c.Comparison
if comp == "" {
comp = Equal
}
n += fmt.Sprintf(" -%s '%v'", comp, c.Value)
}
n += c.motiveFormatted()
return
}
n += " ("
first := true
for _, cond := range c.And {
if !first {
n += " &&"
} else {
first = false
}
n += " " + cond.Describe()
}
for _, cond := range c.Xor {
if !first {
n += " ⊕"
} else {
first = false
}
n += " " + cond.Describe()
}
for _, cond := range c.Or {
if !first {
n += " ||"
} else {
first = false
}
n += " " + cond.Describe()
}
n += " )"
n += c.motiveFormatted()
return
}
func ValidateParamConditions(params map[string]interface{}, condition Condition) error {
comp := condition.Comparison
if comp == "" {
comp = Equal
}
if condition.Key != "" {
v, ok := params[condition.Key]
if comp != NotEqual {
if !ok || v == nil {
return fmt.Errorf("missing param: %s", condition.Key)
}
if condition.Value != nil {
if comp == Equal && condition.Value != v{
return fmt.Errorf("param '%s' did not match expected '%v' (%v)", condition.Key, condition.Value, v)
} else if comp == Match{
matched, err := regexp.Match(condition.Value.(string), []byte(v.(string)))
if err != nil {
return fmt.Errorf("invalid regex pattern '%v' for condition '%s'", condition.Value, condition.Key)
} else if !matched {
return fmt.Errorf("param '%s' did not match expected pattern '%v' (%v)", condition.Key, condition.Value, v)
}
}
}
switch t := v.(type) {
case string:
if t == "" {
return fmt.Errorf("empty/missing param '%s'", condition.Key)
}
}
} else {
if condition.Value == nil && ok {
return fmt.Errorf("parameter '%s' was specified when not expected", condition.Key)
} else if condition.Value != nil && ok {
if condition.Value == v {
return fmt.Errorf("parameter '%s' value '%v' matched '%v' when inequality expected", condition.Key, v, condition.Value)
}
}
}
}
// And
for _, c := range condition.And {
if err := ValidateParamConditions(params, c); err != nil {
return err
}
}
// Xor
var found []string
var missing []string
var errs []string
for _, c := range condition.Xor {
if err := ValidateParamConditions(params, c); err != nil {
errs = append(errs, err.Error())
missing = append(missing, c.Describe())
} else {
found = append(found, c.Describe())
}
if len(found) > 1 {
return fmt.Errorf("2+ XOR param(s): %s%s", strings.Join(found, ", "), c.motiveFormatted())
}
}
if len(condition.Xor) > 0 && len(found) < 1 {
return fmt.Errorf("invalid/missing XOR param(s): %s\n\tREASON:\n\t%s\n", strings.Join(missing, ", "), strings.Join(errs, "\n\t"))
}
// Or
missing = []string{}
for _, c := range condition.Or {
if err := ValidateParamConditions(params, c); err == nil {
return nil
} else {
errs = append(errs, err.Error())
}
missing = append(missing, c.Describe())
}
if len(condition.Or) > 1 {
return fmt.Errorf("invalid/missing OR param(s): %s\n\tREASON:\n\t%s\n", strings.Join(missing, ", "), strings.Join(errs, "\n\t"))
}
return nil
}