Skip to content

Commit 2a4130a

Browse files
Merge pull request #108 from harness/FFM-6384-number-target-attribute-matching
(FFM-6384) Match target attributes to numbers
2 parents f75fa8f + 1140e36 commit 2a4130a

File tree

3 files changed

+107
-18
lines changed

3 files changed

+107
-18
lines changed

evaluation/evaluator.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package evaluation
33
import (
44
"encoding/json"
55
"fmt"
6-
"reflect"
76
"regexp"
87
"sort"
98
"strconv"
@@ -98,23 +97,7 @@ func (e Evaluator) evaluateClause(clause *rest.Clause, target *Target) bool {
9897
return false
9998
}
10099

101-
object := ""
102-
switch attrValue.Kind() {
103-
case reflect.Int, reflect.Int64:
104-
object = strconv.FormatInt(attrValue.Int(), 10)
105-
case reflect.Bool:
106-
object = strconv.FormatBool(attrValue.Bool())
107-
case reflect.String:
108-
object = attrValue.String()
109-
case reflect.Array, reflect.Chan, reflect.Complex128, reflect.Complex64, reflect.Func, reflect.Interface,
110-
reflect.Invalid, reflect.Ptr, reflect.Slice, reflect.Struct, reflect.Uintptr, reflect.UnsafePointer,
111-
reflect.Float32, reflect.Float64, reflect.Int16, reflect.Int32, reflect.Int8, reflect.Map, reflect.Uint,
112-
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
113-
object = fmt.Sprintf("%v", object)
114-
default:
115-
// Use string formatting as last ditch effort for any unexpected values
116-
object = fmt.Sprintf("%v", object)
117-
}
100+
object := reflectValueToString(attrValue)
118101

119102
switch operator {
120103
case startsWithOperator:

evaluation/util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package evaluation
33
import (
44
"fmt"
55
"reflect"
6+
"strconv"
67
"strings"
78

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

41+
func reflectValueToString(val reflect.Value) string {
42+
stringValue := ""
43+
switch val.Kind() {
44+
case reflect.Int, reflect.Int64:
45+
stringValue = strconv.FormatInt(val.Int(), 10)
46+
case reflect.Bool:
47+
stringValue = strconv.FormatBool(val.Bool())
48+
case reflect.String:
49+
stringValue = val.String()
50+
case reflect.Array, reflect.Chan, reflect.Complex128, reflect.Complex64, reflect.Func, reflect.Interface,
51+
reflect.Invalid, reflect.Ptr, reflect.Slice, reflect.Struct, reflect.Uintptr, reflect.UnsafePointer,
52+
reflect.Float32, reflect.Float64, reflect.Int16, reflect.Int32, reflect.Int8, reflect.Map, reflect.Uint,
53+
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
54+
stringValue = fmt.Sprintf("%v", val)
55+
default:
56+
// Use string formatting as last ditch effort for any unexpected values
57+
stringValue = fmt.Sprintf("%v", val)
58+
}
59+
return stringValue
60+
}
61+
4062
func findVariation(variations []rest.Variation, identifier string) (rest.Variation, error) {
4163
for _, variation := range variations {
4264
if variation.Identifier == identifier {

evaluation/util_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,90 @@ func Test_getAttrValueIsNil(t *testing.T) {
4848
}
4949
}
5050

51+
func Test_reflectValueToString(t *testing.T) {
52+
type args struct {
53+
attr interface{}
54+
}
55+
tests := []struct {
56+
name string
57+
args args
58+
want string
59+
}{
60+
{
61+
name: "string value",
62+
args: args{
63+
64+
},
65+
66+
},
67+
{
68+
name: "int value",
69+
args: args{
70+
attr: 20,
71+
},
72+
want: "20",
73+
},
74+
{
75+
name: "int64 value",
76+
args: args{
77+
attr: int64(20),
78+
},
79+
want: "20",
80+
},
81+
{
82+
name: "float32 value",
83+
args: args{
84+
attr: float32(20),
85+
},
86+
want: "20",
87+
},
88+
{
89+
name: "float32 digit value",
90+
args: args{
91+
attr: float32(20.5678),
92+
},
93+
want: "20.5678",
94+
},
95+
{
96+
name: "float64 value",
97+
args: args{
98+
attr: float64(20),
99+
},
100+
want: "20",
101+
},
102+
{
103+
name: "float64 digit value",
104+
args: args{
105+
attr: float64(20.5678),
106+
},
107+
want: "20.5678",
108+
},
109+
{
110+
name: "bool true value",
111+
args: args{
112+
attr: true,
113+
},
114+
want: "true",
115+
},
116+
{
117+
name: "bool false value",
118+
args: args{
119+
attr: false,
120+
},
121+
want: "false",
122+
},
123+
}
124+
for _, tt := range tests {
125+
t.Run(tt.name, func(t *testing.T) {
126+
value := reflect.ValueOf(tt.args.attr)
127+
got := reflectValueToString(value)
128+
if got != tt.want {
129+
t.Errorf("valueToString() = %v, want %v", got, tt.want)
130+
}
131+
})
132+
}
133+
}
134+
51135
func Test_getAttrValue(t *testing.T) {
52136
email := "[email protected]"
53137
type args struct {

0 commit comments

Comments
 (0)