@@ -3,6 +3,7 @@ package checker
33import (
44 "fmt"
55 "reflect"
6+ "regexp"
67
78 "github.com/antonmedv/expr/ast"
89 "github.com/antonmedv/expr/conf"
@@ -78,8 +79,6 @@ func (v *visitor) visit(node ast.Node) (reflect.Type, info) {
7879 t , i = v .UnaryNode (n )
7980 case * ast.BinaryNode :
8081 t , i = v .BinaryNode (n )
81- case * ast.MatchesNode :
82- t , i = v .MatchesNode (n )
8382 case * ast.ChainNode :
8483 t , i = v .ChainNode (n )
8584 case * ast.MemberNode :
@@ -232,23 +231,6 @@ func (v *visitor) BinaryNode(node *ast.BinaryNode) (reflect.Type, info) {
232231 return boolType , info {}
233232 }
234233
235- case "in" , "not in" :
236- if (isString (l ) || isAny (l )) && isStruct (r ) {
237- return boolType , info {}
238- }
239- if isMap (r ) {
240- return boolType , info {}
241- }
242- if isArray (r ) {
243- return boolType , info {}
244- }
245- if isAny (l ) && anyOf (r , isString , isArray , isMap ) {
246- return boolType , info {}
247- }
248- if isAny (l ) && isAny (r ) {
249- return boolType , info {}
250- }
251-
252234 case "<" , ">" , ">=" , "<=" :
253235 if isNumber (l ) && isNumber (r ) {
254236 return boolType , info {}
@@ -315,6 +297,38 @@ func (v *visitor) BinaryNode(node *ast.BinaryNode) (reflect.Type, info) {
315297 return anyType , info {}
316298 }
317299
300+ case "in" :
301+ if (isString (l ) || isAny (l )) && isStruct (r ) {
302+ return boolType , info {}
303+ }
304+ if isMap (r ) {
305+ return boolType , info {}
306+ }
307+ if isArray (r ) {
308+ return boolType , info {}
309+ }
310+ if isAny (l ) && anyOf (r , isString , isArray , isMap ) {
311+ return boolType , info {}
312+ }
313+ if isAny (l ) && isAny (r ) {
314+ return boolType , info {}
315+ }
316+
317+ case "matches" :
318+ if s , ok := node .Right .(* ast.StringNode ); ok {
319+ r , err := regexp .Compile (s .Value )
320+ if err != nil {
321+ return v .error (node , err .Error ())
322+ }
323+ node .Regexp = r
324+ }
325+ if isString (l ) && isString (r ) {
326+ return boolType , info {}
327+ }
328+ if or (l , r , isString ) {
329+ return boolType , info {}
330+ }
331+
318332 case "contains" , "startsWith" , "endsWith" :
319333 if isString (l ) && isString (r ) {
320334 return boolType , info {}
@@ -340,20 +354,6 @@ func (v *visitor) BinaryNode(node *ast.BinaryNode) (reflect.Type, info) {
340354 return v .error (node , `invalid operation: %v (mismatched types %v and %v)` , node .Operator , l , r )
341355}
342356
343- func (v * visitor ) MatchesNode (node * ast.MatchesNode ) (reflect.Type , info ) {
344- l , _ := v .visit (node .Left )
345- r , _ := v .visit (node .Right )
346-
347- if isString (l ) && isString (r ) {
348- return boolType , info {}
349- }
350- if or (l , r , isString ) {
351- return boolType , info {}
352- }
353-
354- return v .error (node , `invalid operation: matches (mismatched types %v and %v)` , l , r )
355- }
356-
357357func (v * visitor ) ChainNode (node * ast.ChainNode ) (reflect.Type , info ) {
358358 return v .visit (node .Node )
359359}
0 commit comments