Skip to content

Commit 201d1f5

Browse files
Fix return types
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
1 parent dbb83b6 commit 201d1f5

10 files changed

+203
-276
lines changed

go/api/base_client.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func (client *baseClient) HGet(key string, field string) (Result[string], error)
376376
return handleStringOrNilResponse(result)
377377
}
378378

379-
func (client *baseClient) HGetAll(key string) (map[Result[string]]Result[string], error) {
379+
func (client *baseClient) HGetAll(key string) (map[string]string, error) {
380380
result, err := client.executeCommand(C.HGetAll, []string{key})
381381
if err != nil {
382382
return nil, err
@@ -616,7 +616,7 @@ func (client *baseClient) SUnionStore(destination string, keys []string) (int64,
616616
return handleIntResponse(result)
617617
}
618618

619-
func (client *baseClient) SMembers(key string) (map[Result[string]]struct{}, error) {
619+
func (client *baseClient) SMembers(key string) (map[string]struct{}, error) {
620620
result, err := client.executeCommand(C.SMembers, []string{key})
621621
if err != nil {
622622
return nil, err
@@ -643,7 +643,7 @@ func (client *baseClient) SIsMember(key string, member string) (bool, error) {
643643
return handleBoolResponse(result)
644644
}
645645

646-
func (client *baseClient) SDiff(keys []string) (map[Result[string]]struct{}, error) {
646+
func (client *baseClient) SDiff(keys []string) (map[string]struct{}, error) {
647647
result, err := client.executeCommand(C.SDiff, keys)
648648
if err != nil {
649649
return nil, err
@@ -661,7 +661,7 @@ func (client *baseClient) SDiffStore(destination string, keys []string) (int64,
661661
return handleIntResponse(result)
662662
}
663663

664-
func (client *baseClient) SInter(keys []string) (map[Result[string]]struct{}, error) {
664+
func (client *baseClient) SInter(keys []string) (map[string]struct{}, error) {
665665
result, err := client.executeCommand(C.SInter, keys)
666666
if err != nil {
667667
return nil, err
@@ -726,7 +726,7 @@ func (client *baseClient) SMIsMember(key string, members []string) ([]bool, erro
726726
return handleBoolArrayResponse(result)
727727
}
728728

729-
func (client *baseClient) SUnion(keys []string) (map[Result[string]]struct{}, error) {
729+
func (client *baseClient) SUnion(keys []string) (map[string]struct{}, error) {
730730
result, err := client.executeCommand(C.SUnion, keys)
731731
if err != nil {
732732
return nil, err
@@ -889,7 +889,7 @@ func (client *baseClient) LPushX(key string, elements []string) (int64, error) {
889889
return handleIntResponse(result)
890890
}
891891

892-
func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map[Result[string]][]Result[string], error) {
892+
func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map[string][]string, error) {
893893
listDirectionStr, err := listDirection.toString()
894894
if err != nil {
895895
return nil, err
@@ -910,14 +910,14 @@ func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map
910910
return nil, err
911911
}
912912

913-
return handleStringToStringArrayMapOrNullResponse(result)
913+
return handleStringToStringArrayMapOrNilResponse(result)
914914
}
915915

916916
func (client *baseClient) LMPopCount(
917917
keys []string,
918918
listDirection ListDirection,
919919
count int64,
920-
) (map[Result[string]][]Result[string], error) {
920+
) (map[string][]string, error) {
921921
listDirectionStr, err := listDirection.toString()
922922
if err != nil {
923923
return nil, err
@@ -938,14 +938,14 @@ func (client *baseClient) LMPopCount(
938938
return nil, err
939939
}
940940

941-
return handleStringToStringArrayMapOrNullResponse(result)
941+
return handleStringToStringArrayMapOrNilResponse(result)
942942
}
943943

944944
func (client *baseClient) BLMPop(
945945
keys []string,
946946
listDirection ListDirection,
947947
timeoutSecs float64,
948-
) (map[Result[string]][]Result[string], error) {
948+
) (map[string][]string, error) {
949949
listDirectionStr, err := listDirection.toString()
950950
if err != nil {
951951
return nil, err
@@ -966,15 +966,15 @@ func (client *baseClient) BLMPop(
966966
return nil, err
967967
}
968968

969-
return handleStringToStringArrayMapOrNullResponse(result)
969+
return handleStringToStringArrayMapOrNilResponse(result)
970970
}
971971

972972
func (client *baseClient) BLMPopCount(
973973
keys []string,
974974
listDirection ListDirection,
975975
count int64,
976976
timeoutSecs float64,
977-
) (map[Result[string]][]Result[string], error) {
977+
) (map[string][]string, error) {
978978
listDirectionStr, err := listDirection.toString()
979979
if err != nil {
980980
return nil, err
@@ -995,7 +995,7 @@ func (client *baseClient) BLMPopCount(
995995
return nil, err
996996
}
997997

998-
return handleStringToStringArrayMapOrNullResponse(result)
998+
return handleStringToStringArrayMapOrNilResponse(result)
999999
}
10001000

10011001
func (client *baseClient) LSet(key string, index int64, element string) (string, error) {
@@ -1495,31 +1495,31 @@ func (client *baseClient) ZIncrBy(key string, increment float64, member string)
14951495
return handleFloatResponse(result)
14961496
}
14971497

1498-
func (client *baseClient) ZPopMin(key string) (map[Result[string]]Result[float64], error) {
1498+
func (client *baseClient) ZPopMin(key string) (map[string]float64, error) {
14991499
result, err := client.executeCommand(C.ZPopMin, []string{key})
15001500
if err != nil {
15011501
return nil, err
15021502
}
15031503
return handleStringDoubleMapResponse(result)
15041504
}
15051505

1506-
func (client *baseClient) ZPopMinWithCount(key string, count int64) (map[Result[string]]Result[float64], error) {
1506+
func (client *baseClient) ZPopMinWithCount(key string, count int64) (map[string]float64, error) {
15071507
result, err := client.executeCommand(C.ZPopMin, []string{key, utils.IntToString(count)})
15081508
if err != nil {
15091509
return nil, err
15101510
}
15111511
return handleStringDoubleMapResponse(result)
15121512
}
15131513

1514-
func (client *baseClient) ZPopMax(key string) (map[Result[string]]Result[float64], error) {
1514+
func (client *baseClient) ZPopMax(key string) (map[string]float64, error) {
15151515
result, err := client.executeCommand(C.ZPopMax, []string{key})
15161516
if err != nil {
15171517
return nil, err
15181518
}
15191519
return handleStringDoubleMapResponse(result)
15201520
}
15211521

1522-
func (client *baseClient) ZPopMaxWithCount(key string, count int64) (map[Result[string]]Result[float64], error) {
1522+
func (client *baseClient) ZPopMaxWithCount(key string, count int64) (map[string]float64, error) {
15231523
result, err := client.executeCommand(C.ZPopMax, []string{key, utils.IntToString(count)})
15241524
if err != nil {
15251525
return nil, err
@@ -1635,7 +1635,7 @@ func (client *baseClient) ZRange(key string, rangeQuery options.ZRangeQuery) ([]
16351635
func (client *baseClient) ZRangeWithScores(
16361636
key string,
16371637
rangeQuery options.ZRangeQueryWithScores,
1638-
) (map[Result[string]]Result[float64], error) {
1638+
) (map[string]float64, error) {
16391639
args := make([]string, 0, 10)
16401640
args = append(args, key)
16411641
args = append(args, rangeQuery.ToArgs()...)

go/api/glide_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (client *glideClient) ConfigSet(parameters map[string]string) (string, erro
5151
return handleStringResponse(result)
5252
}
5353

54-
func (client *glideClient) ConfigGet(args []string) (map[Result[string]]Result[string], error) {
54+
func (client *glideClient) ConfigGet(args []string) (map[string]string, error) {
5555
res, err := client.executeCommand(C.ConfigGet, args)
5656
if err != nil {
5757
return nil, err

go/api/hash_commands.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,10 @@ type HashCommands interface {
4646
//
4747
// For example:
4848
// fieldValueMap, err := client.HGetAll("my_hash")
49-
// // field1 equals api.CreateStringResult("field1")
50-
// // value1 equals api.CreateStringResult("value1")
51-
// // field2 equals api.CreateStringResult("field2")
52-
// // value2 equals api.CreateStringResult("value2")
53-
// // fieldValueMap equals map[api.Result[string]]api.Result[string]{field1: value1, field2: value2}
49+
// // fieldValueMap equals map[string]string{field1: value1, field2: value2}
5450
//
5551
// [valkey.io]: https://valkey.io/commands/hgetall/
56-
HGetAll(key string) (map[Result[string]]Result[string], error)
52+
HGetAll(key string) (map[string]string, error)
5753

5854
// HMGet returns the values associated with the specified fields in the hash stored at key.
5955
//

go/api/list_commands.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ type ListCommands interface {
493493
// For example:
494494
// result, err := client.LPush("my_list", []string{"one", "two", "three"})
495495
// result, err := client.LMPop([]string{"my_list"}, api.Left)
496-
// result[api.CreateStringResult("my_list")] = []api.Result[string]{api.CreateStringResult("three")}
496+
// result["my_list"] = []string{"three"}
497497
//
498498
// [valkey.io]: https://valkey.io/commands/lmpop/
499-
LMPop(keys []string, listDirection ListDirection) (map[Result[string]][]Result[string], error)
499+
LMPop(keys []string, listDirection ListDirection) (map[string][]string, error)
500500

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

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

555555
// Blocks the connection until it pops one or more elements from the first non-empty list from the provided keys.
556556
// BLMPopCount is the blocking variant of [api.LMPopCount].
@@ -578,7 +578,7 @@ type ListCommands interface {
578578
// For example:
579579
// result, err: client.LPush("my_list", []string{"one", "two", "three"})
580580
// result, err := client.BLMPopCount([]string{"my_list"}, api.Left, int64(1), float64(0.1))
581-
// result[api.CreateStringResult("my_list")] = []api.Result[string]{api.CreateStringResult("three")}
581+
// result["my_list"] = []string{"three"}
582582
//
583583
// [valkey.io]: https://valkey.io/commands/blmpop/
584584
// [Blocking Commands]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#blocking-commands
@@ -587,7 +587,7 @@ type ListCommands interface {
587587
listDirection ListDirection,
588588
count int64,
589589
timeoutSecs float64,
590-
) (map[Result[string]][]Result[string], error)
590+
) (map[string][]string, error)
591591

592592
// Sets the list element at index to element.
593593
// The index is zero-based, so 0 means the first element,1 the second element and so on. Negative indices can be used to

go/api/response_handlers.go

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -338,57 +338,63 @@ func handleBoolArrayResponse(response *C.struct_CommandResponse) ([]bool, error)
338338
return slice, nil
339339
}
340340

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

344344
typeErr := checkResponseType(response, C.Map, false)
345345
if typeErr != nil {
346346
return nil, typeErr
347347
}
348348

349-
m := make(map[Result[string]]Result[float64], response.array_value_len)
350-
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
351-
key, err := convertCharArrayToString(v.map_key, true)
352-
if err != nil {
353-
return nil, err
354-
}
355-
typeErr := checkResponseType(v.map_value, C.Float, false)
356-
if typeErr != nil {
357-
return nil, typeErr
358-
}
359-
value := CreateFloat64Result(float64(v.map_value.float_value))
360-
m[key] = value
349+
data, err := parseMap(response)
350+
if err != nil {
351+
return nil, err
352+
}
353+
aMap := data.(map[string]interface{})
354+
355+
converted, err := mapConverter[float64]{
356+
nil,
357+
}.convert(aMap)
358+
if err != nil {
359+
return nil, err
360+
}
361+
result, ok := converted.(map[string]float64)
362+
if !ok {
363+
return nil, &RequestError{fmt.Sprintf("unexpected type of map: %T", converted)}
361364
}
362-
return m, nil
365+
return result, nil
363366
}
364367

365-
func handleStringToStringMapResponse(response *C.struct_CommandResponse) (map[Result[string]]Result[string], error) {
368+
func handleStringToStringMapResponse(response *C.struct_CommandResponse) (map[string]string, error) {
366369
defer C.free_command_response(response)
367370

368371
typeErr := checkResponseType(response, C.Map, false)
369372
if typeErr != nil {
370373
return nil, typeErr
371374
}
372375

373-
m := make(map[Result[string]]Result[string], response.array_value_len)
374-
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
375-
key, err := convertCharArrayToString(v.map_key, true)
376-
if err != nil {
377-
return nil, err
378-
}
379-
value, err := convertCharArrayToString(v.map_value, true)
380-
if err != nil {
381-
return nil, err
382-
}
383-
m[key] = value
376+
data, err := parseMap(response)
377+
if err != nil {
378+
return nil, err
384379
}
380+
aMap := data.(map[string]interface{})
385381

386-
return m, nil
382+
converted, err := mapConverter[string]{
383+
nil,
384+
}.convert(aMap)
385+
if err != nil {
386+
return nil, err
387+
}
388+
result, ok := converted.(map[string]string)
389+
if !ok {
390+
return nil, &RequestError{fmt.Sprintf("unexpected type of map: %T", converted)}
391+
}
392+
return result, nil
387393
}
388394

389-
func handleStringToStringArrayMapOrNullResponse(
395+
func handleStringToStringArrayMapOrNilResponse(
390396
response *C.struct_CommandResponse,
391-
) (map[Result[string]][]Result[string], error) {
397+
) (map[string][]string, error) {
392398
defer C.free_command_response(response)
393399

394400
typeErr := checkResponseType(response, C.Map, true)
@@ -400,37 +406,41 @@ func handleStringToStringArrayMapOrNullResponse(
400406
return nil, nil
401407
}
402408

403-
m := make(map[Result[string]][]Result[string], response.array_value_len)
404-
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
405-
key, err := convertCharArrayToString(v.map_key, true)
406-
if err != nil {
407-
return nil, err
408-
}
409-
value, err := convertStringArray(v.map_value)
410-
if err != nil {
411-
return nil, err
412-
}
413-
m[key] = value
409+
data, err := parseMap(response)
410+
if err != nil {
411+
return nil, err
412+
}
413+
414+
converters := mapConverter[[]string]{
415+
arrayConverter[string]{},
416+
}
417+
418+
res, err := converters.convert(data)
419+
if err != nil {
420+
return nil, err
421+
}
422+
if result, ok := res.(map[string][]string); ok {
423+
return result, nil
414424
}
415425

416-
return m, nil
426+
return nil, &RequestError{fmt.Sprintf("unexpected type received: %T", res)}
417427
}
418428

419-
func handleStringSetResponse(response *C.struct_CommandResponse) (map[Result[string]]struct{}, error) {
429+
func handleStringSetResponse(response *C.struct_CommandResponse) (map[string]struct{}, error) {
420430
defer C.free_command_response(response)
421431

422432
typeErr := checkResponseType(response, C.Sets, false)
423433
if typeErr != nil {
424434
return nil, typeErr
425435
}
426436

427-
slice := make(map[Result[string]]struct{}, response.sets_value_len)
437+
slice := make(map[string]struct{}, response.sets_value_len)
428438
for _, v := range unsafe.Slice(response.sets_value, response.sets_value_len) {
429439
res, err := convertCharArrayToString(&v, true)
430440
if err != nil {
431441
return nil, err
432442
}
433-
slice[res] = struct{}{}
443+
slice[res.Value()] = struct{}{}
434444
}
435445

436446
return slice, nil

0 commit comments

Comments
 (0)