Skip to content

Commit

Permalink
Merge pull request #7 from marekm4/list-keys
Browse files Browse the repository at this point in the history
Add list operation
  • Loading branch information
marekm4 committed Dec 10, 2023
2 parents a168d82 + 558bde8 commit fcaccb5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"
)

type Database struct {
Expand All @@ -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}
Expand Down
15 changes: 15 additions & 0 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
}
11 changes: 11 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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}
}
Expand Down
3 changes: 2 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand Down
7 changes: 7 additions & 0 deletions websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fcaccb5

Please sign in to comment.