@@ -3,9 +3,20 @@ package parser
3
3
import (
4
4
"fmt"
5
5
"strings"
6
+
6
7
"github.com/expr-lang/expr"
7
8
)
8
9
10
+ // ComparisonType represents the type of comparison in an expression
11
+ type ComparisonType string
12
+
13
+ const (
14
+ // SINGLE_ENTITY represents comparison between one entity and a static value
15
+ SINGLE_ENTITY ComparisonType = "SINGLE_ENTITY"
16
+ // DUAL_ENTITY represents comparison between two different entities
17
+ DUAL_ENTITY ComparisonType = "DUAL_ENTITY"
18
+ )
19
+
9
20
// EvaluateExpressionTree evaluates the expression tree against input data
10
21
// and returns filtered data based on the expression conditions
11
22
func EvaluateExpressionTree (tree * ExpressionNode , data []map [string ]interface {}) ([]map [string ]interface {}, error ) {
@@ -19,7 +30,7 @@ func EvaluateExpressionTree(tree *ExpressionNode, data []map[string]interface{})
19
30
if err != nil {
20
31
return nil , fmt .Errorf ("evaluation error: %w" , err )
21
32
}
22
-
33
+
23
34
// Only include items that match the expression
24
35
if matches .(bool ) {
25
36
result = append (result , item )
@@ -57,16 +68,73 @@ func evaluateNode(node *ExpressionNode, data map[string]interface{}) (interface{
57
68
}
58
69
59
70
// nodeToExprString converts an ExpressionNode to an expr-lang expression string
71
+ // DetectComparisonType analyzes a binary expression node and determines if it's comparing
72
+ // a single entity with a static value or comparing two different entities
73
+ func DetectComparisonType (node * ExpressionNode ) (ComparisonType , error ) {
74
+ if node == nil {
75
+ return "" , fmt .Errorf ("nil node" )
76
+ }
77
+
78
+ // Only analyze binary nodes
79
+ if node .Type != "binary" {
80
+ return "" , fmt .Errorf ("not a binary node" )
81
+ }
82
+
83
+ // Get entity names from left and right sides
84
+ leftEntity , err := getEntityName (node .Left )
85
+ if err != nil {
86
+ return "" , fmt .Errorf ("failed to get left entity: %w" , err )
87
+ }
88
+
89
+ rightEntity , err := getEntityName (node .Right )
90
+ if err != nil {
91
+ return "" , fmt .Errorf ("failed to get right entity: %w" , err )
92
+ }
93
+
94
+ // If either side is empty (literal/static value) or they're the same entity,
95
+ // it's a SINGLE_ENTITY comparison
96
+ if leftEntity == "" || rightEntity == "" || leftEntity == rightEntity {
97
+ return SINGLE_ENTITY , nil
98
+ }
99
+
100
+ // Different entities are being compared
101
+ return DUAL_ENTITY , nil
102
+ }
103
+
104
+ // getEntityName extracts the entity name from a node.
105
+ // Returns empty string for literals and static values.
106
+ func getEntityName (node * ExpressionNode ) (string , error ) {
107
+ if node == nil {
108
+ return "" , fmt .Errorf ("nil node" )
109
+ }
110
+
111
+ switch node .Type {
112
+ case "variable" :
113
+ return node .Value , nil
114
+ case "method_call" :
115
+ // For method calls, consider the target object as the entity
116
+ parts := strings .Split (node .Value , "." )
117
+ if len (parts ) > 0 {
118
+ return parts [0 ], nil
119
+ }
120
+ return "" , nil
121
+ case "literal" :
122
+ return "" , nil // Literals are static values
123
+ default :
124
+ return "" , fmt .Errorf ("unsupported node type: %s" , node .Type )
125
+ }
126
+ }
127
+
60
128
func nodeToExprString (node * ExpressionNode ) (string , error ) {
61
129
switch node .Type {
62
130
case "binary" :
63
131
left , err := nodeToExprString (node .Left )
64
132
if err != nil {
65
- return "" , err
133
+ return "" , err
66
134
}
67
135
right , err := nodeToExprString (node .Right )
68
136
if err != nil {
69
- return "" , err
137
+ return "" , err
70
138
}
71
139
return fmt .Sprintf ("(%s %s %s)" , left , node .Operator , right ), nil
72
140
@@ -111,5 +179,3 @@ func nodeToExprString(node *ExpressionNode) (string, error) {
111
179
return "" , fmt .Errorf ("unknown node type: %s" , node .Type )
112
180
}
113
181
}
114
-
115
-
0 commit comments