Skip to content

Commit

Permalink
perf(parser): updating parser algorithm for parsing TQL queries (zurv…
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jan 28, 2024
1 parent dba7791 commit f17c9c9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
11 changes: 8 additions & 3 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package commands

import (
"bufio"
"errors"
"fmt"
"net"
"os"
"strings"
"time"

cobra "github.com/spf13/cobra"
"github.com/zurvan-lab/TimeTrace/utils/errors"
)

const PROMPT = "\n>> "
Expand Down Expand Up @@ -51,15 +51,20 @@ func REPLCommand(parentCmd *cobra.Command) {
cmd.Print(do(conn, input))
}
} else {
Dead(cmd, errors.New(response)) //nolint
Dead(cmd, fmt.Errorf("%w: %s", errors.ErrInvalidCommand, response))
}
}
}

func do(conn net.Conn, q string) string {
resBuf := make([]byte, 1024)
query := []byte(q)

_, err := conn.Write([]byte(q))
if len(query) < 1 {
return "INVALID"
}

_, err := conn.Write(query)
if err != nil {
return err.Error()
}
Expand Down
18 changes: 4 additions & 14 deletions core/TQL/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@ import (

// parsing TQL queries. see: docs/TQL.
func ParseQuery(query string) database.Query {
command := ""
args := []string{}
q := strings.Split(query, " ")

for _, word := range strings.Split(query, " ") {
if word == "" {
continue
}

if command != "" {
args = append(args, word)
} else {
command = word
}
return database.Query{
Command: q[0],
Args: q[1:],
}

return database.Query{Command: command, Args: args}
}
1 change: 1 addition & 0 deletions utils/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ var (

// CLI.
ErrInvalidUserOrPassword = errors.New("user or user information you provided is invalid")
ErrInvalidCommand = errors.New("invalid command")
)

0 comments on commit f17c9c9

Please sign in to comment.