From 1ef6dcf30f916e2a5966f88d841ff7d791e06dde Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 16 Nov 2023 16:31:47 +0000 Subject: [PATCH] feat: parse query function for execution --- core/database/types.go | 5 +++++ core/execution.go | 27 +++++++++++++++++++++++++++ core/execution_test.go | 18 ++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 core/execution_test.go diff --git a/core/database/types.go b/core/database/types.go index fb19fb4..f9b46e9 100644 --- a/core/database/types.go +++ b/core/database/types.go @@ -7,6 +7,11 @@ type Element struct { time time.Time } +type Query struct { + Command string + Args []string +} + type ( Sets map[string]Set Set map[string]SubSet diff --git a/core/execution.go b/core/execution.go index 9a8bc95..147ec89 100644 --- a/core/execution.go +++ b/core/execution.go @@ -1 +1,28 @@ package core + +import ( + "strings" + + "github.com/zurvan-lab/TimeTrace/core/database" +) + +// parsing TQL queries. see: docs/TQL +func ParseQuery(query string) database.Query { + command := "" + args := []string{} + for _, word := range strings.Split(query, " ") { + if word == "" { + continue + } + if command != "" { + args = append(args, word) + } else { + command = word + } + } + return database.Query{Command: command, Args: args} +} + +func Execute(query database.Query, db database.Database) string { + return "" +} diff --git a/core/execution_test.go b/core/execution_test.go new file mode 100644 index 0000000..d020c94 --- /dev/null +++ b/core/execution_test.go @@ -0,0 +1,18 @@ +package core + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseQuery(t *testing.T) { + query := "PUSH testSet testSubSet hello NOW" + 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], "NOW") +}