Skip to content

Commit

Permalink
Merge pull request #213 from wszaranski/add-getdel-command
Browse files Browse the repository at this point in the history
Add GETDEL command
  • Loading branch information
alicebob authored Jun 13, 2021
2 parents e8f5b53 + d1c7b8c commit 8c22d3d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Implemented commands:
- GETBIT
- GETRANGE
- GETSET
- GETDEL
- INCR
- INCRBY
- INCRBYFLOAT
Expand Down
36 changes: 36 additions & 0 deletions cmd_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func commandsString(m *Miniredis) {
m.srv.Register("GET", m.cmdGet)
m.srv.Register("GETRANGE", m.cmdGetrange)
m.srv.Register("GETSET", m.cmdGetset)
m.srv.Register("GETDEL", m.cmdGetdel)
m.srv.Register("INCRBYFLOAT", m.cmdIncrbyfloat)
m.srv.Register("INCRBY", m.cmdIncrby)
m.srv.Register("INCR", m.cmdIncr)
Expand Down Expand Up @@ -415,6 +416,41 @@ func (m *Miniredis) cmdGetset(c *server.Peer, cmd string, args []string) {
})
}

// GETDEL
func (m *Miniredis) cmdGetdel(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}

withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)

key := args[0]

if !db.exists(key) {
c.WriteNull()
return
}

if db.t(key) != "string" {
c.WriteError(msgWrongType)
return
}

v := db.stringGet(key)
db.del(key, true)
c.WriteBulk(v)
})
}

// MGET
func (m *Miniredis) cmdMget(c *server.Peer, cmd string, args []string) {
if len(args) < 1 {
Expand Down
45 changes: 45 additions & 0 deletions cmd_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,51 @@ func TestGetSet(t *testing.T) {
}
}

func TestGetdel(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()

// Missing key
{
mustNil(t, c, "GETDEL", "foo")
}

// Existing key
{
s.Set("foo", "bar")
mustDo(t, c,
"GETDEL", "foo",
proto.String("bar"),
)
must0(t, c, "EXISTS", "foo")
}

// Wrong type of existing key
{
s.HSet("wrong", "foo", "bar")
mustDo(t, c,
"GETDEL", "wrong",
proto.Error(msgWrongType),
)
}

// Wrong usage
{
mustDo(t, c,
"GETDEL",
proto.Error(errWrongNumber("getdel")),
)
mustDo(t, c,
"GETDEL", "foo", "bar",
proto.Error(errWrongNumber("getdel")),
)
}
}

func TestStrlen(t *testing.T) {
s, err := Run()
ok(t, err)
Expand Down

0 comments on commit 8c22d3d

Please sign in to comment.