aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-05 22:09:51 +0200
committeralex <[email protected]>2026-07-05 22:09:51 +0200
commitefa1c05885a10ed5e2f3260bb7d1e96fdae39554 (patch)
tree7f00dd6b00c7fa390acb6f727727fb3c95ef0e2a
parent4af7ab55ede62dc6be3f7ac515adea93f3ad384c (diff)
downloadredis-clone-efa1c05885a10ed5e2f3260bb7d1e96fdae39554.tar.xz
redis-clone-efa1c05885a10ed5e2f3260bb7d1e96fdae39554.zip
refactored code to use serializeRESP function
-rw-r--r--main.go60
1 files changed, 34 insertions, 26 deletions
diff --git a/main.go b/main.go
index eb9ff36..510a0f0 100644
--- a/main.go
+++ b/main.go
@@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
+ "errors"
)
type RedisDB struct {
@@ -47,17 +48,18 @@ func handleGet(conn net.Conn, key string) {
value, exists := db.data[key]
if !exists {
- conn.Write([]byte("$-1\r\n"))
- return
+ conn.Write(serializeRESP(nil))
+ return
}
strValue, ok := value.(string)
if !ok {
- conn.Write([]byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"))
- return
+ err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ conn.Write(serializeRESP(err))
+ return
}
- conn.Write([]byte("+" + strValue + "\r\n"))
+ conn.Write(serializeRESP(strValue))
}
func handleExists(conn net.Conn, key string) {
@@ -66,9 +68,9 @@ func handleExists(conn net.Conn, key string) {
_, exists := db.data[key]
if exists {
- conn.Write([]byte(":1\r\n"))
+ conn.Write(serializeRESP(1))
} else {
- conn.Write([]byte(":0\r\n"))
+ conn.Write(serializeRESP(0))
}
}
@@ -77,7 +79,7 @@ func handleSet(conn net.Conn, key, value string) {
defer db.mu.Unlock()
db.data[key] = value
- conn.Write([]byte("+OK\r\n"))
+ conn.Write(serializeRESP(SimpleString("OK")))
}
func handleDel(conn net.Conn, key string) {
@@ -87,9 +89,9 @@ func handleDel(conn net.Conn, key string) {
_, exists := db.data[key]
if exists {
delete(db.data, key)
- conn.Write([]byte(":1\r\n"))
+ conn.Write(serializeRESP(1))
} else {
- conn.Write([]byte(":0\r\n"))
+ conn.Write(serializeRESP(0))
}
}
@@ -101,22 +103,24 @@ func handleIncr(conn net.Conn, key string) {
if exists {
strValue, ok := value.(string)
if !ok {
- conn.Write([]byte("-ERR value is not an integer or out of range\r\n"))
+ err := errors.New("value is not an integer or out of range")
+ conn.Write(serializeRESP(err))
return
}
currentInt, err := strconv.Atoi(strValue)
if err != nil {
- conn.Write([]byte("-ERR value is not an integer or out of range\r\n"))
+ err := errors.New("value is not an integer or out of range")
+ conn.Write(serializeRESP(err))
return
}
newValueStr := strconv.Itoa(currentInt + 1)
db.data[key] = newValueStr
- conn.Write([]byte(":" + newValueStr + "\r\n"))
+ conn.Write(serializeRESP(currentInt + 1))
} else {
db.data[key] = "1"
- conn.Write([]byte(":1\r\n"))
+ conn.Write(serializeRESP(1))
}
}
@@ -128,27 +132,29 @@ func handleDecr(conn net.Conn, key string) {
if exists {
strValue, ok := value.(string)
if !ok {
- conn.Write([]byte("-ERR value is not an integer or out of range\r\n"))
+ err := errors.New("value is not an integer or out of range")
+ conn.Write(serializeRESP(err))
return
}
currentInt, err := strconv.Atoi(strValue)
if err != nil {
- conn.Write([]byte("-ERR value is not an integer or out of range\r\n"))
+ err := errors.New("value is not an integer or out of range")
+ conn.Write(serializeRESP(err))
return
}
newValueStr := strconv.Itoa(currentInt - 1)
db.data[key] = newValueStr
- conn.Write([]byte(":" + newValueStr + "\r\n"))
+ conn.Write(serializeRESP(currentInt - 1))
} else {
db.data[key] = "-1"
- conn.Write([]byte(":-1\r\n"))
+ conn.Write(serializeRESP(-1))
}
}
func handlePing(conn net.Conn) {
- conn.Write([]byte("+PONG\r\n"))
+ conn.Write(serializeRESP(SimpleString("PONG")))
}
func handleFlushall(conn net.Conn) {
@@ -156,7 +162,7 @@ func handleFlushall(conn net.Conn) {
defer db.mu.Unlock()
db.data = make(map[string]any)
- conn.Write([]byte("+OK\r\n"))
+ conn.Write(serializeRESP(SimpleString("OK")))
}
func handleRpush(conn net.Conn, args []string) {
@@ -173,7 +179,8 @@ func handleRpush(conn net.Conn, args []string) {
var ok bool
list, ok = existingValue.([]string)
if !ok {
- conn.Write([]byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"))
+ err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ conn.Write(serializeRESP(err))
return
}
} else {
@@ -181,8 +188,7 @@ func handleRpush(conn net.Conn, args []string) {
}
list = append(list, newItems...)
db.data[key] = list
-
- conn.Write([]byte(":" + strconv.Itoa(len(list)) + "\r\n"))
+ conn.Write(serializeRESP(len(list)))
}
func handleLrange(conn net.Conn, key string, start, stop int) {
@@ -191,13 +197,15 @@ func handleLrange(conn net.Conn, key string, start, stop int) {
value, exists := db.data[key]
if !exists {
- conn.Write([]byte("*0\r\n"))
+ conn.Write(serializeRESP([]string{}))
+
return
}
typedList, ok := value.([]string)
if !ok {
- conn.Write([]byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"))
+ err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ conn.Write(serializeRESP(err))
return
}
@@ -222,7 +230,7 @@ func handleLrange(conn net.Conn, key string, start, stop int) {
}
if start > stop || start >= size {
- conn.Write([]byte("*0\r\n"))
+ conn.Write(serializeRESP([]string{}))
return
}