Skip to content

Commit

Permalink
Go: Fix return types, parn 5 (valkey-io#2964)
Browse files Browse the repository at this point in the history
Fix return types

Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand authored Jan 20, 2025
1 parent b95fbbd commit 502a8d7
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 284 deletions.
36 changes: 18 additions & 18 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (client *baseClient) HGet(key string, field string) (Result[string], error)
return handleStringOrNilResponse(result)
}

func (client *baseClient) HGetAll(key string) (map[Result[string]]Result[string], error) {
func (client *baseClient) HGetAll(key string) (map[string]string, error) {
result, err := client.executeCommand(C.HGetAll, []string{key})
if err != nil {
return nil, err
Expand Down Expand Up @@ -672,7 +672,7 @@ func (client *baseClient) SUnionStore(destination string, keys []string) (int64,
return handleIntResponse(result)
}

func (client *baseClient) SMembers(key string) (map[Result[string]]struct{}, error) {
func (client *baseClient) SMembers(key string) (map[string]struct{}, error) {
result, err := client.executeCommand(C.SMembers, []string{key})
if err != nil {
return nil, err
Expand All @@ -699,7 +699,7 @@ func (client *baseClient) SIsMember(key string, member string) (bool, error) {
return handleBoolResponse(result)
}

func (client *baseClient) SDiff(keys []string) (map[Result[string]]struct{}, error) {
func (client *baseClient) SDiff(keys []string) (map[string]struct{}, error) {
result, err := client.executeCommand(C.SDiff, keys)
if err != nil {
return nil, err
Expand All @@ -717,7 +717,7 @@ func (client *baseClient) SDiffStore(destination string, keys []string) (int64,
return handleIntResponse(result)
}

func (client *baseClient) SInter(keys []string) (map[Result[string]]struct{}, error) {
func (client *baseClient) SInter(keys []string) (map[string]struct{}, error) {
result, err := client.executeCommand(C.SInter, keys)
if err != nil {
return nil, err
Expand Down Expand Up @@ -782,7 +782,7 @@ func (client *baseClient) SMIsMember(key string, members []string) ([]bool, erro
return handleBoolArrayResponse(result)
}

func (client *baseClient) SUnion(keys []string) (map[Result[string]]struct{}, error) {
func (client *baseClient) SUnion(keys []string) (map[string]struct{}, error) {
result, err := client.executeCommand(C.SUnion, keys)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1025,7 +1025,7 @@ func (client *baseClient) LPushX(key string, elements []string) (int64, error) {
return handleIntResponse(result)
}

func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map[Result[string]][]Result[string], error) {
func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map[string][]string, error) {
listDirectionStr, err := listDirection.toString()
if err != nil {
return nil, err
Expand All @@ -1046,14 +1046,14 @@ func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
return handleStringToStringArrayMapOrNilResponse(result)
}

func (client *baseClient) LMPopCount(
keys []string,
listDirection ListDirection,
count int64,
) (map[Result[string]][]Result[string], error) {
) (map[string][]string, error) {
listDirectionStr, err := listDirection.toString()
if err != nil {
return nil, err
Expand All @@ -1074,14 +1074,14 @@ func (client *baseClient) LMPopCount(
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
return handleStringToStringArrayMapOrNilResponse(result)
}

func (client *baseClient) BLMPop(
keys []string,
listDirection ListDirection,
timeoutSecs float64,
) (map[Result[string]][]Result[string], error) {
) (map[string][]string, error) {
listDirectionStr, err := listDirection.toString()
if err != nil {
return nil, err
Expand All @@ -1102,15 +1102,15 @@ func (client *baseClient) BLMPop(
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
return handleStringToStringArrayMapOrNilResponse(result)
}

func (client *baseClient) BLMPopCount(
keys []string,
listDirection ListDirection,
count int64,
timeoutSecs float64,
) (map[Result[string]][]Result[string], error) {
) (map[string][]string, error) {
listDirectionStr, err := listDirection.toString()
if err != nil {
return nil, err
Expand All @@ -1131,7 +1131,7 @@ func (client *baseClient) BLMPopCount(
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
return handleStringToStringArrayMapOrNilResponse(result)
}

func (client *baseClient) LSet(key string, index int64, element string) (string, error) {
Expand Down Expand Up @@ -1754,31 +1754,31 @@ func (client *baseClient) ZIncrBy(key string, increment float64, member string)
return handleFloatResponse(result)
}

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

func (client *baseClient) ZPopMinWithCount(key string, count int64) (map[Result[string]]Result[float64], error) {
func (client *baseClient) ZPopMinWithCount(key string, count int64) (map[string]float64, error) {
result, err := client.executeCommand(C.ZPopMin, []string{key, utils.IntToString(count)})
if err != nil {
return nil, err
}
return handleStringDoubleMapResponse(result)
}

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

func (client *baseClient) ZPopMaxWithCount(key string, count int64) (map[Result[string]]Result[float64], error) {
func (client *baseClient) ZPopMaxWithCount(key string, count int64) (map[string]float64, error) {
result, err := client.executeCommand(C.ZPopMax, []string{key, utils.IntToString(count)})
if err != nil {
return nil, err
Expand Down Expand Up @@ -1892,7 +1892,7 @@ func (client *baseClient) ZRange(key string, rangeQuery options.ZRangeQuery) ([]
func (client *baseClient) ZRangeWithScores(
key string,
rangeQuery options.ZRangeQueryWithScores,
) (map[Result[string]]Result[float64], error) {
) (map[string]float64, error) {
args := make([]string, 0, 10)
args = append(args, key)
args = append(args, rangeQuery.ToArgs()...)
Expand Down
2 changes: 1 addition & 1 deletion go/api/glide_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (client *glideClient) ConfigSet(parameters map[string]string) (string, erro
return handleStringResponse(result)
}

func (client *glideClient) ConfigGet(args []string) (map[Result[string]]Result[string], error) {
func (client *glideClient) ConfigGet(args []string) (map[string]string, error) {
res, err := client.executeCommand(C.ConfigGet, args)
if err != nil {
return nil, err
Expand Down
8 changes: 2 additions & 6 deletions go/api/hash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,10 @@ type HashCommands interface {
//
// For example:
// fieldValueMap, err := client.HGetAll("my_hash")
// // field1 equals api.CreateStringResult("field1")
// // value1 equals api.CreateStringResult("value1")
// // field2 equals api.CreateStringResult("field2")
// // value2 equals api.CreateStringResult("value2")
// // fieldValueMap equals map[api.Result[string]]api.Result[string]{field1: value1, field2: value2}
// // fieldValueMap equals map[string]string{field1: value1, field2: value2}
//
// [valkey.io]: https://valkey.io/commands/hgetall/
HGetAll(key string) (map[Result[string]]Result[string], error)
HGetAll(key string) (map[string]string, error)

// HMGet returns the values associated with the specified fields in the hash stored at key.
//
Expand Down
16 changes: 8 additions & 8 deletions go/api/list_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,10 @@ type ListCommands interface {
// For example:
// result, err := client.LPush("my_list", []string{"one", "two", "three"})
// result, err := client.LMPop([]string{"my_list"}, api.Left)
// result[api.CreateStringResult("my_list")] = []api.Result[string]{api.CreateStringResult("three")}
// result["my_list"] = []string{"three"}
//
// [valkey.io]: https://valkey.io/commands/lmpop/
LMPop(keys []string, listDirection ListDirection) (map[Result[string]][]Result[string], error)
LMPop(keys []string, listDirection ListDirection) (map[string][]string, error)

// Pops one or more elements from the first non-empty list from the provided keys.
//
Expand All @@ -514,10 +514,10 @@ type ListCommands interface {
// For example:
// result, err := client.LPush("my_list", []string{"one", "two", "three"})
// result, err := client.LMPopCount([]string{"my_list"}, api.Left, int64(1))
// result[api.CreateStringResult("my_list")] = []api.Result[string]{api.CreateStringResult("three")}
// result["my_list"] = []string{"three"}
//
// [valkey.io]: https://valkey.io/commands/lmpop/
LMPopCount(keys []string, listDirection ListDirection, count int64) (map[Result[string]][]Result[string], error)
LMPopCount(keys []string, listDirection ListDirection, count int64) (map[string][]string, error)

// Blocks the connection until it pops one element from the first non-empty list from the provided keys. BLMPop is the
// blocking variant of [api.LMPop].
Expand All @@ -544,11 +544,11 @@ type ListCommands interface {
// For example:
// result, err := client.LPush("my_list", []string{"one", "two", "three"})
// result, err := client.BLMPop([]string{"my_list"}, api.Left, float64(0.1))
// result[api.CreateStringResult("my_list")] = []api.Result[string]{api.CreateStringResult("three")}
// result["my_list"] = []string{"three"}
//
// [valkey.io]: https://valkey.io/commands/blmpop/
// [Blocking Commands]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#blocking-commands
BLMPop(keys []string, listDirection ListDirection, timeoutSecs float64) (map[Result[string]][]Result[string], error)
BLMPop(keys []string, listDirection ListDirection, timeoutSecs float64) (map[string][]string, error)

// Blocks the connection until it pops one or more elements from the first non-empty list from the provided keys.
// BLMPopCount is the blocking variant of [api.LMPopCount].
Expand Down Expand Up @@ -576,7 +576,7 @@ type ListCommands interface {
// For example:
// result, err: client.LPush("my_list", []string{"one", "two", "three"})
// result, err := client.BLMPopCount([]string{"my_list"}, api.Left, int64(1), float64(0.1))
// result[api.CreateStringResult("my_list")] = []api.Result[string]{api.CreateStringResult("three")}
// result["my_list"] = []string{"three"}
//
// [valkey.io]: https://valkey.io/commands/blmpop/
// [Blocking Commands]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#blocking-commands
Expand All @@ -585,7 +585,7 @@ type ListCommands interface {
listDirection ListDirection,
count int64,
timeoutSecs float64,
) (map[Result[string]][]Result[string], error)
) (map[string][]string, error)

// Sets the list element at index to element.
// The index is zero-based, so 0 means the first element,1 the second element and so on. Negative indices can be used to
Expand Down
99 changes: 55 additions & 44 deletions go/api/response_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,57 +351,63 @@ func handleBoolArrayResponse(response *C.struct_CommandResponse) ([]bool, error)
return slice, nil
}

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

typeErr := checkResponseType(response, C.Map, false)
if typeErr != nil {
return nil, typeErr
}

m := make(map[Result[string]]Result[float64], response.array_value_len)
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
key, err := convertCharArrayToString(v.map_key, true)
if err != nil {
return nil, err
}
typeErr := checkResponseType(v.map_value, C.Float, false)
if typeErr != nil {
return nil, typeErr
}
value := CreateFloat64Result(float64(v.map_value.float_value))
m[key] = value
data, err := parseMap(response)
if err != nil {
return nil, err
}
aMap := data.(map[string]interface{})

converted, err := mapConverter[float64]{
nil, false,
}.convert(aMap)
if err != nil {
return nil, err
}
result, ok := converted.(map[string]float64)
if !ok {
return nil, &RequestError{fmt.Sprintf("unexpected type of map: %T", converted)}
}
return m, nil
return result, nil
}

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

typeErr := checkResponseType(response, C.Map, false)
if typeErr != nil {
return nil, typeErr
}

m := make(map[Result[string]]Result[string], response.array_value_len)
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
key, err := convertCharArrayToString(v.map_key, true)
if err != nil {
return nil, err
}
value, err := convertCharArrayToString(v.map_value, true)
if err != nil {
return nil, err
}
m[key] = value
data, err := parseMap(response)
if err != nil {
return nil, err
}
aMap := data.(map[string]interface{})

return m, nil
converted, err := mapConverter[string]{
nil, false,
}.convert(aMap)
if err != nil {
return nil, err
}
result, ok := converted.(map[string]string)
if !ok {
return nil, &RequestError{fmt.Sprintf("unexpected type of map: %T", converted)}
}
return result, nil
}

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

typeErr := checkResponseType(response, C.Map, true)
Expand All @@ -413,37 +419,42 @@ func handleStringToStringArrayMapOrNullResponse(
return nil, nil
}

m := make(map[Result[string]][]Result[string], response.array_value_len)
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
key, err := convertCharArrayToString(v.map_key, true)
if err != nil {
return nil, err
}
value, err := convertStringOrNilArray(v.map_value)
if err != nil {
return nil, err
}
m[key] = value
data, err := parseMap(response)
if err != nil {
return nil, err
}

converters := mapConverter[[]string]{
arrayConverter[string]{},
false,
}

return m, nil
res, err := converters.convert(data)
if err != nil {
return nil, err
}
if result, ok := res.(map[string][]string); ok {
return result, nil
}

return nil, &RequestError{fmt.Sprintf("unexpected type received: %T", res)}
}

func handleStringSetResponse(response *C.struct_CommandResponse) (map[Result[string]]struct{}, error) {
func handleStringSetResponse(response *C.struct_CommandResponse) (map[string]struct{}, error) {
defer C.free_command_response(response)

typeErr := checkResponseType(response, C.Sets, false)
if typeErr != nil {
return nil, typeErr
}

slice := make(map[Result[string]]struct{}, response.sets_value_len)
slice := make(map[string]struct{}, response.sets_value_len)
for _, v := range unsafe.Slice(response.sets_value, response.sets_value_len) {
res, err := convertCharArrayToString(&v, true)
if err != nil {
return nil, err
}
slice[res] = struct{}{}
slice[res.Value()] = struct{}{}
}

return slice, nil
Expand Down
Loading

0 comments on commit 502a8d7

Please sign in to comment.