Skip to content

Commit

Permalink
feat: revert parsing limit and offset separately for AST
Browse files Browse the repository at this point in the history
  • Loading branch information
tauslim committed Feb 27, 2024
1 parent c7e92ce commit 241d0ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 36 deletions.
41 changes: 8 additions & 33 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"time"
)

var (
reservedOps = []string{OffsetOp, LimitOp, SelectOp, SortOp}
)

const (
OffsetOp = "offset"
LimitOp = "limit"
Expand Down Expand Up @@ -65,15 +61,15 @@ func (tb TokenBloc) String() (s string) {
}

func (r *RqlRootNode) parseSpecialOps() {
if parseLimit(r.Node, r) || parseSort(r.Node, r) || parseOffset(r.Node, r) || parseFields(r.Node, r) {
if parseLimitOffset(r.Node, r) || parseSort(r.Node, r) || parseFields(r.Node, r) {
r.Node = nil
} else if r.Node != nil {
if strings.ToUpper(r.Node.Op) == "AND" {
tmpNodeArgs := r.Node.Args[:0]
for _, c := range r.Node.Args {
switch n := c.(type) {
case *RqlNode:
isSpecialOps := parseLimit(n, r) || parseSort(n, r) || parseOffset(n, r) || parseFields(n, r)
isSpecialOps := parseLimitOffset(n, r) || parseSort(n, r) || parseFields(n, r)
if !isSpecialOps {
tmpNodeArgs = append(tmpNodeArgs, n)
}
Expand All @@ -87,28 +83,20 @@ func (r *RqlRootNode) parseSpecialOps() {
}
}

func parseLimit(n *RqlNode, root *RqlRootNode) (isLimitOp bool) {
func parseLimitOffset(n *RqlNode, root *RqlRootNode) (isLimitOp bool) {
if n == nil {
return false
}
if n.Op == LimitOp {
root.limit = n.Args[1].(string)
if strings.ToLower(n.Op) == LimitOp {
root.limit = n.Args[0].(string)
if len(n.Args) > 1 {
root.offset = n.Args[1].(string)
}
isLimitOp = true
}
return
}

func parseOffset(n *RqlNode, root *RqlRootNode) (isOffsetOp bool) {
if n == nil {
return false
}
if n.Op == OffsetOp {
root.offset = n.Args[1].(string)
isOffsetOp = true
}
return
}

func parseSort(n *RqlNode, root *RqlRootNode) (isSortOp bool) {
if n == nil {
return false
Expand Down Expand Up @@ -456,9 +444,6 @@ func getBlocNode(tb []TokenString) (*RqlNode, error) {
if err != nil {
return nil, err
}
} else if isReservedBloc(tb) {
n.Op = tb[0].s
n.Args = []interface{}{tb[0].s, tb[2].s}
} else if isSimpleEqualBloc(tb) {
n.Op = "eq"
n.Args = []interface{}{tb[0].s, tb[2].s}
Expand Down Expand Up @@ -555,16 +540,6 @@ func isSimpleEqualBloc(tb []TokenString) bool {
return isSimple
}

func isReservedBloc(tb []TokenString) bool {
matchReserved := false
for _, r := range reservedOps {
if tb[0].s == r {
matchReserved = true
}
}
return tb[0].t == Ident && tb[1].t == EqualSign && matchReserved
}

func isDoubleEqualBloc(tb []TokenString) bool {
return tb[0].t == Ident && tb[1].t == EqualSign && tb[2].t == Ident && tb[3].t == EqualSign
}
2 changes: 1 addition & 1 deletion pkg/driver/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var tests = []Test{
},
{
Name: `Sort and limit`,
RQL: `eq(foo,42)&sort(+price,-length)&limit=10&offset=20`,
RQL: `eq(foo,42)&sort(+price,-length)&limit(10,20)`,
SQL: `WHERE ((foo = 42)) ORDER BY price, length DESC LIMIT 10 OFFSET 20`,
WantParseError: false,
WantTranslatorError: false,
Expand Down
4 changes: 2 additions & 2 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (p *Parser) validateSpecialOps(r *RqlRootNode) error {
return err
}
}
if len(r.Sort()) > 0 {
if p.c != nil && len(r.Sort()) > 0 {
err := p.validateSort(r.Sort())
if err != nil {
return err
Expand Down Expand Up @@ -173,7 +173,7 @@ func (p *Parser) validateLimit(l string) error {
if limit < 0 {
return fmt.Errorf("specified limit is less than zero")
}
if limit > p.c.LimitMaxValue {
if p.c != nil && limit > p.c.LimitMaxValue {
return fmt.Errorf("specified limit is more than the max limit %d allowed", p.c.DefaultLimit)
}
return nil
Expand Down

0 comments on commit 241d0ec

Please sign in to comment.