aboutsummaryrefslogtreecommitdiff
path: root/pkg/commands/decr.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-11 11:57:44 +0200
committeralex <[email protected]>2026-07-11 11:57:44 +0200
commit1470772a00faad4a5b4130e0fe47513e5618e31d (patch)
tree981091c3175d1e5a62e8b8be5da4f6838b0b25d2 /pkg/commands/decr.go
parentc51e7f6d095153fa4d5358216271aeb4ad73ee0c (diff)
downloadredis-clone-1470772a00faad4a5b4130e0fe47513e5618e31d.tar.xz
redis-clone-1470772a00faad4a5b4130e0fe47513e5618e31d.zip
refactored some more commands to work with the new command registry
Diffstat (limited to 'pkg/commands/decr.go')
-rw-r--r--pkg/commands/decr.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/pkg/commands/decr.go b/pkg/commands/decr.go
new file mode 100644
index 0000000..c598b46
--- /dev/null
+++ b/pkg/commands/decr.go
@@ -0,0 +1,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
+ }
+} \ No newline at end of file