Skip to content

Commit 6198684

Browse files
authored
Add support for ConstantNode in checker (#197)
* Add support for ConstantNode in checker * Added test
1 parent e0e26fb commit 6198684

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

checker/checker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func (v *visitor) visit(node ast.Node) reflect.Type {
7171
t = v.BoolNode(n)
7272
case *ast.StringNode:
7373
t = v.StringNode(n)
74+
case *ast.ConstantNode:
75+
t = v.ConstantNode(n)
7476
case *ast.UnaryNode:
7577
t = v.UnaryNode(n)
7678
case *ast.BinaryNode:
@@ -160,6 +162,10 @@ func (v *visitor) StringNode(*ast.StringNode) reflect.Type {
160162
return stringType
161163
}
162164

165+
func (v *visitor) ConstantNode(node *ast.ConstantNode) reflect.Type {
166+
return reflect.TypeOf(node.Value)
167+
}
168+
163169
func (v *visitor) UnaryNode(node *ast.UnaryNode) reflect.Type {
164170
t := v.visit(node.Node)
165171

checker/checker_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package checker_test
22

33
import (
44
"fmt"
5+
"reflect"
6+
"regexp"
57
"strings"
68
"testing"
79
"time"
810

911
"github.com/antonmedv/expr"
12+
"github.com/antonmedv/expr/ast"
1013
"github.com/antonmedv/expr/checker"
1114
"github.com/antonmedv/expr/conf"
1215
"github.com/antonmedv/expr/parser"
@@ -85,6 +88,19 @@ func TestVisitor_BuiltinNode(t *testing.T) {
8588
}
8689
}
8790

91+
func TestVisitor_ConstantNode(t *testing.T) {
92+
tree, err := parser.Parse(`re("[a-z]")`)
93+
94+
regexValue := regexp.MustCompile("[a-z]")
95+
constNode := &ast.ConstantNode{Value: regexValue}
96+
ast.Patch(&tree.Node, constNode)
97+
98+
_, err = checker.Check(tree, conf.New(&mockEnv{}))
99+
assert.NoError(t, err)
100+
101+
assert.Equal(t, reflect.TypeOf(regexValue), tree.Node.Type())
102+
}
103+
88104
func TestCheck(t *testing.T) {
89105
var typeTests = []string{
90106
"!Bool",

0 commit comments

Comments
 (0)