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