Skip to content

Commit

Permalink
enhancement: simplify error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Sep 10, 2023
1 parent 76b83df commit e9d9757
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
44 changes: 9 additions & 35 deletions services/search/pkg/query/kql/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions services/search/pkg/query/kql/validate.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e9d9757

Please sign in to comment.