Skip to content

Commit

Permalink
Go: Fix return types, part 6 (#2965)
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 084a68b commit b95fbbd
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 397 deletions.
80 changes: 39 additions & 41 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (client *baseClient) MGet(keys []string) ([]Result[string], error) {
return nil, err
}

return handleStringArrayResponse(result)
return handleStringOrNilArrayResponse(result)
}

func (client *baseClient) Incr(key string) (int64, error) {
Expand Down Expand Up @@ -391,7 +391,7 @@ func (client *baseClient) HMGet(key string, fields []string) ([]Result[string],
return nil, err
}

return handleStringArrayResponse(result)
return handleStringOrNilArrayResponse(result)
}

func (client *baseClient) HSet(key string, values map[string]string) (int64, error) {
Expand Down Expand Up @@ -430,7 +430,7 @@ func (client *baseClient) HLen(key string) (int64, error) {
return handleIntResponse(result)
}

func (client *baseClient) HVals(key string) ([]Result[string], error) {
func (client *baseClient) HVals(key string) ([]string, error) {
result, err := client.executeCommand(C.HVals, []string{key})
if err != nil {
return nil, err
Expand All @@ -448,7 +448,7 @@ func (client *baseClient) HExists(key string, field string) (bool, error) {
return handleBoolResponse(result)
}

func (client *baseClient) HKeys(key string) ([]Result[string], error) {
func (client *baseClient) HKeys(key string) ([]string, error) {
result, err := client.executeCommand(C.HKeys, []string{key})
if err != nil {
return nil, err
Expand Down Expand Up @@ -583,13 +583,13 @@ func (client *baseClient) LPop(key string) (Result[string], error) {
return handleStringOrNilResponse(result)
}

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

return handleStringArrayOrNullResponse(result)
return handleStringArrayOrNilResponse(result)
}

func (client *baseClient) LPos(key string, element string) (Result[int64], error) {
Expand All @@ -610,7 +610,7 @@ func (client *baseClient) LPosWithOptions(key string, element string, options *L
return handleIntOrNilResponse(result)
}

func (client *baseClient) LPosCount(key string, element string, count int64) ([]Result[int64], error) {
func (client *baseClient) LPosCount(key string, element string, count int64) ([]int64, error) {
result, err := client.executeCommand(C.LPos, []string{key, element, CountKeyword, utils.IntToString(count)})
if err != nil {
return nil, err
Expand All @@ -624,7 +624,7 @@ func (client *baseClient) LPosCountWithOptions(
element string,
count int64,
options *LPosOptions,
) ([]Result[int64], error) {
) ([]int64, error) {
result, err := client.executeCommand(
C.LPos,
append([]string{key, element, CountKeyword, utils.IntToString(count)}, options.toArgs()...),
Expand Down Expand Up @@ -904,7 +904,7 @@ func (client *baseClient) SMove(source string, destination string, member string
return handleBoolResponse(result)
}

func (client *baseClient) LRange(key string, start int64, end int64) ([]Result[string], error) {
func (client *baseClient) LRange(key string, start int64, end int64) ([]string, error) {
result, err := client.executeCommand(C.LRange, []string{key, utils.IntToString(start), utils.IntToString(end)})
if err != nil {
return nil, err
Expand Down Expand Up @@ -958,13 +958,13 @@ func (client *baseClient) RPop(key string) (Result[string], error) {
return handleStringOrNilResponse(result)
}

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

return handleStringArrayOrNullResponse(result)
return handleStringArrayOrNilResponse(result)
}

func (client *baseClient) LInsert(
Expand All @@ -989,22 +989,22 @@ func (client *baseClient) LInsert(
return handleIntResponse(result)
}

func (client *baseClient) BLPop(keys []string, timeoutSecs float64) ([]Result[string], error) {
func (client *baseClient) BLPop(keys []string, timeoutSecs float64) ([]string, error) {
result, err := client.executeCommand(C.BLPop, append(keys, utils.FloatToString(timeoutSecs)))
if err != nil {
return nil, err
}

return handleStringArrayOrNullResponse(result)
return handleStringArrayOrNilResponse(result)
}

func (client *baseClient) BRPop(keys []string, timeoutSecs float64) ([]Result[string], error) {
func (client *baseClient) BRPop(keys []string, timeoutSecs float64) ([]string, error) {
result, err := client.executeCommand(C.BRPop, append(keys, utils.FloatToString(timeoutSecs)))
if err != nil {
return nil, err
}

return handleStringArrayOrNullResponse(result)
return handleStringArrayOrNilResponse(result)
}

func (client *baseClient) RPushX(key string, elements []string) (int64, error) {
Expand Down Expand Up @@ -1394,12 +1394,12 @@ func (client *baseClient) Unlink(keys []string) (int64, error) {
return handleIntResponse(result)
}

func (client *baseClient) Type(key string) (Result[string], error) {
func (client *baseClient) Type(key string) (string, error) {
result, err := client.executeCommand(C.Type, []string{key})
if err != nil {
return CreateNilStringResult(), err
return defaultStringResponse, err
}
return handleStringOrNilResponse(result)
return handleStringResponse(result)
}

func (client *baseClient) Touch(keys []string) (int64, error) {
Expand All @@ -1411,12 +1411,12 @@ func (client *baseClient) Touch(keys []string) (int64, error) {
return handleIntResponse(result)
}

func (client *baseClient) Rename(key string, newKey string) (Result[string], error) {
func (client *baseClient) Rename(key string, newKey string) (string, error) {
result, err := client.executeCommand(C.Rename, []string{key, newKey})
if err != nil {
return CreateNilStringResult(), err
return defaultStringResponse, err
}
return handleStringOrNilResponse(result)
return handleStringResponse(result)
}

func (client *baseClient) Renamenx(key string, newKey string) (bool, error) {
Expand Down Expand Up @@ -1838,16 +1838,15 @@ func (client *baseClient) BZPopMin(keys []string, timeoutSecs float64) (Result[K
// result, err := client.ZRange("my_sorted_set", options.NewRangeByIndexQuery(0, -1))
//
// // Retrieve members within a score range in descending order
//
// query := options.NewRangeByScoreQuery(options.NewScoreBoundary(3, false),
// options.NewInfiniteScoreBoundary(options.NegativeInfinity)).
//
// .SetReverse()
// query := options.NewRangeByScoreQuery(
// options.NewScoreBoundary(3, false),
// options.NewInfiniteScoreBoundary(options.NegativeInfinity)).
// SetReverse()
// result, err := client.ZRange("my_sorted_set", query)
// // `result` contains members which have scores within the range of negative infinity to 3, in descending order
//
// [valkey.io]: https://valkey.io/commands/zrange/
func (client *baseClient) ZRange(key string, rangeQuery options.ZRangeQuery) ([]Result[string], error) {
func (client *baseClient) ZRange(key string, rangeQuery options.ZRangeQuery) ([]string, error) {
args := make([]string, 0, 10)
args = append(args, key)
args = append(args, rangeQuery.ToArgs()...)
Expand Down Expand Up @@ -1882,10 +1881,9 @@ func (client *baseClient) ZRange(key string, rangeQuery options.ZRangeQuery) ([]
// result, err := client.ZRangeWithScores("my_sorted_set", options.NewRangeByIndexQuery(0, -1))
//
// // Retrieve members within a score range in descending order
//
// query := options.NewRangeByScoreQuery(options.NewScoreBoundary(3, false),
// options.NewInfiniteScoreBoundary(options.NegativeInfinity)).
//
// query := options.NewRangeByScoreQuery(
// options.NewScoreBoundary(3, false),
// options.NewInfiniteScoreBoundary(options.NegativeInfinity)).
// SetReverse()
// result, err := client.ZRangeWithScores("my_sorted_set", query)
// // `result` contains members with scores within the range of negative infinity to 3, in descending order
Expand Down Expand Up @@ -2821,7 +2819,7 @@ func (client *baseClient) Sort(key string) ([]Result[string], error) {
if err != nil {
return nil, err
}
return handleStringArrayResponse(result)
return handleStringOrNilArrayResponse(result)
}

func (client *baseClient) SortWithOptions(key string, options *options.SortOptions) ([]Result[string], error) {
Expand All @@ -2830,15 +2828,15 @@ func (client *baseClient) SortWithOptions(key string, options *options.SortOptio
if err != nil {
return nil, err
}
return handleStringArrayResponse(result)
return handleStringOrNilArrayResponse(result)
}

func (client *baseClient) SortReadOnly(key string) ([]Result[string], error) {
result, err := client.executeCommand(C.SortReadOnly, []string{key})
if err != nil {
return nil, err
}
return handleStringArrayResponse(result)
return handleStringOrNilArrayResponse(result)
}

func (client *baseClient) SortReadOnlyWithOptions(key string, options *options.SortOptions) ([]Result[string], error) {
Expand All @@ -2847,28 +2845,28 @@ func (client *baseClient) SortReadOnlyWithOptions(key string, options *options.S
if err != nil {
return nil, err
}
return handleStringArrayResponse(result)
return handleStringOrNilArrayResponse(result)
}

func (client *baseClient) SortStore(key string, destination string) (Result[int64], error) {
func (client *baseClient) SortStore(key string, destination string) (int64, error) {
result, err := client.executeCommand(C.Sort, []string{key, "STORE", destination})
if err != nil {
return CreateNilInt64Result(), err
return defaultIntResponse, err
}
return handleIntOrNilResponse(result)
return handleIntResponse(result)
}

func (client *baseClient) SortStoreWithOptions(
key string,
destination string,
options *options.SortOptions,
) (Result[int64], error) {
) (int64, error) {
optionArgs := options.ToArgs()
result, err := client.executeCommand(C.Sort, append([]string{key, "STORE", destination}, optionArgs...))
if err != nil {
return CreateNilInt64Result(), err
return defaultIntResponse, err
}
return handleIntOrNilResponse(result)
return handleIntResponse(result)
}

// XGroupCreateConsumer creates a consumer named `consumer` in the consumer group `group` for the
Expand Down
20 changes: 9 additions & 11 deletions go/api/generic_base_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,17 +369,17 @@ type GenericBaseCommands interface {
// key - string
//
// Return value:
// If the key exists, the type of the stored value is returned. Otherwise, a none" string is returned.
// If the key exists, the type of the stored value is returned. Otherwise, a "none" string is returned.
//
// Example:
// result, err := client.Type([]string{"key"})
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: string
// fmt.Println(result) // Output: string
//
// [valkey.io]: Https://valkey.io/commands/type/
Type(key string) (Result[string], error)
Type(key string) (string, error)

// Renames key to new key.
// If new Key already exists it is overwritten.
Expand All @@ -399,10 +399,10 @@ type GenericBaseCommands interface {
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: OK
// fmt.Println(result) // Output: OK
//
// [valkey.io]: https://valkey.io/commands/rename/
Rename(key string, newKey string) (Result[string], error)
Rename(key string, newKey string) (string, error)

// Renames key to newkey if newKey does not yet exist.
//
Expand Down Expand Up @@ -613,11 +613,10 @@ type GenericBaseCommands interface {
// Example:
//
// result, err := client.SortStore("key","destkey")
// result.Value(): 1
// result.IsNil(): false
// result: 1
//
// [valkey.io]: https://valkey.io/commands/sort/
SortStore(key string, destination string) (Result[int64], error)
SortStore(key string, destination string) (int64, error)

// Sorts the elements in the list, set, or sorted set at key and stores the result in
// destination. The sort command can be used to sort elements based on
Expand Down Expand Up @@ -648,11 +647,10 @@ type GenericBaseCommands interface {
//
// options := api.NewSortOptions().SetByPattern("weight_*").SetIsAlpha(false).AddGetPattern("object_*").AddGetPattern("#")
// result, err := client.SortStore("key","destkey",options)
// result.Value(): 1
// result.IsNil(): false
// result: 1
//
// [valkey.io]: https://valkey.io/commands/sort/
SortStoreWithOptions(key string, destination string, sortOptions *options.SortOptions) (Result[int64], error)
SortStoreWithOptions(key string, destination string, sortOptions *options.SortOptions) (int64, error)

// Sorts the elements in the list, set, or sorted set at key and returns the result.
// The sortReadOnly command can be used to sort elements based on different criteria and apply
Expand Down
17 changes: 6 additions & 11 deletions go/api/hash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,14 @@ type HashCommands interface {
// key - The key of the hash.
//
// Return value:
// A slice of Result[string]s containing all the values in the hash, or an empty slice when key does not exist.
// A slice containing all the values in the hash, or an empty slice when key does not exist.
//
// For example:
// values, err := client.HVals("myHash")
// // value1 equals api.CreateStringResult("value1")
// // value2 equals api.CreateStringResult("value2")
// // value3 equals api.CreateStringResult("value3")
// // values equals []api.Result[string]{value1, value2, value3}
// values: []string{"value1", "value2", "value3"}
//
// [valkey.io]: https://valkey.io/commands/hvals/
HVals(key string) ([]Result[string], error)
HVals(key string) ([]string, error)

// HExists returns if field is an existing field in the hash stored at key.
//
Expand Down Expand Up @@ -215,16 +212,14 @@ type HashCommands interface {
// key - The key of the hash.
//
// Return value:
// A slice of Result[string]s containing all the field names in the hash, or an empty slice when key does not exist.
// A slice containing all the field names in the hash, or an empty slice when key does not exist.
//
// For example:
// names, err := client.HKeys("my_hash")
// // field1 equals api.CreateStringResult("field_1")
// // field2 equals api.CreateStringResult("field_2")
// // names equals []api.Result[string]{field1, field2}
// names: []string{"field1", "field2"}
//
// [valkey.io]: https://valkey.io/commands/hkeys/
HKeys(key string) ([]Result[string], error)
HKeys(key string) ([]string, error)

// HStrLen returns the string length of the value associated with field in the hash stored at key.
// If the key or the field do not exist, 0 is returned.
Expand Down
Loading

0 comments on commit b95fbbd

Please sign in to comment.