diff options
| author | alex <[email protected]> | 2026-07-06 13:57:05 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-06 13:57:05 +0200 |
| commit | 48df6e2f7c36a859e7d76de23017eef84452c0ed (patch) | |
| tree | 40458db47a28e07b17ef8cf8f2957186f4c496a0 | |
| parent | 6745833aba4799104f299704af261696ce82b291 (diff) | |
| download | redis-clone-48df6e2f7c36a859e7d76de23017eef84452c0ed.tar.xz redis-clone-48df6e2f7c36a859e7d76de23017eef84452c0ed.zip | |
main loop now uses map to check which handler to use
| -rw-r--r-- | main.go | 48 |
1 files changed, 24 insertions, 24 deletions
@@ -2,6 +2,7 @@ package main import ( "bufio" + "errors" "fmt" "net" "os" @@ -11,6 +12,23 @@ import ( var db RedisDB +type handlerFunc func(net.Conn, []string) + +var commandRegistry = map[string]handlerFunc{ + "GET": handleGet, + "SET": handleSet, + "DEL": handleDel, + "EXISTS": handleExists, + "INCR": handleIncr, + "DECR": handleDecr, + "PING": handlePing, + "FLUSHALL": handleFlushall, + "RPUSH": handleRpush, + "LRANGE": handleLrange, + "LPOP": handleLpop, + "RPOP": handleRpop, +} + func main() { db = RedisDB{data: make(map[string]any)} @@ -52,29 +70,11 @@ func handleConnection(conn net.Conn) { command := strings.ToUpper(args[0]) - switch { - case command == "GET" && len(args) == 2: - handleGet(conn, args) - case command == "SET" && len(args) == 3: - handleSet(conn, args) - case command == "EXISTS" && len(args) == 2: - handleExists(conn, args) - case command == "DEL" && len(args) == 2: - handleDel(conn, args) - case command == "INCR" && len(args) == 2: - handleIncr(conn, args) - case command == "DECR" && len(args) == 2: - handleDecr(conn, args) - case command == "PING" && len(args) == 1: - handlePing(conn, args) - case command == "FLUSHALL" && len(args) == 1: - handleFlushall(conn, args) - case command == "RPUSH" && len(args) >= 3: - handleRpush(conn, args) - case command == "LRANGE" && len(args) == 4: - handleLrange(conn, args) - default: - conn.Write([]byte("-ERR unknown command or wrong arguments\r\n")) - } + if handler, ok := commandRegistry[command]; ok { + handler(conn, args) + } else { + conn.Write(serializeRESP(errors.New("unknown command"))) + } + } }
\ No newline at end of file |
