Skip to content

Commit

Permalink
feat: add string comparison support
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Jun 27, 2024
1 parent bdbb290 commit 52975c1
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions pkg/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,35 @@ func (intr *treeInterpreter) execute(node parsing.ASTNode, value interface{}, fu
case parsing.TOKNE:
return !util.ObjsEqual(left, right), nil
}
leftNum, ok := util.ToNumber(left)
if !ok {
return nil, nil
}
rightNum, ok := util.ToNumber(right)
if !ok {
return nil, nil
if leftStr, ok := left.(string); ok {
if rightStr, ok := right.(string); ok {
switch node.Value {
case parsing.TOKGT:
return leftStr > rightStr, nil
case parsing.TOKGTE:
return leftStr >= rightStr, nil
case parsing.TOKLT:
return leftStr < rightStr, nil
case parsing.TOKLTE:
return leftStr <= rightStr, nil

Check warning on line 134 in pkg/interpreter/interpreter.go

View check run for this annotation

Codecov / codecov/patch

pkg/interpreter/interpreter.go#L125-L134

Added lines #L125 - L134 were not covered by tests
}
}
}
switch node.Value {
case parsing.TOKGT:
return leftNum > rightNum, nil
case parsing.TOKGTE:
return leftNum >= rightNum, nil
case parsing.TOKLT:
return leftNum < rightNum, nil
case parsing.TOKLTE:
return leftNum <= rightNum, nil
if leftNum, ok := util.ToNumber(left); ok {
if rightNum, ok := util.ToNumber(right); ok {
switch node.Value {
case parsing.TOKGT:
return leftNum > rightNum, nil
case parsing.TOKGTE:
return leftNum >= rightNum, nil
case parsing.TOKLT:
return leftNum < rightNum, nil
case parsing.TOKLTE:
return leftNum <= rightNum, nil
}
}
}
return nil, nil
case parsing.ASTExpRef:
return func(data interface{}) (interface{}, error) {
return intr.execute(node.Children[0], data, functionCaller)
Expand Down

0 comments on commit 52975c1

Please sign in to comment.