Skip to content

Commit 5cd4086

Browse files
authored
Resolves #174, resolves #345 (#349)
1 parent 2134fd5 commit 5cd4086

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

pkg/reflectmath.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
// EvaluateMultiplication will evaluate multiplication operation over two value
2424
func EvaluateMultiplication(left, right reflect.Value) (reflect.Value, error) {
25+
left, right = GetValueElem(left), GetValueElem(right)
2526
switch left.Kind() {
2627
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2728
lv := left.Int()
@@ -75,6 +76,7 @@ func EvaluateMultiplication(left, right reflect.Value) (reflect.Value, error) {
7576

7677
// EvaluateDivision will evaluate division operation over two value
7778
func EvaluateDivision(left, right reflect.Value) (reflect.Value, error) {
79+
left, right = GetValueElem(left), GetValueElem(right)
7880
switch left.Kind() {
7981
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
8082
lv := left.Int()
@@ -128,6 +130,7 @@ func EvaluateDivision(left, right reflect.Value) (reflect.Value, error) {
128130

129131
// EvaluateModulo will evaluate modulo operation over two value
130132
func EvaluateModulo(left, right reflect.Value) (reflect.Value, error) {
133+
left, right = GetValueElem(left), GetValueElem(right)
131134
switch left.Kind() {
132135
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
133136
lv := left.Int()
@@ -160,6 +163,7 @@ func EvaluateModulo(left, right reflect.Value) (reflect.Value, error) {
160163

161164
// EvaluateAddition will evaluate addition operation over two value
162165
func EvaluateAddition(left, right reflect.Value) (reflect.Value, error) {
166+
left, right = GetValueElem(left), GetValueElem(right)
163167
switch left.Kind() {
164168
case reflect.String:
165169
lv := left.String()
@@ -247,6 +251,7 @@ func EvaluateAddition(left, right reflect.Value) (reflect.Value, error) {
247251

248252
// EvaluateSubtraction will evaluate subtraction operation over two value
249253
func EvaluateSubtraction(left, right reflect.Value) (reflect.Value, error) {
254+
left, right = GetValueElem(left), GetValueElem(right)
250255
switch left.Kind() {
251256
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
252257
lv := left.Int()
@@ -300,6 +305,7 @@ func EvaluateSubtraction(left, right reflect.Value) (reflect.Value, error) {
300305

301306
// EvaluateBitAnd will evaluate Bitwise And operation over two value
302307
func EvaluateBitAnd(left, right reflect.Value) (reflect.Value, error) {
308+
left, right = GetValueElem(left), GetValueElem(right)
303309
switch left.Kind() {
304310
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
305311
lv := left.Int()
@@ -332,6 +338,7 @@ func EvaluateBitAnd(left, right reflect.Value) (reflect.Value, error) {
332338

333339
// EvaluateBitOr will evaluate Bitwise Or operation over two value
334340
func EvaluateBitOr(left, right reflect.Value) (reflect.Value, error) {
341+
left, right = GetValueElem(left), GetValueElem(right)
335342
switch left.Kind() {
336343
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
337344
lv := left.Int()
@@ -364,6 +371,7 @@ func EvaluateBitOr(left, right reflect.Value) (reflect.Value, error) {
364371

365372
// EvaluateGreaterThan will evaluate GreaterThan operation over two value
366373
func EvaluateGreaterThan(left, right reflect.Value) (reflect.Value, error) {
374+
left, right = GetValueElem(left), GetValueElem(right)
367375
switch left.Kind() {
368376
case reflect.String:
369377
lv := left.String()
@@ -431,6 +439,7 @@ func EvaluateGreaterThan(left, right reflect.Value) (reflect.Value, error) {
431439

432440
// EvaluateLesserThan will evaluate LesserThan operation over two value
433441
func EvaluateLesserThan(left, right reflect.Value) (reflect.Value, error) {
442+
left, right = GetValueElem(left), GetValueElem(right)
434443
switch left.Kind() {
435444
case reflect.String:
436445
lv := left.String()
@@ -498,6 +507,7 @@ func EvaluateLesserThan(left, right reflect.Value) (reflect.Value, error) {
498507

499508
// EvaluateGreaterThanEqual will evaluate GreaterThanEqual operation over two value
500509
func EvaluateGreaterThanEqual(left, right reflect.Value) (reflect.Value, error) {
510+
left, right = GetValueElem(left), GetValueElem(right)
501511
switch left.Kind() {
502512
case reflect.String:
503513
lv := left.String()
@@ -565,6 +575,7 @@ func EvaluateGreaterThanEqual(left, right reflect.Value) (reflect.Value, error)
565575

566576
// EvaluateLesserThanEqual will evaluate LesserThanEqual operation over two value
567577
func EvaluateLesserThanEqual(left, right reflect.Value) (reflect.Value, error) {
578+
left, right = GetValueElem(left), GetValueElem(right)
568579
switch left.Kind() {
569580
case reflect.String:
570581
lv := left.String()
@@ -632,6 +643,7 @@ func EvaluateLesserThanEqual(left, right reflect.Value) (reflect.Value, error) {
632643

633644
// EvaluateEqual will evaluate Equal operation over two value
634645
func EvaluateEqual(left, right reflect.Value) (reflect.Value, error) {
646+
left, right = GetValueElem(left), GetValueElem(right)
635647
switch left.Kind() {
636648
case reflect.String:
637649
lv := left.String()
@@ -704,6 +716,7 @@ func EvaluateEqual(left, right reflect.Value) (reflect.Value, error) {
704716

705717
// EvaluateNotEqual will evaluate NotEqual operation over two value
706718
func EvaluateNotEqual(left, right reflect.Value) (reflect.Value, error) {
719+
left, right = GetValueElem(left), GetValueElem(right)
707720
switch left.Kind() {
708721
case reflect.String:
709722
lv := left.String()
@@ -776,6 +789,7 @@ func EvaluateNotEqual(left, right reflect.Value) (reflect.Value, error) {
776789

777790
// EvaluateLogicAnd will evaluate LogicalAnd operation over two value
778791
func EvaluateLogicAnd(left, right reflect.Value) (reflect.Value, error) {
792+
left, right = GetValueElem(left), GetValueElem(right)
779793
if left.Kind() == reflect.Bool && right.Kind() == reflect.Bool {
780794
lv := left.Bool()
781795
rv := right.Bool()
@@ -786,6 +800,7 @@ func EvaluateLogicAnd(left, right reflect.Value) (reflect.Value, error) {
786800

787801
// EvaluateLogicOr will evaluate LogicalOr operation over two value
788802
func EvaluateLogicOr(left, right reflect.Value) (reflect.Value, error) {
803+
left, right = GetValueElem(left), GetValueElem(right)
789804
if left.Kind() == reflect.Bool && right.Kind() == reflect.Bool {
790805
lv := left.Bool()
791806
rv := right.Bool()
@@ -796,6 +811,7 @@ func EvaluateLogicOr(left, right reflect.Value) (reflect.Value, error) {
796811

797812
// EvaluateLogicSingle will evaluate single expression value
798813
func EvaluateLogicSingle(left reflect.Value) (reflect.Value, error) {
814+
left = GetValueElem(left)
799815
if left.Kind() == reflect.Bool {
800816
lv := left.Bool()
801817
return reflect.ValueOf(lv), nil

pkg/reflectmath_test.go

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,23 @@ import (
2020
)
2121

2222
var (
23-
intVal = reflect.ValueOf(12)
24-
int8Val = reflect.ValueOf(int8(12))
25-
int16Val = reflect.ValueOf(int16(12))
26-
int32Val = reflect.ValueOf(int32(12))
27-
int64Val = reflect.ValueOf(int64(12))
23+
intVar = 12
24+
int8Var = int8(12)
25+
int16Var = int16(12)
26+
int32Var = int32(12)
27+
int64Var = int64(12)
28+
29+
intVal = reflect.ValueOf(intVar)
30+
int8Val = reflect.ValueOf(int8Var)
31+
int16Val = reflect.ValueOf(int16Var)
32+
int32Val = reflect.ValueOf(int32Var)
33+
int64Val = reflect.ValueOf(int64Var)
34+
35+
intPtr = reflect.ValueOf(&intVar)
36+
int8Ptr = reflect.ValueOf(&int8Var)
37+
int16Ptr = reflect.ValueOf(&int16Var)
38+
int32Ptr = reflect.ValueOf(&int32Var)
39+
int64Ptr = reflect.ValueOf(&int64Var)
2840

2941
uintVal = reflect.ValueOf(uint(12))
3042
uint8Val = reflect.ValueOf(uint8(12))
@@ -35,11 +47,23 @@ var (
3547
float32Val = reflect.ValueOf(float32(12))
3648
float64Val = reflect.ValueOf(float64(12))
3749

38-
intVal2 = reflect.ValueOf(3)
39-
int8Val2 = reflect.ValueOf(int8(3))
40-
int16Val2 = reflect.ValueOf(int16(3))
41-
int32Val2 = reflect.ValueOf(int32(3))
42-
int64Val2 = reflect.ValueOf(int64(3))
50+
intVar2 = 3
51+
int8Var2 = int8(3)
52+
int16Var2 = int16(3)
53+
int32Var2 = int32(3)
54+
int64Var2 = int64(3)
55+
56+
intVal2 = reflect.ValueOf(intVar2)
57+
int8Val2 = reflect.ValueOf(int8Var2)
58+
int16Val2 = reflect.ValueOf(int16Var2)
59+
int32Val2 = reflect.ValueOf(int32Var2)
60+
int64Val2 = reflect.ValueOf(int64Var2)
61+
62+
intPtr2 = reflect.ValueOf(&intVar2)
63+
int8Ptr2 = reflect.ValueOf(&int8Var2)
64+
int16Ptr2 = reflect.ValueOf(&int16Var2)
65+
int32Ptr2 = reflect.ValueOf(&int32Var2)
66+
int64Ptr2 = reflect.ValueOf(&int64Var2)
4367

4468
uintVal2 = reflect.ValueOf(uint(3))
4569
uint8Val2 = reflect.ValueOf(uint8(3))
@@ -50,8 +74,8 @@ var (
5074
float32Val2 = reflect.ValueOf(float32(3))
5175
float64Val2 = reflect.ValueOf(float64(3))
5276

53-
valuesA = []reflect.Value{intVal, int8Val, int16Val, int32Val, int64Val, uintVal, uint8Val, uint16Val, uint32Val, uint64Val, float32Val, float64Val}
54-
valuesB = []reflect.Value{intVal2, int8Val2, int16Val2, int32Val2, int64Val2, uintVal2, uint8Val2, uint16Val2, uint32Val2, uint64Val2, float32Val2, float64Val2}
77+
valuesA = []reflect.Value{intVal, int8Val, int16Val, int32Val, int64Val, uintVal, uint8Val, uint16Val, uint32Val, uint64Val, float32Val, float64Val, intPtr, int8Ptr, int16Ptr, int32Ptr, int64Ptr}
78+
valuesB = []reflect.Value{intVal2, int8Val2, int16Val2, int32Val2, int64Val2, uintVal2, uint8Val2, uint16Val2, uint32Val2, uint64Val2, float32Val2, float64Val2, intPtr2, int8Ptr2, int16Ptr2, int32Ptr2, int64Ptr2}
5579

5680
intVal3 = reflect.ValueOf(0x55)
5781
int8Val3 = reflect.ValueOf(int8(0x55))
@@ -177,6 +201,8 @@ func TestValueAdd(t *testing.T) {
177201
} else {
178202
t.Errorf("Math Add expect number types return")
179203
}
204+
205+
va, vb = GetValueElem(va), GetValueElem(vb)
180206
if GetBaseKind(va) == reflect.Float64 || GetBaseKind(vb) == reflect.Float64 {
181207
if vc.Kind() != reflect.Float64 {
182208
t.Errorf("Any Add to float should yield Float64, but %s", vc.Kind().String())
@@ -213,6 +239,30 @@ func TestValueAdd(t *testing.T) {
213239
} else if GetBaseKind(va) == reflect.Float64 && vs.String() != "Text12.000000" {
214240
t.Errorf("Should be \"Text12.000000\". Got \"%s\"", vd.String())
215241
}
242+
243+
str := "Text"
244+
stringPtr := reflect.ValueOf(&str)
245+
vp, err := EvaluateAddition(va, stringPtr)
246+
if err != nil {
247+
t.Errorf("Error while adding string. Got %v", err)
248+
} else if vp.Kind() != reflect.String {
249+
t.Errorf("Add to string should yield a string. Got %s", vp.Kind().String())
250+
} else if GetBaseKind(va) != reflect.Float64 && vp.String() != "12Text" {
251+
t.Errorf("Should be \"12Text\". Got \"%s\"", vp.String())
252+
} else if GetBaseKind(va) == reflect.Float64 && vp.String() != "12.000000Text" {
253+
t.Errorf("Should be \"12.000000Text\". Got \"%s\"", vp.String())
254+
}
255+
256+
vq, err := EvaluateAddition(stringPtr, va)
257+
if err != nil {
258+
t.Errorf("Error while adding string. Got %v", err)
259+
} else if vq.Kind() != reflect.String {
260+
t.Errorf("Add to string should yield a string. Got %s", vq.Kind().String())
261+
} else if GetBaseKind(va) != reflect.Float64 && vq.String() != "Text12" {
262+
t.Errorf("Should be \"Text12\". Got \"%s\"", vq.String())
263+
} else if GetBaseKind(va) == reflect.Float64 && vq.String() != "Text12.000000" {
264+
t.Errorf("Should be \"Text12.000000\". Got \"%s\"", vq.String())
265+
}
216266
}
217267
}
218268

@@ -239,6 +289,8 @@ func TestValueSub(t *testing.T) {
239289
} else {
240290
t.Errorf("Math Sub expect number types return")
241291
}
292+
293+
va, vb = GetValueElem(va), GetValueElem(vb)
242294
if GetBaseKind(va) == reflect.Float64 || GetBaseKind(vb) == reflect.Float64 {
243295
if vc.Kind() != reflect.Float64 {
244296
t.Errorf("Any Sub to float should yield Float64, but %s", vc.Kind().String())
@@ -285,6 +337,8 @@ func TestValueMul(t *testing.T) {
285337
} else {
286338
t.Errorf("Math Mul expect number types return")
287339
}
340+
341+
va, vb = GetValueElem(va), GetValueElem(vb)
288342
if GetBaseKind(va) == reflect.Float64 || GetBaseKind(vb) == reflect.Float64 {
289343
if vc.Kind() != reflect.Float64 {
290344
t.Errorf("Any Mul to float should yield Float64, but %s", vc.Kind().String())
@@ -331,6 +385,8 @@ func TestValueDiv(t *testing.T) {
331385
} else {
332386
t.Errorf("Math div expect number types return")
333387
}
388+
389+
va, vb = GetValueElem(va), GetValueElem(vb)
334390
if GetBaseKind(va) == reflect.Float64 || GetBaseKind(vb) == reflect.Float64 {
335391
if vc.Kind() != reflect.Float64 {
336392
t.Errorf("Any Div to float should yield Float64, but %s", vc.Kind().String())

pkg/reflectools.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ package pkg
1616

1717
import (
1818
"fmt"
19-
"github.com/hyperjumptech/grule-rule-engine/logger"
2019
"math"
2120
"reflect"
21+
22+
"github.com/hyperjumptech/grule-rule-engine/logger"
2223
)
2324

2425
// GetFunctionList get list of functions in a struct instance
@@ -558,3 +559,12 @@ func IsNumber(val reflect.Value) bool {
558559
}
559560
return false
560561
}
562+
563+
// GetValueElem will return the value val contains if val is of Kind Interface or Pointer
564+
func GetValueElem(val reflect.Value) reflect.Value {
565+
if val.Kind() == reflect.Pointer || val.Kind() == reflect.Interface {
566+
return GetValueElem(val.Elem())
567+
}
568+
569+
return val
570+
}

0 commit comments

Comments
 (0)