From 6c9c2f171d7f1c07be4ad15377e08dafc11962be Mon Sep 17 00:00:00 2001 From: Danis Kirasirov <dbgbbu@gmail.com> Date: Tue, 23 Jan 2024 12:00:35 +0300 Subject: [PATCH] refactor --- pkg/expr/mongo.go | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/pkg/expr/mongo.go b/pkg/expr/mongo.go index 84471cce..08a33e0f 100644 --- a/pkg/expr/mongo.go +++ b/pkg/expr/mongo.go @@ -43,7 +43,6 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter env[EnvContextKey] = ctx config := GetDefaultConfig(env) - config.Visitors = append(config.Visitors, notinPatcher{}) for _, op := range ops { op(config) @@ -207,14 +206,14 @@ func (c *compiler) ConstantNode(node *ast.ConstantNode) interface{} { } func (c *compiler) UnaryNode(node *ast.UnaryNode) interface{} { - op := c.compile(node.Node) - switch node.Operator { - case "!", "not": - return bson.M{"$not": op} - case "": - return op + nodeIn, ok := node.Node.(*ast.BinaryNode) + if !ok || nodeIn.Operator != "in" { + return bson.M{"$not": c.compile(node.Node)} + } + + return bson.M{c.identifier(nodeIn.Left): bson.M{"$nin": c.eval(nodeIn.Right)}} default: panic(fmt.Sprintf("unknown operator (%v)", node.Operator)) } @@ -642,25 +641,3 @@ func (c *compiler) PairNode(node *ast.PairNode) interface{} { //c.compile(node.Key) //c.compile(node.Value) } - -// notinPatcher проходит по лексемам ast-дерева -// и объединяет лексемы "not" и "in" в "not in" -type notinPatcher struct{} - -func (t notinPatcher) Visit(node *ast.Node) { - nodeNot, ok := (*node).(*ast.UnaryNode) - if !ok || nodeNot.Operator != "not" { - return - } - - nodeIn, ok := nodeNot.Node.(*ast.BinaryNode) - if !ok || nodeIn.Operator != "in" { - return - } - - nodeNot.Operator = "" - ast.Patch(node, nodeNot) - - nodeIn.Operator = "not in" - ast.Patch(&nodeNot.Node, nodeIn) -} \ No newline at end of file -- GitLab