Skip to content

Commit

Permalink
fix priority of unary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Jul 23, 2024
1 parent 9acd4cc commit 0ff8886
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,19 @@ func (e expression) prefixSuffixExpression(prefix string, suffix string, priorit
if err != nil {
return "", err
}
return prefix + exprSql + suffix, nil
var sb strings.Builder
sb.Grow(len(prefix) + len(exprSql) + len(suffix) + 2)
sb.WriteString(prefix)
shouldParenthesize := e.priority > priority
if shouldParenthesize {
sb.WriteByte('(')
}
sb.WriteString(exprSql)
if shouldParenthesize {
sb.WriteByte(')')
}
sb.WriteString(suffix)
return sb.String(), nil
},
priority: priority,
isBool: isBool,
Expand Down
1 change: 1 addition & 0 deletions expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func TestLogicalExpression(t *testing.T) {
assertValue(t, Or(a, b, c, d), "a OR b OR c OR d")
assertValue(t, a.And(b).Or(c).And(a).Or(b).And(c), "((a AND b OR c) AND a OR b) AND c")
assertValue(t, a.Or(b).And(c.Or(d)), "(a OR b) AND (c OR d)")
assertValue(t, a.Or(b).And(c).Not(), "NOT ((a OR b) AND c)")

assertValue(t, And(), "1")
assertValue(t, Or(), "0")
Expand Down

0 comments on commit 0ff8886

Please sign in to comment.