Skip to content

Commit ab9e6e6

Browse files
committed
fixed operator evaluation not passing tests
1 parent 5bbbf0d commit ab9e6e6

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

correlation/cache/operators.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ import (
1212

1313
func inCIDR(addr, network string) (bool, error) {
1414
_, subnet, err := net.ParseCIDR(network)
15-
if err == nil {
16-
ip := net.ParseIP(addr)
17-
if ip != nil {
18-
if subnet.Contains(ip) {
19-
return true, nil
20-
}
21-
}
15+
if err != nil {
16+
return false, err
17+
}
18+
ip := net.ParseIP(addr)
19+
if ip == nil {
2220
return false, fmt.Errorf("invalid IP address")
2321
}
24-
return false, err
22+
return subnet.Contains(ip), nil
2523
}
2624

2725
func equal(val1, val2 string) bool {
@@ -56,12 +54,10 @@ func endWith(str, suff string) bool {
5654

5755
func expresion(exp, str string) (bool, error) {
5856
re, err := regexp.Compile(exp)
59-
if err == nil {
60-
if re.MatchString(str) {
61-
return true, nil
62-
}
57+
if err != nil {
58+
return false, err
6359
}
64-
return false, err
60+
return re.MatchString(str), nil
6561
}
6662

6763
func parseFloats(val1, val2 string) (float64, float64, error) {
@@ -115,7 +111,7 @@ func compare(operator, val1, val2 string) bool {
115111
if err != nil {
116112
return false
117113
}
118-
return matched
114+
return !matched
119115
case "<":
120116
f1, f2, err := parseFloats(val1, val2)
121117
if err != nil {
@@ -160,8 +156,10 @@ func compare(operator, val1, val2 string) bool {
160156
}
161157

162158
func evalElement(elem, field, operator, value string) bool {
163-
if gjson.Get(elem, field).Exists() {
164-
return compare(operator, gjson.Get(elem, field).String(), value)
159+
if elem := gjson.Get(elem, field); elem.Exists() {
160+
raw := elem
161+
fmt.Printf("raw: %s\n", raw)
162+
return compare(operator, raw.String(), value)
165163
} else if operator == "not exist" {
166164
return true
167165
}

0 commit comments

Comments
 (0)