Skip to content

Commit

Permalink
Merge pull request #108 from harness/FFM-6384-number-target-attribute…
Browse files Browse the repository at this point in the history
…-matching

(FFM-6384) Match target attributes to numbers
  • Loading branch information
conormurray95 authored Jan 25, 2023
2 parents f75fa8f + 1140e36 commit 2a4130a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 18 deletions.
19 changes: 1 addition & 18 deletions evaluation/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package evaluation
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -98,23 +97,7 @@ func (e Evaluator) evaluateClause(clause *rest.Clause, target *Target) bool {
return false
}

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)
}
object := reflectValueToString(attrValue)

switch operator {
case startsWithOperator:
Expand Down
22 changes: 22 additions & 0 deletions evaluation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package evaluation
import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/harness/ff-golang-server-sdk/log"
Expand Down Expand Up @@ -37,6 +38,27 @@ func getAttrValue(target *Target, attr string) reflect.Value {
return value
}

func reflectValueToString(val reflect.Value) string {
stringValue := ""
switch val.Kind() {
case reflect.Int, reflect.Int64:
stringValue = strconv.FormatInt(val.Int(), 10)
case reflect.Bool:
stringValue = strconv.FormatBool(val.Bool())
case reflect.String:
stringValue = val.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:
stringValue = fmt.Sprintf("%v", val)
default:
// Use string formatting as last ditch effort for any unexpected values
stringValue = fmt.Sprintf("%v", val)
}
return stringValue
}

func findVariation(variations []rest.Variation, identifier string) (rest.Variation, error) {
for _, variation := range variations {
if variation.Identifier == identifier {
Expand Down
84 changes: 84 additions & 0 deletions evaluation/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,90 @@ func Test_getAttrValueIsNil(t *testing.T) {
}
}

func Test_reflectValueToString(t *testing.T) {
type args struct {
attr interface{}
}
tests := []struct {
name string
args args
want string
}{
{
name: "string value",
args: args{
attr: "[email protected]",
},
want: "[email protected]",
},
{
name: "int value",
args: args{
attr: 20,
},
want: "20",
},
{
name: "int64 value",
args: args{
attr: int64(20),
},
want: "20",
},
{
name: "float32 value",
args: args{
attr: float32(20),
},
want: "20",
},
{
name: "float32 digit value",
args: args{
attr: float32(20.5678),
},
want: "20.5678",
},
{
name: "float64 value",
args: args{
attr: float64(20),
},
want: "20",
},
{
name: "float64 digit value",
args: args{
attr: float64(20.5678),
},
want: "20.5678",
},
{
name: "bool true value",
args: args{
attr: true,
},
want: "true",
},
{
name: "bool false value",
args: args{
attr: false,
},
want: "false",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value := reflect.ValueOf(tt.args.attr)
got := reflectValueToString(value)
if got != tt.want {
t.Errorf("valueToString() = %v, want %v", got, tt.want)
}
})
}
}

func Test_getAttrValue(t *testing.T) {
email := "[email protected]"
type args struct {
Expand Down

0 comments on commit 2a4130a

Please sign in to comment.