Skip to content

Commit

Permalink
Merge pull request #14 from absolutelightning/no-error
Browse files Browse the repository at this point in the history
Execution hook does not produce error
  • Loading branch information
absolutelightning authored Oct 2, 2024
2 parents 853e176 + b6d9e59 commit cdc5438
Show file tree
Hide file tree
Showing 65 changed files with 228 additions and 219 deletions.
8 changes: 6 additions & 2 deletions commands/db_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func validateDBSize() ValidationHook {
}

func executeDBSize() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return store.Size()
return func(args []string, store store.Store) string {
res, err := store.Size()
if err != nil {
return err.Error()
}
return res
}
}
7 changes: 4 additions & 3 deletions commands/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"

"treds/store"
)

Expand All @@ -26,11 +27,11 @@ func validateDel() ValidationHook {
}

func executeDel() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
err := store.Delete(args[0])
if err != nil {
return "", err
return err.Error()
}
return "OK\n", nil
return "OK\n"
}
}
6 changes: 3 additions & 3 deletions commands/del_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func validateDeletePrefix() ValidationHook {
}

func executeDeletePrefix() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
numDel, err := store.DeletePrefix(args[0])
if err != nil {
return "", err
return err.Error()
}
return strconv.Itoa(numDel), nil
return strconv.Itoa(numDel)
}
}
7 changes: 2 additions & 5 deletions commands/del_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ func TestExecuteDeletePrefix(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executionHook := executeDeletePrefix()
result, err := executionHook(tt.args, tt.store)
if (err != nil) != tt.expectErr {
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
}
if err == nil && result != tt.expectedMsg {
result := executionHook(tt.args, tt.store)
if result != tt.expectedMsg {
t.Errorf("expected result: %s, got: %s", tt.expectedMsg, result)
}
})
Expand Down
7 changes: 2 additions & 5 deletions commands/del_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ func TestExecuteDel(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executionHook := executeDel()
result, err := executionHook(tt.args, tt.store)
if (err != nil) != tt.expectErr {
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
}
if err == nil && result != tt.expectedMsg {
result := executionHook(tt.args, tt.store)
if result != tt.expectedMsg {
t.Errorf("expected result: %s, got: %s", tt.expectedMsg, result)
}
})
Expand Down
8 changes: 6 additions & 2 deletions commands/expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ func validateExpireCommand() ValidationHook {
}

func executeExpireCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
seconds, _ := strconv.Atoi(args[1])
now := time.Now()
expiryTime := now.Add(time.Duration(seconds) * time.Second)
return "OK\n", store.Expire(key, expiryTime)
err := store.Expire(key, expiryTime)
if err != nil {
return err.Error()
}
return "OK\n"
}
}
8 changes: 6 additions & 2 deletions commands/flush_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ func RegisterFlushAllCommand(r CommandRegistry) {
}

func executeFlushAll() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return "OK\n", store.FlushAll()
return func(args []string, store store.Store) string {
err := store.FlushAll()
if err != nil {
return err.Error()
}
return "OK\n"
}
}
8 changes: 6 additions & 2 deletions commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func validateGet() ValidationHook {
}

func executeGet() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return store.Get(args[0])
return func(args []string, store store.Store) string {
res, err := store.Get(args[0])
if err != nil {
return err.Error()
}
return res
}
}
9 changes: 2 additions & 7 deletions commands/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,9 @@ func TestExecuteGet(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executionHook := executeGet()
result, err := executionHook(tt.args, tt.store)
if (err != nil) != tt.expectErr {
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
}
if err == nil && result != tt.expectedMsg {
result := executionHook(tt.args, tt.store)
if result != tt.expectedMsg {
t.Errorf("expected result: %s, got: %s", tt.expectedMsg, result)
} else if err != nil && err.Error() != tt.expectedMsg {
t.Errorf("expected error message: %s, got: %s", tt.expectedMsg, err.Error())
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions commands/hdel.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func validateHDelCommand() ValidationHook {
}

func executeHDelCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
err := store.HDel(key, args[1:])
if err != nil {
return "", err
return err.Error()
}
return "OK\n", nil
return "OK\n"
}
}
6 changes: 3 additions & 3 deletions commands/hexists.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func validateHExistsCommand() ValidationHook {
}

func executeHExistsCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
found, err := store.HExists(key, args[1])
if err != nil {
return "", err
return err.Error()
}
return strconv.FormatBool(found), nil
return strconv.FormatBool(found)
}
}
8 changes: 6 additions & 2 deletions commands/hget.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ func validateHGetCommand() ValidationHook {
}

func executeHGetCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
return store.HGet(key, args[1])
res, err := store.HGet(key, args[1])
if err != nil {
return err.Error()
}
return res
}
}
8 changes: 6 additions & 2 deletions commands/hgetall.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ func validateHGetAllCommand() ValidationHook {
}

func executeHGetAllCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
return store.HGetAll(key)
res, err := store.HGetAll(key)
if err != nil {
return err.Error()
}
return res
}
}
8 changes: 6 additions & 2 deletions commands/hkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ func validateHKeysCommand() ValidationHook {
}

func executeHKeysCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
return store.HKeys(key)
res, err := store.HKeys(key)
if err != nil {
return err.Error()
}
return res
}
}
6 changes: 3 additions & 3 deletions commands/hlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func validateHLenCommand() ValidationHook {
}

func executeHLenCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
size, err := store.HLen(key)
if err != nil {
return "", err
return err.Error()
}
return strconv.Itoa(size), nil
return strconv.Itoa(size)
}
}
6 changes: 3 additions & 3 deletions commands/hset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func validateHSetCommand() ValidationHook {
}

func executeHSetCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
err := store.HSet(key, args[1:])
if err != nil {
return "", err
return err.Error()
}
return "OK\n", nil
return "OK\n"
}
}
8 changes: 6 additions & 2 deletions commands/hvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ func validateHValsCommand() ValidationHook {
}

func executeHValsCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
return store.HVals(key)
res, err := store.HVals(key)
if err != nil {
return err.Error()
}
return res
}
}
6 changes: 3 additions & 3 deletions commands/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func validateKeys() ValidationHook {
}

func executeKeys() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
regex := ""
count := math.MaxInt64
if len(args) >= 2 {
Expand All @@ -50,8 +50,8 @@ func executeKeys() ExecutionHook {
}
v, err := store.Keys(args[0], regex, count)
if err != nil {
return "", err
return err.Error()
}
return fmt.Sprintf("%v", v), nil
return fmt.Sprintf("%v", v)
}
}
9 changes: 2 additions & 7 deletions commands/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,9 @@ func TestExecuteKeys(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executionHook := executeKeys()
result, err := executionHook(tt.args, tt.store)
if (err != nil) != tt.expectErr {
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
}
if err == nil && result != tt.expectedMsg {
result := executionHook(tt.args, tt.store)
if result != tt.expectedMsg {
t.Errorf("expected result: %s, got: %s", tt.expectedMsg, result)
} else if err != nil && err.Error() != tt.expectedMsg {
t.Errorf("expected error message: %s, got: %s", tt.expectedMsg, err.Error())
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions commands/kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func validateKVS() ValidationHook {
}

func executeKVS() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
regex := ""
count := math.MaxInt64
if len(args) >= 2 {
Expand All @@ -50,8 +50,8 @@ func executeKVS() ExecutionHook {
}
v, err := store.KVS(args[0], regex, count)
if err != nil {
return "", err
return err.Error()
}
return fmt.Sprintf("%v", v), nil
return fmt.Sprintf("%v", v)
}
}
9 changes: 2 additions & 7 deletions commands/kvs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,9 @@ func TestExecuteKVS(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executionHook := executeKVS()
result, err := executionHook(tt.args, tt.store)
if (err != nil) != tt.expectErr {
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
}
if err == nil && result != tt.expectedMsg {
result := executionHook(tt.args, tt.store)
if result != tt.expectedMsg {
t.Errorf("expected result: %s, got: %s", tt.expectedMsg, result)
} else if err != nil && err.Error() != tt.expectedMsg {
t.Errorf("expected error message: %s, got: %s", tt.expectedMsg, err.Error())
}
})
}
Expand Down
8 changes: 6 additions & 2 deletions commands/lindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func validateLIndexCommand() ValidationHook {
}

func executeLIndexCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return store.LIndex(args)
return func(args []string, store store.Store) string {
res, err := store.LIndex(args)
if err != nil {
return err.Error()
}
return res
}
}
8 changes: 6 additions & 2 deletions commands/llen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ func validateLLenCommand() ValidationHook {
}

func executeLLenCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
key := args[0]
return store.LLen(key)
res, err := store.LLen(key)
if err != nil {
return err.Error()
}
return res
}
}
8 changes: 6 additions & 2 deletions commands/lng_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ func RegisterLongestPrefixCommand(r CommandRegistry) {
}

func executeLongestPrefixCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
return func(args []string, store store.Store) string {
prefix := args[0]
return store.LongestPrefix(prefix)
res, err := store.LongestPrefix(prefix)
if err != nil {
return err.Error()
}
return res
}
}
Loading

0 comments on commit cdc5438

Please sign in to comment.