Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Add request parse key #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (req *Request) getKey() (string, bool) {
var key string

if req.Args.Next(&key) {
key = parseKey(key)
req.key = &key
req.Args = MultiArgs(List(key), req.Args)
}
Expand All @@ -99,3 +100,24 @@ func (req *Request) getKey() (string, bool) {

return *req.key, true
}

func parseKey(k string) string {
var key string
var s bool
for i := 0; i < len(k); i++ {
switch string(k[i]) {
case "{":
s = true
case "}":
break
default:
if s {
key += string(k[i])
}
}
}
if key == "" {
return k
}
return key
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to implement this more efficiently would be to use the strings.IndexByte function and return a subslice of the original string, do you want to give it a shot?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I wanted this kind of feedback :) doing it now :)

27 changes: 27 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package redis

import "testing"

func TestGetKey(t *testing.T) {
req1 := NewRequest("localhost:6379", "SET", List("foo{bar}", "42"))

k1, ok1 := req1.getKey()
if !ok1 {
t.Errorf("got no key for %s", req1.Args)
}

if k1 != "bar" {
t.Errorf("expected 1 got %s", k1)
}

req2 := NewRequest("localhost:6379", "SET", List("foo", "bar"))

k2, ok2 := req2.getKey()
if !ok2 {
t.Errorf("got no key for %s", req2.Args)
}

if k2 != "foo" {
t.Errorf("expected foo got %s", k2)
}
}