Skip to content

Commit

Permalink
Go: Implementing Hash commands (valkey-io#2226)
Browse files Browse the repository at this point in the history
* add HashCommands interface and tests for hash-related commands
* update method comments and style

---------

Signed-off-by: umit <[email protected]>
  • Loading branch information
umit authored Sep 23, 2024
1 parent 2547e0e commit 634f1c8
Show file tree
Hide file tree
Showing 7 changed files with 893 additions and 36 deletions.
150 changes: 133 additions & 17 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
// BaseClient defines an interface for methods common to both [GlideClient] and [GlideClusterClient].
type BaseClient interface {
StringCommands
HashCommands

// Close terminates the client by closing all associated resources.
Close()
Expand Down Expand Up @@ -146,63 +147,71 @@ func (client *baseClient) Set(key string, value string) (string, error) {
if err != nil {
return "", err
}
return handleStringResponse(result), nil

return handleStringResponse(result)
}

func (client *baseClient) SetWithOptions(key string, value string, options *SetOptions) (string, error) {
result, err := client.executeCommand(C.Set, append([]string{key, value}, options.toArgs()...))
if err != nil {
return "", err
}
return handleStringOrNullResponse(result), nil

return handleStringOrNullResponse(result)
}

func (client *baseClient) Get(key string) (string, error) {
result, err := client.executeCommand(C.Get, []string{key})
if err != nil {
return "", err
}
return handleStringOrNullResponse(result), nil

return handleStringOrNullResponse(result)
}

func (client *baseClient) MSet(keyValueMap map[string]string) (string, error) {
result, err := client.executeCommand(C.MSet, utils.MapToString(keyValueMap))
if err != nil {
return "", err
}
return handleStringResponse(result), nil

return handleStringResponse(result)
}

func (client *baseClient) MSetNX(keyValueMap map[string]string) (bool, error) {
result, err := client.executeCommand(C.MSetNX, utils.MapToString(keyValueMap))
if err != nil {
return false, err
}
return handleBooleanResponse(result), nil

return handleBooleanResponse(result)
}

func (client *baseClient) MGet(keys []string) ([]string, error) {
result, err := client.executeCommand(C.MGet, keys)
if err != nil {
return nil, err
}
return handleStringArrayResponse(result), nil

return handleStringArrayResponse(result)
}

func (client *baseClient) Incr(key string) (int64, error) {
result, err := client.executeCommand(C.Incr, []string{key})
if err != nil {
return 0, err
}
return handleLongResponse(result), nil

return handleLongResponse(result)
}

func (client *baseClient) IncrBy(key string, amount int64) (int64, error) {
result, err := client.executeCommand(C.IncrBy, []string{key, utils.IntToString(amount)})
if err != nil {
return 0, err
}
return handleLongResponse(result), nil

return handleLongResponse(result)
}

func (client *baseClient) IncrByFloat(key string, amount float64) (float64, error) {
Expand All @@ -213,63 +222,71 @@ func (client *baseClient) IncrByFloat(key string, amount float64) (float64, erro
if err != nil {
return 0, err
}
return handleDoubleResponse(result), nil

return handleDoubleResponse(result)
}

func (client *baseClient) Decr(key string) (int64, error) {
result, err := client.executeCommand(C.Decr, []string{key})
if err != nil {
return 0, err
}
return handleLongResponse(result), nil

return handleLongResponse(result)
}

func (client *baseClient) DecrBy(key string, amount int64) (int64, error) {
result, err := client.executeCommand(C.DecrBy, []string{key, utils.IntToString(amount)})
if err != nil {
return 0, err
}
return handleLongResponse(result), nil

return handleLongResponse(result)
}

func (client *baseClient) Strlen(key string) (int64, error) {
result, err := client.executeCommand(C.Strlen, []string{key})
if err != nil {
return 0, err
}
return handleLongResponse(result), nil

return handleLongResponse(result)
}

func (client *baseClient) SetRange(key string, offset int, value string) (int64, error) {
result, err := client.executeCommand(C.SetRange, []string{key, strconv.Itoa(offset), value})
if err != nil {
return 0, err
}
return handleLongResponse(result), nil

return handleLongResponse(result)
}

func (client *baseClient) GetRange(key string, start int, end int) (string, error) {
result, err := client.executeCommand(C.GetRange, []string{key, strconv.Itoa(start), strconv.Itoa(end)})
if err != nil {
return "", err
}
return handleStringResponse(result), nil

return handleStringResponse(result)
}

func (client *baseClient) Append(key string, value string) (int64, error) {
result, err := client.executeCommand(C.Append, []string{key, value})
if err != nil {
return 0, err
}
return handleLongResponse(result), nil

return handleLongResponse(result)
}

func (client *baseClient) LCS(key1 string, key2 string) (string, error) {
result, err := client.executeCommand(C.LCS, []string{key1, key2})
if err != nil {
return "", err
}
return handleStringResponse(result), nil

return handleStringResponse(result)
}

func (client *baseClient) GetDel(key string) (string, error) {
Expand All @@ -282,5 +299,104 @@ func (client *baseClient) GetDel(key string) (string, error) {
return "", err
}

return handleStringOrNullResponse(result), nil
return handleStringOrNullResponse(result)
}

func (client *baseClient) HGet(key string, field string) (string, error) {
result, err := client.executeCommand(C.HGet, []string{key, field})
if err != nil {
return "", err
}

return handleStringOrNullResponse(result)
}

func (client *baseClient) HGetAll(key string) (map[string]string, error) {
result, err := client.executeCommand(C.HGetAll, []string{key})
if err != nil {
return nil, err
}

return handleStringToStringMapResponse(result)
}

func (client *baseClient) HMGet(key string, fields []string) ([]string, error) {
result, err := client.executeCommand(C.HMGet, append([]string{key}, fields...))
if err != nil {
return nil, err
}

return handleStringArrayResponse(result)
}

func (client *baseClient) HSet(key string, values map[string]string) (int64, error) {
result, err := client.executeCommand(C.HSet, utils.ConvertMapToKeyValueStringArray(key, values))
if err != nil {
return 0, err
}

return handleLongResponse(result)
}

func (client *baseClient) HSetNX(key string, field string, value string) (bool, error) {
result, err := client.executeCommand(C.HSetNX, []string{key, field, value})
if err != nil {
return false, err
}

return handleBooleanResponse(result)
}

func (client *baseClient) HDel(key string, fields []string) (int64, error) {
result, err := client.executeCommand(C.HDel, append([]string{key}, fields...))
if err != nil {
return 0, err
}

return handleLongResponse(result)
}

func (client *baseClient) HLen(key string) (int64, error) {
result, err := client.executeCommand(C.HLen, []string{key})
if err != nil {
return 0, err
}

return handleLongResponse(result)
}

func (client *baseClient) HVals(key string) ([]string, error) {
result, err := client.executeCommand(C.HVals, []string{key})
if err != nil {
return nil, err
}

return handleStringArrayResponse(result)
}

func (client *baseClient) HExists(key string, field string) (bool, error) {
result, err := client.executeCommand(C.HExists, []string{key, field})
if err != nil {
return false, err
}

return handleBooleanResponse(result)
}

func (client *baseClient) HKeys(key string) ([]string, error) {
result, err := client.executeCommand(C.HKeys, []string{key})
if err != nil {
return nil, err
}

return handleStringArrayResponse(result)
}

func (client *baseClient) HStrLen(key string, field string) (int64, error) {
result, err := client.executeCommand(C.HStrlen, []string{key, field})
if err != nil {
return 0, err
}

return handleLongResponse(result)
}
Loading

0 comments on commit 634f1c8

Please sign in to comment.