Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFM-2934 Bugfix - compare custom attributes based on string values #78

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be slower than conv, Sprintf accept interface and it will do one pass more, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea it will but in the interest of not having to write a specific conversion case for all these unexpected types I just defaulted to using fmt.Sprintf. The only reason this case is actually really in here instead of letting them all fallthrough to the default was to please the linter.

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