aboutsummaryrefslogtreecommitdiff
path: root/pkg/commands/rpop.go
blob: 4ef4a1d0d9f21d8853df5d53774e436c5672443d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package commands

import (
	"errors"
	"redisClone/pkg/core"
)

func Rpop(args []string, getShard func(k string) *core.Shard) interface{}{
		key := args[1]
		s := getShard(key)
		item, exists := s.Data[key]
		if !exists {
			return nil
		}
		typedList, ok := item.Value.([]string)
		if !ok {
			return errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
		}
		if len(typedList) == 0 {
			return nil
		}
		newItem := typedList[len(typedList)-1]
		newList := typedList[:len(typedList)-1]
		if len(newList) <= 0 {
			delete(s.Data, key)
		} else {
			s.Data[key] = core.Item{Value: newList}
		}
		return newItem
}