@@ -3,11 +3,10 @@ package evaluation
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
-
7
- "github.com/drone/ff-golang-server-sdk/types"
8
-
9
6
"reflect"
10
7
"strconv"
8
+
9
+ "github.com/drone/ff-golang-server-sdk/types"
11
10
)
12
11
13
12
const (
@@ -43,26 +42,34 @@ type Clause struct {
43
42
44
43
// Evaluate clause using target but it can be used also with segments if Op field is segmentMach
45
44
func (c * Clause ) Evaluate (target * Target , segments Segments , operator types.ValueType ) bool {
46
- switch c .Op {
47
- case segmentMatchOperator :
45
+
46
+ // Special case - segment matcher doesn't require a
47
+ // valid operator.
48
+ if c .Op == segmentMatchOperator {
48
49
if segments == nil {
49
50
return false
50
51
}
51
52
return segments .Evaluate (target )
52
- case inOperator :
53
- return operator .In (c .Value )
54
- case equalOperator :
55
- return operator .Equal (c .Value )
56
- case gtOperator :
57
- return operator .GreaterThan (c .Value )
58
- case startsWithOperator :
59
- return operator .StartsWith (c .Value )
60
- case endsWithOperator :
61
- return operator .EndsWith (c .Value )
62
- case containsOperator :
63
- return operator .Contains (c .Value )
64
- case equalSensitiveOperator :
65
- return operator .EqualSensitive (c .Value )
53
+ }
54
+
55
+ // Ensure operator is valid and not nil
56
+ if operator != nil {
57
+ switch c .Op {
58
+ case inOperator :
59
+ return operator .In (c .Value )
60
+ case equalOperator :
61
+ return operator .Equal (c .Value )
62
+ case gtOperator :
63
+ return operator .GreaterThan (c .Value )
64
+ case startsWithOperator :
65
+ return operator .StartsWith (c .Value )
66
+ case endsWithOperator :
67
+ return operator .EndsWith (c .Value )
68
+ case containsOperator :
69
+ return operator .Contains (c .Value )
70
+ case equalSensitiveOperator :
71
+ return operator .EqualSensitive (c .Value )
72
+ }
66
73
}
67
74
return false
68
75
}
@@ -71,11 +78,16 @@ func (c *Clause) Evaluate(target *Target, segments Segments, operator types.Valu
71
78
type Clauses []Clause
72
79
73
80
// Evaluate clauses using target but it can be used also with segments if Op field is segmentMach
81
+ // TODO this func can return false because of an error. We need a way to indicate to the caller if this is false
82
+ // because it evaluated false, or because it actually failed to work.
74
83
func (c Clauses ) Evaluate (target * Target , segments Segments ) bool {
75
84
// AND operation
76
85
for _ , clause := range c {
77
86
// operator should be evaluated based on type of attribute
78
- op := target .GetOperator (clause .Attribute )
87
+ op , err := target .GetOperator (clause .Attribute )
88
+ if err != nil {
89
+ fmt .Print (err )
90
+ }
79
91
if ! clause .Evaluate (target , segments , op ) {
80
92
return false
81
93
}
0 commit comments