Skip to content

Commit 6bb5f78

Browse files
authored
Merge pull request #78 from harness/FFM-2934
FFM-2934 Bugfix - compare custom attributes based on string values
2 parents 842ac1a + 625a0bb commit 6bb5f78

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

evaluation/evaluator.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package evaluation
33
import (
44
"encoding/json"
55
"fmt"
6+
"reflect"
67
"regexp"
78
"sort"
89
"strconv"
@@ -83,7 +84,23 @@ func (e Evaluator) evaluateClause(clause *rest.Clause, target *Target) bool {
8384
return false
8485
}
8586

86-
object := attrValue.String()
87+
object := ""
88+
switch attrValue.Kind() {
89+
case reflect.Int, reflect.Int64:
90+
object = strconv.FormatInt(attrValue.Int(), 10)
91+
case reflect.Bool:
92+
object = strconv.FormatBool(attrValue.Bool())
93+
case reflect.String:
94+
object = attrValue.String()
95+
case reflect.Array, reflect.Chan, reflect.Complex128, reflect.Complex64, reflect.Func, reflect.Interface,
96+
reflect.Invalid, reflect.Ptr, reflect.Slice, reflect.Struct, reflect.Uintptr, reflect.UnsafePointer,
97+
reflect.Float32, reflect.Float64, reflect.Int16, reflect.Int32, reflect.Int8, reflect.Map, reflect.Uint,
98+
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
99+
object = fmt.Sprintf("%v", object)
100+
default:
101+
// Use string formatting as last ditch effort for any unexpected values
102+
object = fmt.Sprintf("%v", object)
103+
}
87104

88105
switch operator {
89106
case startsWithOperator:

evaluation/util_test.go

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package evaluation
22

33
import (
44
"reflect"
5+
"strconv"
56
"testing"
67

78
"github.com/harness/ff-golang-server-sdk/rest"
@@ -54,9 +55,10 @@ func Test_getAttrValue(t *testing.T) {
5455
attr string
5556
}
5657
tests := []struct {
57-
name string
58-
args args
59-
want reflect.Value
58+
name string
59+
args args
60+
want reflect.Value
61+
wantStr string
6062
}{
6163
{
6264
name: "check identifier",
@@ -66,7 +68,8 @@ func Test_getAttrValue(t *testing.T) {
6668
},
6769
attr: identifier,
6870
},
69-
want: reflect.ValueOf(harness),
71+
want: reflect.ValueOf(harness),
72+
wantStr: "harness",
7073
},
7174
{
7275
name: "check name",
@@ -76,7 +79,8 @@ func Test_getAttrValue(t *testing.T) {
7679
},
7780
attr: "name",
7881
},
79-
want: reflect.ValueOf(harness),
82+
want: reflect.ValueOf(harness),
83+
wantStr: "harness",
8084
},
8185
{
8286
name: "check attributes",
@@ -89,14 +93,73 @@ func Test_getAttrValue(t *testing.T) {
8993
},
9094
attr: "email",
9195
},
92-
want: reflect.ValueOf(email),
96+
want: reflect.ValueOf(email),
97+
wantStr: email,
98+
},
99+
{
100+
name: "check integer attributes",
101+
args: args{
102+
target: &Target{
103+
Identifier: identifier,
104+
Attributes: &map[string]interface{}{
105+
"age": 123,
106+
},
107+
},
108+
attr: "age",
109+
},
110+
want: reflect.ValueOf(123),
111+
wantStr: "123",
112+
},
113+
{
114+
name: "check int64 attributes",
115+
args: args{
116+
target: &Target{
117+
Identifier: identifier,
118+
Attributes: &map[string]interface{}{
119+
"age": int64(123),
120+
},
121+
},
122+
attr: "age",
123+
},
124+
want: reflect.ValueOf(int64(123)),
125+
wantStr: "123",
126+
},
127+
{
128+
name: "check boolean attributes",
129+
args: args{
130+
target: &Target{
131+
Identifier: identifier,
132+
Attributes: &map[string]interface{}{
133+
"active": true,
134+
},
135+
},
136+
attr: "active",
137+
},
138+
want: reflect.ValueOf(true),
139+
wantStr: "true",
93140
},
94141
}
95142
for _, tt := range tests {
96143
t.Run(tt.name, func(t *testing.T) {
97-
if got := getAttrValue(tt.args.target, tt.args.attr); !reflect.DeepEqual(got.Interface(), tt.want.Interface()) {
144+
got := getAttrValue(tt.args.target, tt.args.attr)
145+
if !reflect.DeepEqual(got.Interface(), tt.want.Interface()) {
98146
t.Errorf("getAttrValue() = %v, want %v", got, tt.want)
99147
}
148+
149+
expObjString := ""
150+
//nolint
151+
switch got.Kind() {
152+
case reflect.Int, reflect.Int64:
153+
expObjString = strconv.FormatInt(got.Int(), 10)
154+
case reflect.Bool:
155+
expObjString = strconv.FormatBool(got.Bool())
156+
case reflect.String:
157+
expObjString = got.String()
158+
}
159+
160+
if expObjString != tt.wantStr {
161+
t.Errorf("getAttrValue() expObjString= %v, want %v", got.String(), tt.wantStr)
162+
}
100163
})
101164
}
102165
}

0 commit comments

Comments
 (0)