From e9d9757ee82b65aef45e1460bcc53bdb943fe9c6 Mon Sep 17 00:00:00 2001 From: Florian Schade Date: Sun, 10 Sep 2023 12:22:53 +0200 Subject: [PATCH] enhancement: simplify error handling --- services/search/pkg/query/kql/factory.go | 44 +++++------------------ services/search/pkg/query/kql/validate.go | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 services/search/pkg/query/kql/validate.go diff --git a/services/search/pkg/query/kql/factory.go b/services/search/pkg/query/kql/factory.go index 05827184830..bab2d945ae6 100644 --- a/services/search/pkg/query/kql/factory.go +++ b/services/search/pkg/query/kql/factory.go @@ -38,24 +38,16 @@ func buildAST(n interface{}, text []byte, pos position) (*ast.Ast, error) { return nil, err } - if len(nodes) == 0 { - return nil, nil + a := &ast.Ast{ + Base: b, + Nodes: connectNodes(DefaultConnector{sameKeyOPValue: BoolOR}, nodes...), } - nodes = connectNodes(DefaultConnector{sameKeyOPValue: BoolOR}, nodes...) - - switch node := nodes[0].(type) { - case *ast.OperatorNode: - switch node.Value { - case BoolAND, BoolOR: - return nil, StartsWithBinaryOperatorError{Node: node} - } + if err := validateAst(a); err != nil { + return nil, err } - return &ast.Ast{ - Base: b, - Nodes: nodes, - }, nil + return a, nil } func buildStringNode(k, v interface{}, text []byte, pos position) (*ast.StringNode, error) { @@ -170,32 +162,14 @@ func buildGroupNode(k, n interface{}, text []byte, pos position) (*ast.GroupNode return nil, err } - nodes = connectNodes(DefaultConnector{sameKeyOPValue: BoolAND}, nodes...) - gn := &ast.GroupNode{ Base: b, Key: key, - Nodes: nodes, + Nodes: connectNodes(DefaultConnector{sameKeyOPValue: BoolAND}, nodes...), } - if len(nodes) == 0 { - return gn, nil - } - - switch node := nodes[0].(type) { - case *ast.OperatorNode: - switch node.Value { - case BoolAND, BoolOR: - return nil, StartsWithBinaryOperatorError{Node: node} - } - } - - if key != "" { - for _, node := range nodes { - if ast.NodeKey(node) != "" { - return nil, NamedGroupInvalidNodesError{Node: node} - } - } + if err := validateGroupNode(gn); err != nil { + return nil, err } return gn, nil diff --git a/services/search/pkg/query/kql/validate.go b/services/search/pkg/query/kql/validate.go new file mode 100644 index 00000000000..9e9a2428b99 --- /dev/null +++ b/services/search/pkg/query/kql/validate.go @@ -0,0 +1,37 @@ +package kql + +import ( + "github.com/owncloud/ocis/v2/services/search/pkg/query/ast" +) + +func validateAst(a *ast.Ast) error { + switch node := a.Nodes[0].(type) { + case *ast.OperatorNode: + switch node.Value { + case BoolAND, BoolOR: + return StartsWithBinaryOperatorError{Node: node} + } + } + + return nil +} + +func validateGroupNode(n *ast.GroupNode) error { + switch node := n.Nodes[0].(type) { + case *ast.OperatorNode: + switch node.Value { + case BoolAND, BoolOR: + return StartsWithBinaryOperatorError{Node: node} + } + } + + if n.Key != "" { + for _, node := range n.Nodes { + if ast.NodeKey(node) != "" { + return NamedGroupInvalidNodesError{Node: node} + } + } + } + + return nil +}