diff --git a/src/modules/acl/commands.go b/src/modules/acl/commands.go index 0bc0548..00be0fc 100644 --- a/src/modules/acl/commands.go +++ b/src/modules/acl/commands.go @@ -64,12 +64,12 @@ func (p Plugin) HandleCommand(ctx context.Context, cmd []string, server utils.Se func (p Plugin) handleAuth(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) { if len(cmd) < 2 || len(cmd) > 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } if err := p.acl.AuthenticateConnection(conn, cmd); err != nil { return nil, err } - return []byte("+OK\r\n\n"), nil + return []byte(utils.OK_RESPONSE), nil } func (p Plugin) handleGetUser(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) { diff --git a/src/modules/etc/commands.go b/src/modules/etc/commands.go index 437714a..0d4fd6e 100644 --- a/src/modules/etc/commands.go +++ b/src/modules/etc/commands.go @@ -54,7 +54,7 @@ func handleSet(ctx context.Context, cmd []string, s utils.Server) ([]byte, error switch x := len(cmd); { default: - return nil, errors.New("wrong number of args for SET command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) case x == 3: key := cmd[1] @@ -63,7 +63,7 @@ func handleSet(ctx context.Context, cmd []string, s utils.Server) ([]byte, error s.CreateKeyAndLock(ctx, key) s.SetValue(ctx, key, utils.AdaptType(cmd[2])) s.KeyUnlock(key) - return []byte("+OK\r\n\n"), nil + return []byte(utils.OK_RESPONSE), nil } if _, err := s.KeyLock(ctx, key); err != nil { @@ -72,14 +72,14 @@ func handleSet(ctx context.Context, cmd []string, s utils.Server) ([]byte, error s.SetValue(ctx, key, utils.AdaptType(cmd[2])) s.KeyUnlock(key) - return []byte("+OK\r\n\n"), nil + return []byte(utils.OK_RESPONSE), nil } } func handleSetNX(ctx context.Context, cmd []string, s utils.Server) ([]byte, error) { switch x := len(cmd); { default: - return nil, errors.New("wrong number of args for SETNX command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) case x == 3: key := cmd[1] if s.KeyExists(key) { @@ -90,7 +90,7 @@ func handleSetNX(ctx context.Context, cmd []string, s utils.Server) ([]byte, err s.SetValue(ctx, key, utils.AdaptType(cmd[2])) s.KeyUnlock(key) } - return []byte("+OK\r\n\n"), nil + return []byte(utils.OK_RESPONSE), nil } func handleMSet(ctx context.Context, cmd []string, s utils.Server) ([]byte, error) { @@ -148,7 +148,7 @@ func handleMSet(ctx context.Context, cmd []string, s utils.Server) ([]byte, erro s.SetValue(ctx, k, v.value) } - return []byte("+OK\r\n\n"), nil + return []byte(utils.OK_RESPONSE), nil } func NewModule() Plugin { @@ -162,7 +162,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -174,7 +174,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, diff --git a/src/modules/get/commands.go b/src/modules/get/commands.go index fe03d13..26dc980 100644 --- a/src/modules/get/commands.go +++ b/src/modules/get/commands.go @@ -43,7 +43,7 @@ func (p Plugin) HandleCommand(ctx context.Context, cmd []string, server utils.Se func handleGet(ctx context.Context, cmd []string, s utils.Server) ([]byte, error) { if len(cmd) != 2 { - return nil, errors.New("wrong number of args for GET command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } key := cmd[1] @@ -62,7 +62,7 @@ func handleGet(ctx context.Context, cmd []string, s utils.Server) ([]byte, error func handleMGet(ctx context.Context, cmd []string, s utils.Server) ([]byte, error) { if len(cmd) < 2 { - return nil, errors.New("wrong number of args for MGET command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } vals := []string{} @@ -103,7 +103,7 @@ func NewModule() Plugin { Sync: false, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -115,7 +115,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) < 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return cmd[1:], nil }, diff --git a/src/modules/list/commands.go b/src/modules/list/commands.go index a561c40..602731a 100644 --- a/src/modules/list/commands.go +++ b/src/modules/list/commands.go @@ -11,10 +11,6 @@ import ( "strings" ) -const ( - OK = "+OK\r\n\n" -) - type Plugin struct { name string commands []utils.Command @@ -76,7 +72,7 @@ func (p Plugin) HandleCommand(ctx context.Context, cmd []string, server utils.Se func handleLLen(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 2 { - return nil, errors.New("wrong number of args for LLEN command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } if !server.KeyExists(cmd[1]) { @@ -97,7 +93,7 @@ func handleLLen(ctx context.Context, cmd []string, server utils.Server) ([]byte, func handleLIndex(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 3 { - return nil, errors.New("wrong number of args for LINDEX command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } index, ok := utils.AdaptType(cmd[2]).(int64) @@ -127,7 +123,7 @@ func handleLIndex(ctx context.Context, cmd []string, server utils.Server) ([]byt func handleLRange(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments for LRANGE command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } start, startOk := utils.AdaptType(cmd[2]).(int64) @@ -206,7 +202,7 @@ func handleLRange(ctx context.Context, cmd []string, server utils.Server) ([]byt func handleLSet(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments for LSET command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } if !server.KeyExists(cmd[1]) { @@ -239,12 +235,12 @@ func handleLSet(ctx context.Context, cmd []string, server utils.Server) ([]byte, server.SetValue(ctx, cmd[1], list) server.KeyUnlock(cmd[1]) - return []byte(OK), nil + return []byte(utils.OK_RESPONSE), nil } func handleLTrim(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of args for command LTRIM") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } start, startOk := utils.AdaptType(cmd[2]).(int64) @@ -276,17 +272,17 @@ func handleLTrim(ctx context.Context, cmd []string, server utils.Server) ([]byte if end == -1 || int(end) > len(list) { server.SetValue(ctx, cmd[1], list[start:]) server.KeyUnlock(cmd[1]) - return []byte(OK), nil + return []byte(utils.OK_RESPONSE), nil } server.SetValue(ctx, cmd[1], list[start:end]) server.KeyUnlock(cmd[1]) - return []byte(OK), nil + return []byte(utils.OK_RESPONSE), nil } func handleLRem(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments for LREM command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } value := cmd[3] @@ -343,12 +339,12 @@ func handleLRem(ctx context.Context, cmd []string, server utils.Server) ([]byte, server.SetValue(ctx, cmd[1], list) server.KeyUnlock(cmd[1]) - return []byte(OK), nil + return []byte(utils.OK_RESPONSE), nil } func handleLMove(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 5 { - return nil, errors.New("wrong number of arguments for LMOVE command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } whereFrom := strings.ToLower(cmd[3]) @@ -392,12 +388,12 @@ func handleLMove(ctx context.Context, cmd []string, server utils.Server) ([]byte server.KeyUnlock(cmd[1]) server.KeyUnlock(cmd[2]) - return []byte(OK), nil + return []byte(utils.OK_RESPONSE), nil } func handleLPush(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) < 3 { - return nil, fmt.Errorf("wrong number of arguments for %s command", strings.ToUpper(cmd[0])) + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } newElems := []interface{}{} @@ -430,12 +426,12 @@ func handleLPush(ctx context.Context, cmd []string, server utils.Server) ([]byte } server.SetValue(ctx, key, append(newElems, l...)) - return []byte(OK), nil + return []byte(utils.OK_RESPONSE), nil } func handleRPush(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) < 3 { - return nil, fmt.Errorf("wrong number of arguments for %s command", strings.ToUpper(cmd[0])) + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } newElems := []interface{}{} @@ -466,12 +462,12 @@ func handleRPush(ctx context.Context, cmd []string, server utils.Server) ([]byte } server.SetValue(ctx, cmd[1], append(l, newElems...)) - return []byte(OK), nil + return []byte(utils.OK_RESPONSE), nil } func handlePop(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd) != 2 { - return nil, fmt.Errorf("wrong number of args for %s command", strings.ToUpper(cmd[0])) + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } if !server.KeyExists(cmd[1]) { @@ -513,7 +509,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) < 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -525,7 +521,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -537,7 +533,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -549,7 +545,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -561,7 +557,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -573,7 +569,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -585,7 +581,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -597,7 +593,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -609,7 +605,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -621,7 +617,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 5 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1], cmd[2]}, nil }, @@ -633,7 +629,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -645,7 +641,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) < 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -657,7 +653,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, diff --git a/src/modules/ping/commands.go b/src/modules/ping/commands.go index 0467e39..1358b5b 100644 --- a/src/modules/ping/commands.go +++ b/src/modules/ping/commands.go @@ -8,10 +8,6 @@ import ( "strings" ) -const ( - OK = "+OK\r\n\n" -) - type Plugin struct { name string commands []utils.Command @@ -46,7 +42,7 @@ func (p Plugin) HandleCommand(ctx context.Context, cmd []string, server utils.Se func handlePing(ctx context.Context, cmd []string, s utils.Server) ([]byte, error) { switch len(cmd) { default: - return nil, errors.New("wrong number of arguments for PING command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) case 1: return []byte("+PONG\r\n\n"), nil case 2: diff --git a/src/modules/pubsub/commands.go b/src/modules/pubsub/commands.go index 5d1a057..3d06873 100644 --- a/src/modules/pubsub/commands.go +++ b/src/modules/pubsub/commands.go @@ -9,10 +9,6 @@ import ( "strings" ) -const ( - OK = "+OK\r\n\n" -) - type Plugin struct { name string commands []utils.Command @@ -60,7 +56,7 @@ func handleSubscribe(ctx context.Context, p Plugin, cmd []string, s utils.Server // Subscribe to specified channel and specified consumer group p.pubSub.Subscribe(ctx, conn, cmd[1], cmd[2]) default: - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []byte("+SUBSCRIBE_OK\r\n\n"), nil } @@ -72,9 +68,9 @@ func handleUnsubscribe(ctx context.Context, p Plugin, cmd []string, s utils.Serv case 2: p.pubSub.Unsubscribe(ctx, conn, cmd[1]) default: - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } - return []byte("+OK\r\n\n"), nil + return []byte(utils.OK_RESPONSE), nil } func handlePublish(ctx context.Context, p Plugin, cmd []string, s utils.Server) ([]byte, error) { @@ -83,7 +79,7 @@ func handlePublish(ctx context.Context, p Plugin, cmd []string, s utils.Server) } else if len(cmd) == 2 { p.pubSub.Publish(ctx, cmd[1], nil) } else { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []byte("+PUBLISH_OK\r\n\n"), nil } @@ -101,7 +97,7 @@ func NewModule(pubsub *PubSub) Plugin { KeyExtractionFunc: func(cmd []string) ([]string, error) { // Treat the channel as a key if len(cmd) != 3 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -114,7 +110,7 @@ func NewModule(pubsub *PubSub) Plugin { KeyExtractionFunc: func(cmd []string) ([]string, error) { // Treat the channel as a key if len(cmd) < 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -127,7 +123,7 @@ func NewModule(pubsub *PubSub) Plugin { KeyExtractionFunc: func(cmd []string) ([]string, error) { // Treat the channel as a key if len(cmd) != 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, diff --git a/src/modules/string/commands.go b/src/modules/string/commands.go index 87dc80b..7d4fdce 100644 --- a/src/modules/string/commands.go +++ b/src/modules/string/commands.go @@ -46,7 +46,7 @@ func (p Plugin) HandleCommand(ctx context.Context, cmd []string, server utils.Se func handleSetRange(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd[1:]) != 3 { - return nil, errors.New("wrong number of args for SETRANGE command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } key := cmd[1] @@ -112,7 +112,7 @@ func handleSetRange(ctx context.Context, cmd []string, server utils.Server) ([]b func handleStrLen(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd[1:]) != 1 { - return nil, errors.New("wrong number of args for STRLEN command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } key := cmd[1] @@ -137,7 +137,7 @@ func handleStrLen(ctx context.Context, cmd []string, server utils.Server) ([]byt func handleSubStr(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { if len(cmd[1:]) != 3 { - return nil, errors.New("wrong number of args for SUBSTR command") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } key := cmd[1] @@ -209,7 +209,7 @@ func NewModule() Plugin { Sync: true, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -221,7 +221,7 @@ func NewModule() Plugin { Sync: false, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 2 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -233,7 +233,7 @@ func NewModule() Plugin { Sync: false, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, @@ -245,7 +245,7 @@ func NewModule() Plugin { Sync: false, KeyExtractionFunc: func(cmd []string) ([]string, error) { if len(cmd) != 4 { - return nil, errors.New("wrong number of arguments") + return nil, errors.New(utils.WRONG_ARGS_RESPONSE) } return []string{cmd[1]}, nil }, diff --git a/src/utils/const.go b/src/utils/const.go new file mode 100644 index 0000000..1cdb20f --- /dev/null +++ b/src/utils/const.go @@ -0,0 +1,30 @@ +package utils + +const ( + AdminCategory = "admin" + BitmapCategory = "bitmap" + BlockingCategory = "blocking" + ConnectionCategory = "connection" + DangerousCategory = "dangerous" + GeoCategory = "geo" + HashCategory = "hash" + HyperLogLogCategory = "hyperloglog" + FastCategory = "fast" + KeyspaceCategory = "keyspace" + ListCategory = "list" + PubSubCategory = "pubsub" + ReadCategory = "read" + ScriptingCategory = "scripting" + SetCategory = "set" + SortedSetCategory = "sortedset" + SlowCategory = "slow" + StreamCategory = "stream" + StringCategory = "string" + TransactionCategory = "transaction" + WriteCategory = "write" +) + +const ( + OK_RESPONSE = "+OK\r\n\n" + WRONG_ARGS_RESPONSE = "wrong number of arguments" +) diff --git a/src/utils/types.go b/src/utils/types.go index 8d50e31..2db439b 100644 --- a/src/utils/types.go +++ b/src/utils/types.go @@ -60,29 +60,3 @@ type Plugin interface { Description() string HandleCommand(ctx context.Context, cmd []string, server Server, conn *net.Conn) ([]byte, error) } - -type CommandCategory string - -const ( - AdminCategory = "admin" - BitmapCategory = "bitmap" - BlockingCategory = "blocking" - ConnectionCategory = "connection" - DangerousCategory = "dangerous" - GeoCategory = "geo" - HashCategory = "hash" - HyperLogLogCategory = "hyperloglog" - FastCategory = "fast" - KeyspaceCategory = "keyspace" - ListCategory = "list" - PubSubCategory = "pubsub" - ReadCategory = "read" - ScriptingCategory = "scripting" - SetCategory = "set" - SortedSetCategory = "sortedset" - SlowCategory = "slow" - StreamCategory = "stream" - StringCategory = "string" - TransactionCategory = "transaction" - WriteCategory = "write" -)