diff --git a/go/api/base_client.go b/go/api/base_client.go index d691bd3212..df51e4b1ba 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -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) { diff --git a/go/api/response_handlers.go b/go/api/response_handlers.go index 813f6034ab..2818f4cc25 100644 --- a/go/api/response_handlers.go +++ b/go/api/response_handlers.go @@ -7,6 +7,7 @@ package api import "C" import ( + "errors" "unsafe" ) @@ -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. @@ -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 { diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index ac6bd42c74..060692cda4 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -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) }) } @@ -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)