From 558bde89b80a548a6329796af41af7dd06b3b1c5 Mon Sep 17 00:00:00 2001 From: Marek Michalik Date: Sun, 10 Dec 2023 01:25:12 +0100 Subject: [PATCH] Add list operation --- README.md | 5 +++++ database.go | 21 +++++++++++++++++++++ database_test.go | 15 +++++++++++++++ query.go | 11 +++++++++++ query_test.go | 3 ++- websocket_test.go | 7 +++++++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b256312..caffe6a 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ By default, it uses port `8080`, this can be changed by setting up `PORT` env. D See [websocket_test.go](websocket_test.go) for more examples. ### Queries +#### List values +``` +list user +``` + #### Get value ``` select username diff --git a/database.go b/database.go index 7581834..32675f9 100644 --- a/database.go +++ b/database.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" ) type Database struct { @@ -14,6 +15,26 @@ func NewDatabase() Database { return Database{make(map[string]string), make(map[string]float64), make(map[string][]string)} } +func (d Database) List(prefix string) []string { + keys := []string{} + for key := range d.Values { + if strings.HasPrefix(key, prefix) { + keys = append(keys, key) + } + } + for key := range d.Counters { + if strings.HasPrefix(key, prefix) { + keys = append(keys, key) + } + } + for key := range d.Lists { + if strings.HasPrefix(key, prefix) { + keys = append(keys, key) + } + } + return keys +} + func (d Database) Select(key string) []string { if value, ok := d.Values[key]; ok { return []string{value} diff --git a/database_test.go b/database_test.go index 0c17404..ff9efe4 100644 --- a/database_test.go +++ b/database_test.go @@ -92,3 +92,18 @@ func TestDatabase_Clear(t *testing.T) { values := database.Select(key) assert.DeepEqual(t, values, []string{""}) } + +func TestDatabase_List(t *testing.T) { + // Given database with user data + database := NewDatabase() + database.Update("user_name", "John") + database.Increment("user_money", 100.0) + database.Append("user_orders", "order 1") + database.Increment("account_money", 1000.0) + + // When we get user keys + keys := database.List("user_") + + // Then user keys are returned + assert.DeepEqual(t, keys, []string{"user_name", "user_money", "user_orders"}) +} diff --git a/query.go b/query.go index f9e61d4..13c7de3 100644 --- a/query.go +++ b/query.go @@ -16,6 +16,14 @@ func (q EmptyQuery) Execute(_ Database) []string { return []string{} } +type ListQuery struct { + Key string +} + +func (q ListQuery) Execute(database Database) []string { + return database.List(q.Key) +} + type SelectQuery struct { Key string } @@ -61,6 +69,9 @@ func ParseQuery(query string) Query { } operation := query[:i] key := query[i+1:] + if operation == "list" { + return ListQuery{key} + } if operation == "select" { return SelectQuery{key} } diff --git a/query_test.go b/query_test.go index 96937a3..e15fb4e 100644 --- a/query_test.go +++ b/query_test.go @@ -10,7 +10,8 @@ func TestParseQuery(t *testing.T) { Query string Expected Query }{ - "Empty": {"select", EmptyQuery{}}, + "Empty": {"list", EmptyQuery{}}, + "List": {"list user", ListQuery{"user"}}, "Select": {"select user_name", SelectQuery{"user_name"}}, "Update": {"update user_name john", UpdateQuery{"user_name", "john"}}, "Increment": {"increment user_money 5.5", IncrementQuery{"user_money", 5.5}}, diff --git a/websocket_test.go b/websocket_test.go index dcd531b..679547a 100644 --- a/websocket_test.go +++ b/websocket_test.go @@ -30,6 +30,13 @@ func TestWebsocket(t *testing.T) { assert.NilError(t, err) assert.Equal(t, string(message), "john") + // Then value is on the list + err = ws.WriteMessage(websocket.TextMessage, []byte("list user")) + assert.NilError(t, err) + _, message, err = ws.ReadMessage() + assert.NilError(t, err) + assert.Equal(t, string(message), "username") + // When we increment value err = ws.WriteMessage(websocket.TextMessage, []byte("increment money 100")) assert.NilError(t, err)