mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Fix unary node print for conditional expr (#676)
* feat: extract code for compiling equal operator * fix: fix unary node inside condition node print
This commit is contained in:
+7
-4
@@ -45,13 +45,16 @@ func (n *ConstantNode) String() string {
|
||||
}
|
||||
|
||||
func (n *UnaryNode) String() string {
|
||||
op := ""
|
||||
op := n.Operator
|
||||
if n.Operator == "not" {
|
||||
op = fmt.Sprintf("%s ", n.Operator)
|
||||
} else {
|
||||
op = fmt.Sprintf("%s", n.Operator)
|
||||
}
|
||||
if _, ok := n.Node.(*BinaryNode); ok {
|
||||
wrap := false
|
||||
switch n.Node.(type) {
|
||||
case *BinaryNode, *ConditionalNode:
|
||||
wrap = true
|
||||
}
|
||||
if wrap {
|
||||
return fmt.Sprintf("%s(%s)", op, n.Node.String())
|
||||
}
|
||||
return fmt.Sprintf("%s%s", op, n.Node.String())
|
||||
|
||||
@@ -77,6 +77,7 @@ func TestPrint(t *testing.T) {
|
||||
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
|
||||
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
|
||||
{`(One == 1 ? true : false) && Two == 2`, `(One == 1 ? true : false) && Two == 2`},
|
||||
{`not (a == 1 ? b > 1 : b < 2)`, `not (a == 1 ? b > 1 : b < 2)`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user