-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Go: Implementing Hash commands #2226
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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() | ||||
|
@@ -146,63 +147,68 @@ 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 | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In some functions, extra spaces are added and somewhere it's not. Let's keep it consistent.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @janhavigupta007 I've updated with extra spaces. The separate return statement on a new line is generally considered better practice in Go. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this something that can be handled by |
||||
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) { | ||||
|
@@ -213,63 +219,67 @@ 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) { | ||||
|
@@ -282,5 +292,109 @@ 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 | ||||
} | ||||
|
||||
res, err := handleStringToStringMapResponse(result) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
|
||||
return res, nil | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can directly return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @janhavigupta007 It's done. |
||||
} | ||||
|
||||
func (client *baseClient) HMGet(key string, fields []string) ([]string, error) { | ||||
result, err := client.executeCommand(C.HMGet, append([]string{key}, fields...)) | ||||
if err != nil { | ||||
return []string{}, err | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @janhavigupta007 It's fixed. |
||||
} | ||||
|
||||
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) | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for fixing this 👍