File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 1
1
package optimizer
2
2
3
3
import (
4
+ "reflect"
5
+
4
6
. "github.com/antonmedv/expr/ast"
5
7
)
6
8
@@ -10,9 +12,16 @@ func (*inRange) Visit(node *Node) {
10
12
switch n := (* node ).(type ) {
11
13
case * BinaryNode :
12
14
if n .Operator == "in" {
13
- if rng , ok := n .Right .(* BinaryNode ); ok && rng .Operator == ".." {
14
- if from , ok := rng .Left .(* IntegerNode ); ok {
15
- if to , ok := rng .Right .(* IntegerNode ); ok {
15
+ t := n .Left .Type ()
16
+ if t == nil {
17
+ return
18
+ }
19
+ if t .Kind () != reflect .Int {
20
+ return
21
+ }
22
+ if rangeOp , ok := n .Right .(* BinaryNode ); ok && rangeOp .Operator == ".." {
23
+ if from , ok := rangeOp .Left .(* IntegerNode ); ok {
24
+ if to , ok := rangeOp .Right .(* IntegerNode ); ok {
16
25
Patch (node , & BinaryNode {
17
26
Operator : "and" ,
18
27
Left : & BinaryNode {
Original file line number Diff line number Diff line change 4
4
"strings"
5
5
"testing"
6
6
7
+ "github.com/antonmedv/expr"
7
8
"github.com/antonmedv/expr/ast"
8
9
"github.com/antonmedv/expr/checker"
9
10
"github.com/antonmedv/expr/conf"
@@ -77,6 +78,9 @@ func TestOptimize_in_range(t *testing.T) {
77
78
tree , err := parser .Parse (`age in 18..31` )
78
79
require .NoError (t , err )
79
80
81
+ config := conf .New (map [string ]int {"age" : 30 })
82
+ _ , err = checker .Check (tree , config )
83
+
80
84
err = optimizer .Optimize (& tree .Node , nil )
81
85
require .NoError (t , err )
82
86
@@ -104,6 +108,12 @@ func TestOptimize_in_range(t *testing.T) {
104
108
assert .Equal (t , ast .Dump (expected ), ast .Dump (tree .Node ))
105
109
}
106
110
111
+ func TestOptimize_in_range_with_floats (t * testing.T ) {
112
+ out , err := expr .Eval (`f in 1..3` , map [string ]any {"f" : 1.5 })
113
+ require .NoError (t , err )
114
+ assert .Equal (t , false , out )
115
+ }
116
+
107
117
func TestOptimize_const_range (t * testing.T ) {
108
118
tree , err := parser .Parse (`-1..1` )
109
119
require .NoError (t , err )
You can’t perform that action at this time.
0 commit comments