aboutsummaryrefslogtreecommitdiff
path: root/pkg/commands/decr.go
blob: c598b46757ad1aabd1a2e6e805e239d14cdc3907 (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
31
32
33
package commands

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

func Decr(args []string, getShard func(k string) *core.Shard) interface{}{
	key := args[1]
	shard := getShard(key)
	item, exists := shard.Data[key]
		if exists {
			strValue, ok := item.Value.(string)
			if !ok {
				err := errors.New("value is not an integer or out of range")
				return err
			}

			currentInt, err := strconv.Atoi(strValue)
			if err != nil {
				err := errors.New("value is not an integer or out of range")
				return err
			}

			newValueStr := strconv.Itoa(currentInt - 1)
			shard.Data[key] = core.Item{Value: newValueStr}
			return currentInt - 1
		} else {
			shard.Data[key] = core.Item{Value: "-1"}
			return -1
		}
}