Skip to content

Commit

Permalink
Merge pull request #78 from harness/FFM-2934
Browse files Browse the repository at this point in the history
FFM-2934 Bugfix - compare custom attributes based on string values
  • Loading branch information
jcox250 committed Apr 5, 2022
2 parents 842ac1a + 625a0bb commit 6bb5f78
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
19 changes: 18 additions & 1 deletion evaluation/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package evaluation
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -83,7 +84,23 @@ func (e Evaluator) evaluateClause(clause *rest.Clause, target *Target) bool {
return false
}

object := attrValue.String()
object := ""
switch attrValue.Kind() {
case reflect.Int, reflect.Int64:
object = strconv.FormatInt(attrValue.Int(), 10)
case reflect.Bool:
object = strconv.FormatBool(attrValue.Bool())
case reflect.String:
object = attrValue.String()
case reflect.Array, reflect.Chan, reflect.Complex128, reflect.Complex64, reflect.Func, reflect.Interface,
reflect.Invalid, reflect.Ptr, reflect.Slice, reflect.Struct, reflect.Uintptr, reflect.UnsafePointer,
reflect.Float32, reflect.Float64, reflect.Int16, reflect.Int32, reflect.Int8, reflect.Map, reflect.Uint,
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
object = fmt.Sprintf("%v", object)
default:
// Use string formatting as last ditch effort for any unexpected values
object = fmt.Sprintf("%v", object)
}

switch operator {
case startsWithOperator:
Expand Down
77 changes: 70 additions & 7 deletions evaluation/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evaluation

import (
"reflect"
"strconv"
"testing"

"github.com/harness/ff-golang-server-sdk/rest"
Expand Down Expand Up @@ -54,9 +55,10 @@ func Test_getAttrValue(t *testing.T) {
attr string
}
tests := []struct {
name string
args args
want reflect.Value
name string
args args
want reflect.Value
wantStr string
}{
{
name: "check identifier",
Expand All @@ -66,7 +68,8 @@ func Test_getAttrValue(t *testing.T) {
},
attr: identifier,
},
want: reflect.ValueOf(harness),
want: reflect.ValueOf(harness),
wantStr: "harness",
},
{
name: "check name",
Expand All @@ -76,7 +79,8 @@ func Test_getAttrValue(t *testing.T) {
},
attr: "name",
},
want: reflect.ValueOf(harness),
want: reflect.ValueOf(harness),
wantStr: "harness",
},
{
name: "check attributes",
Expand All @@ -89,14 +93,73 @@ func Test_getAttrValue(t *testing.T) {
},
attr: "email",
},
want: reflect.ValueOf(email),
want: reflect.ValueOf(email),
wantStr: email,
},
{
name: "check integer attributes",
args: args{
target: &Target{
Identifier: identifier,
Attributes: &map[string]interface{}{
"age": 123,
},
},
attr: "age",
},
want: reflect.ValueOf(123),
wantStr: "123",
},
{
name: "check int64 attributes",
args: args{
target: &Target{
Identifier: identifier,
Attributes: &map[string]interface{}{
"age": int64(123),
},
},
attr: "age",
},
want: reflect.ValueOf(int64(123)),
wantStr: "123",
},
{
name: "check boolean attributes",
args: args{
target: &Target{
Identifier: identifier,
Attributes: &map[string]interface{}{
"active": true,
},
},
attr: "active",
},
want: reflect.ValueOf(true),
wantStr: "true",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getAttrValue(tt.args.target, tt.args.attr); !reflect.DeepEqual(got.Interface(), tt.want.Interface()) {
got := getAttrValue(tt.args.target, tt.args.attr)
if !reflect.DeepEqual(got.Interface(), tt.want.Interface()) {
t.Errorf("getAttrValue() = %v, want %v", got, tt.want)
}

expObjString := ""
//nolint
switch got.Kind() {
case reflect.Int, reflect.Int64:
expObjString = strconv.FormatInt(got.Int(), 10)
case reflect.Bool:
expObjString = strconv.FormatBool(got.Bool())
case reflect.String:
expObjString = got.String()
}

if expObjString != tt.wantStr {
t.Errorf("getAttrValue() expObjString= %v, want %v", got.String(), tt.wantStr)
}
})
}
}
Expand Down

0 comments on commit 6bb5f78

Please sign in to comment.