Skip to content

Commit

Permalink
fix: fix the wrong type transform for equal
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Nov 5, 2021
1 parent b4fb886 commit 6a2a8a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ func (op *OperatorToken) Validate(tokens []AnyToken) (retryToken AnyToken, err e
switch prev.(type) {
case *NumberToken, *IdentifierToken, *RightBracketToken, *RightSquareBracketToken:
case *OperatorToken, nil:
// only allow unary token !
if name != "!" {
return nil, fmt.Errorf("wrong operator token")
}
Expand Down Expand Up @@ -1340,6 +1341,7 @@ func (exp *Expression) toAst(tokens []AnyToken, isInFunc bool) (*TokenNode, erro
if i+1 < total {
next = lasts[i+1]
if cur, ok := next.(*TokenNode); ok {
// multiple unary node
nextNode = cur.Node
isNextTokenNode = true
} else {
Expand Down
10 changes: 7 additions & 3 deletions lib/expression/expression_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package expression

import (
"fmt"
"log"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -17,7 +15,6 @@ func assertTokenList(t *testing.T, code string, types ...string) {
for i, token := range tokens {
actualType := typeOf(token)
toBeType := types[i]
log.Println(actualType, toBeType, fmt.Sprintf("%#v", token))
assert.Equal(t, actualType, toBeType, "the type is not matched")
}
}
Expand Down Expand Up @@ -99,3 +96,10 @@ func TestTokenize(t *testing.T) {
assertTokenList(t, "$time|strtotime|date_format:\"Y-m-d\"", "IdentifierToken", "OperatorToken", "IdentifierToken", "OperatorToken", "IdentifierToken", "OperatorToken", "StringToken")
})
}

func TestToAst(t *testing.T) {
t.Run("Test to ast", func(t *testing.T) {
_, err := exp.Parse("!!!!!$a.b != \"1\"")
assert.Nil(t, err)
})
}
9 changes: 5 additions & 4 deletions lib/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func (gen *Generator) wrapToFloat(node *Node, options *GenOptions, parseOptions
fn := toFloatFn
if _, ok := compareFnNames[op]; ok {
isNative = true
if op == "==" {
// if equal or not equal
if op == "==" || op == "!=" {
fn = toFloatOrString
}
}
Expand Down Expand Up @@ -196,8 +197,8 @@ func (gen *Generator) parseRecursive(node *Node, options *GenOptions, parseOptio
noObjectIndex, parseConf, captures := parseOptions.NoObjectIndex, parseOptions.Conf, parseOptions.Captures
curType := node.Type
conf := gen.Conf
isNot := node.Operator == "!"
if isNot {
isUnaryNot := node.Operator == "!"
if isUnaryNot {
str.WriteString("(not ")
}
if curType == "raw" {
Expand Down Expand Up @@ -437,7 +438,7 @@ func (gen *Generator) parseRecursive(node *Node, options *GenOptions, parseOptio
}
str.WriteString(")")
}
if isNot {
if isUnaryNot {
str.WriteString(")")
}
return noDelimit, nil
Expand Down

0 comments on commit 6a2a8a5

Please sign in to comment.