Skip to content

Commit

Permalink
Add argument error handling to server commands
Browse files Browse the repository at this point in the history
Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Feb 8, 2024
1 parent c4365cc commit b5e0727
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
7 changes: 6 additions & 1 deletion server/commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package server

import "log"
import (
"errors"
"log"
)

var ErrNotEnoughArgs = errors.New("not enough arguments for command")

func (c *Command) write(msg string) {
_, err := c.Conn.Write([]byte(msg + "\n"))
Expand Down
36 changes: 36 additions & 0 deletions server/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
)

func (s *Server) get(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
result, err := s.DB.Get(cmd.Args[0])
if err != nil {
cmd.error(err)
Expand All @@ -16,13 +20,21 @@ func (s *Server) get(cmd Command) {
}

func (s *Server) set(cmd Command) {
if len(cmd.Args) < 2 {
cmd.error(ErrNotEnoughArgs)
return
}
err := s.DB.Set(cmd.Args[0], []byte(cmd.Args[1]))
if err != nil {
cmd.error(err)
}
}

func (s *Server) setEx(cmd Command) {
if len(cmd.Args) < 3 {
cmd.error(ErrNotEnoughArgs)
return
}
ttl, err := strconv.Atoi(cmd.Args[2])
if err != nil {
cmd.error(err)
Expand All @@ -34,6 +46,10 @@ func (s *Server) setEx(cmd Command) {
}

func (s *Server) del(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
err := s.DB.Del(cmd.Args[0])
if err != nil {
cmd.error(err)
Expand All @@ -48,6 +64,10 @@ func (s *Server) mGet(cmd Command) {
}

func (s *Server) expire(cmd Command) {
if len(cmd.Args) < 2 {
cmd.error(ErrNotEnoughArgs)
return
}
expiration, err := strconv.Atoi(cmd.Args[1])
if err != nil {
cmd.error(err)
Expand All @@ -66,6 +86,10 @@ func (s *Server) keys(cmd Command) {
}

func (s *Server) exists(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
ok := s.DB.Exists(cmd.Args[0])
if !ok {
cmd.write("FALSE")
Expand All @@ -75,13 +99,21 @@ func (s *Server) exists(cmd Command) {
}

func (s *Server) persist(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
err := s.DB.Persist(cmd.Args[0])
if err != nil {
cmd.error(err)
}
}

func (s *Server) expireTime(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
exp, err := s.DB.ExpireTime(cmd.Args[0])
if err != nil {
cmd.error(err)
Expand All @@ -91,6 +123,10 @@ func (s *Server) expireTime(cmd Command) {
}

func (s *Server) ttl(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
ttl, err := s.DB.TTL(cmd.Args[0])
if err != nil {
cmd.error(err)
Expand Down
24 changes: 24 additions & 0 deletions server/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import (
)

func (s *Server) sAdd(cmd Command) {
if len(cmd.Args) < 2 {
cmd.error(ErrNotEnoughArgs)
return
}
s.DB.SAdd(cmd.Args[0], cmd.Args[1])
}

func (s *Server) sMembers(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
elements, err := s.DB.SMembers(cmd.Args[0])
if err != nil {
cmd.error(err)
Expand All @@ -21,6 +29,10 @@ func (s *Server) sMembers(cmd Command) {
}

func (s *Server) sCard(cmd Command) {
if len(cmd.Args) < 1 {
cmd.error(ErrNotEnoughArgs)
return
}
size, err := s.DB.SCard(cmd.Args[0])
if err != nil {
cmd.error(err)
Expand All @@ -30,6 +42,10 @@ func (s *Server) sCard(cmd Command) {
}

func (s *Server) sIsMember(cmd Command) {
if len(cmd.Args) < 2 {
cmd.error(ErrNotEnoughArgs)
return
}
ok, err := s.DB.SIsMember(cmd.Args[0], cmd.Args[1])
if err != nil {
cmd.error(err)
Expand All @@ -43,6 +59,10 @@ func (s *Server) sIsMember(cmd Command) {
}

func (s *Server) sDiff(cmd Command) {
if len(cmd.Args) < 2 {
cmd.error(ErrNotEnoughArgs)
return
}
elements, err := s.DB.SDiff(cmd.Args[0], cmd.Args[1])
if err != nil {
cmd.error(err)
Expand All @@ -54,6 +74,10 @@ func (s *Server) sDiff(cmd Command) {
}

func (s *Server) sDiffStore(cmd Command) {
if len(cmd.Args) < 3 {
cmd.error(ErrNotEnoughArgs)
return
}
err := s.DB.SDiffStore(cmd.Args[0], cmd.Args[1], cmd.Args[2])
if err != nil {
cmd.error(err)
Expand Down

0 comments on commit b5e0727

Please sign in to comment.