Skip to content

Commit

Permalink
refactor nil checks and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
umit committed Sep 5, 2024
1 parent 3462925 commit 70723d9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
9 changes: 7 additions & 2 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,15 @@ func (client *baseClient) HGet(key, field string) (string, error) {
func (client *baseClient) HGetAll(key string) (map[string]string, error) {
result, err := client.executeCommand(C.HGetAll, []string{key})
if err != nil {
return map[string]string{}, err
return nil, err
}

res, err := handleStringMapResponse(result)
if err != nil {
return nil, err
}

return handleStringMapResponse(result), nil
return res, nil
}

func (client *baseClient) HMGet(key string, fields []string) ([]string, error) {
Expand Down
20 changes: 11 additions & 9 deletions go/api/response_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package api
import "C"

import (
"errors"
"unsafe"
)

Expand Down Expand Up @@ -44,19 +45,20 @@ func handleBooleanResponse(response *C.struct_CommandResponse) bool {
}

// handleStringMapResponse processes a C.struct_CommandResponse to extract an array of Go strings.
func handleStringMapResponse(response *C.struct_CommandResponse) map[string]string {
if response == nil { // Check if the response is nil
return nil
}

func handleStringMapResponse(response *C.struct_CommandResponse) (map[string]string, error) {
defer C.free_command_response(response)

// Preallocate slice for lengths.
lengths := unsafe.Slice(response.array_elements_len, response.array_value_len)
numElements := int(response.array_value_len)
if numElements == 0 || numElements%2 != 0 {
// Handle inconsistent array lengths (just in case)
return map[string]string{}
// Handle empty array case.
if numElements == 0 {
return nil, nil
}

// Handle inconsistent array lengths (just in case).
if numElements%2 != 0 {
return nil, errors.New("the number of elements must be even")
}

// Preallocate map for strings with the known size.
Expand All @@ -67,7 +69,7 @@ func handleStringMapResponse(response *C.struct_CommandResponse) map[string]stri
res[convertCharArrayToString(values[i], lengths[i])] = convertCharArrayToString(values[i+1], lengths[i+1])
}

return res
return res, nil
}

func handleStringArrayResponse(response *C.struct_CommandResponse) []string {
Expand Down
4 changes: 2 additions & 2 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (suite *GlideTestSuite) TestHGetAll_WithNotExistingKey() {

res, err := client.HGetAll(key)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), map[string]string{}, res)
assert.Empty(suite.T(), res)
})
}

Expand Down Expand Up @@ -460,7 +460,7 @@ func (suite *GlideTestSuite) TestHDel() {

res3, err := client.HGetAll(key)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), map[string]string{}, res3)
assert.Empty(suite.T(), res3)

res4, err := client.HDel(key, []string{"field1", "field2"})
assert.Nil(suite.T(), err)
Expand Down

0 comments on commit 70723d9

Please sign in to comment.