Skip to content

Commit

Permalink
chore: enhancement (zurvan-lab#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jul 9, 2024
1 parent 491bd52 commit f977919
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/commands/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func PingCommand(parentCmd *cobra.Command) {
ping.Run = func(cmd *cobra.Command, args []string) {
conn, err := net.Dial("tcp", *address)
if err != nil {
Dead(cmd, err)
ExitOnError(cmd, err)
}
defer conn.Close()

Expand Down
4 changes: 2 additions & 2 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func REPLCommand(parentCmd *cobra.Command) {
connect.Run = func(cmd *cobra.Command, args []string) {
conn, err := net.Dial("tcp", *address)
if err != nil {
Dead(cmd, err)
ExitOnError(cmd, err)
}
defer conn.Close()

Expand All @@ -51,7 +51,7 @@ func REPLCommand(parentCmd *cobra.Command) {
cmd.Print(do(conn, input))
}
} else {
Dead(cmd, fmt.Errorf("%w: %s", errors.ErrInvalidCommand, response))
ExitOnError(cmd, fmt.Errorf("%w: %s", errors.ErrInvalidCommand, response))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ func RunCommand(parentCmd *cobra.Command) {

run.Run = func(cmd *cobra.Command, args []string) {
if confingPath == nil || *confingPath == "" {
Dead(cmd, tte.ErrInavlidConfigPath)
ExitOnError(cmd, tte.ErrInavlidConfigPath)
}

cfg, err := config.LoadFromFile(*confingPath)
if err != nil {
Dead(cmd, err)
ExitOnError(cmd, err)
}

db := database.Init(cfg)
ttlog.InitGlobalLogger(&cfg.Log)

server := server.NewServer(cfg, db)
if err := server.Start(); err != nil {
Dead(cmd, err)
ExitOnError(cmd, err)
}
}
}
2 changes: 1 addition & 1 deletion cmd/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

func Dead(cmd *cobra.Command, err error) {
func ExitOnError(cmd *cobra.Command, err error) {
cmd.PrintErrln(err)
os.Exit(1)
}
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func main() {

err := rootCmd.Execute()
if err != nil {
commands.Dead(rootCmd, err)
commands.ExitOnError(rootCmd, err)
}
}
2 changes: 1 addition & 1 deletion core/TQL/execute/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var Executors ExecutorMap = ExecutorMap{
func Execute(q database.Query, db database.IDataBase) string {
execute, ok := Executors[q.Command]
if !ok {
return "INVALID"
return database.INVALID
}

result := execute(db, q.Args)
Expand Down
27 changes: 18 additions & 9 deletions core/TQL/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package core

import (

Check failure on line 3 in core/TQL/parser/parser_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"testing"

"github.com/stretchr/testify/assert"
"testing"

Check failure on line 5 in core/TQL/parser/parser_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
)

func TestParseQuery(t *testing.T) {
query := "PUSH testSet testSubSet hello 1700842078"
paredQuery := ParseQuery(query)
t.Run("good query", func(t *testing.T) {
query := "PUSH testSet testSubSet hello 1700842078"
paredQuery := ParseQuery(query)

assert.Equal(t, paredQuery.Command, "PUSH")
assert.Equal(t, paredQuery.Args[0], "testSet")
assert.Equal(t, paredQuery.Args[1], "testSubSet")
assert.Equal(t, paredQuery.Args[2], "hello")
assert.Equal(t, paredQuery.Args[3], "1700842078")
})

t.Run("empty query", func(t *testing.T) {
query := ""
paredQuery := ParseQuery(query)

assert.Equal(t, paredQuery.Command, "PUSH")
assert.Equal(t, paredQuery.Args[0], "testSet")
assert.Equal(t, paredQuery.Args[1], "testSubSet")
assert.Equal(t, paredQuery.Args[2], "hello")
assert.Equal(t, paredQuery.Args[3], "1700842078")
assert.Equal(t, "", paredQuery.Command)
assert.Equal(t, 0, len(paredQuery.Args))
})
}
13 changes: 13 additions & 0 deletions doc/TQL/TQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ Time trace is using a query language called TQL. Here is documentation and speci
| SSNF | subset is not found |
| ENF | element(s) is not found |
| [DATA separated by space] (key-time key-time key-time) | GET successful response |


# Example

Here is a set of examples to understand TQL and idea behind it:

E1:
```
SET myset
SSET myset mysset
PUSH myset mysset hello 123456789
GET myset mysset 1
```
File renamed without changes.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/zurvan-lab/TimeTrace

go 1.21.1
go 1.22.2

require (
github.com/rs/zerolog v1.31.0
github.com/rs/zerolog v1.32.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit f977919

Please sign in to comment.