From 358a10f2a17c1025a58b2010c3814c3393f6a217 Mon Sep 17 00:00:00 2001 From: Yury-Fridlyand Date: Tue, 14 Jan 2025 14:08:42 -0800 Subject: [PATCH] Use default value. Signed-off-by: Yury-Fridlyand --- go/api/base_client.go | 92 ++++++++++++++++++++-------------------- go/api/response_types.go | 3 ++ 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/go/api/base_client.go b/go/api/base_client.go index 60b98c1b9d..d059b7bfe4 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -264,7 +264,7 @@ func (client *baseClient) MGet(keys []string) ([]Result[string], error) { func (client *baseClient) Incr(key string) (int64, error) { result, err := client.executeCommand(C.Incr, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -273,7 +273,7 @@ func (client *baseClient) Incr(key string) (int64, error) { func (client *baseClient) IncrBy(key string, amount int64) (int64, error) { result, err := client.executeCommand(C.IncrBy, []string{key, utils.IntToString(amount)}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -294,7 +294,7 @@ func (client *baseClient) IncrByFloat(key string, amount float64) (Result[float6 func (client *baseClient) Decr(key string) (int64, error) { result, err := client.executeCommand(C.Decr, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -303,7 +303,7 @@ func (client *baseClient) Decr(key string) (int64, error) { func (client *baseClient) DecrBy(key string, amount int64) (int64, error) { result, err := client.executeCommand(C.DecrBy, []string{key, utils.IntToString(amount)}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -312,7 +312,7 @@ func (client *baseClient) DecrBy(key string, amount int64) (int64, error) { func (client *baseClient) Strlen(key string) (int64, error) { result, err := client.executeCommand(C.Strlen, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -321,7 +321,7 @@ func (client *baseClient) Strlen(key string) (int64, error) { func (client *baseClient) SetRange(key string, offset int, value string) (int64, error) { result, err := client.executeCommand(C.SetRange, []string{key, strconv.Itoa(offset), value}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -339,7 +339,7 @@ func (client *baseClient) GetRange(key string, start int, end int) (Result[strin func (client *baseClient) Append(key string, value string) (int64, error) { result, err := client.executeCommand(C.Append, []string{key, value}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -397,7 +397,7 @@ func (client *baseClient) HMGet(key string, fields []string) ([]Result[string], func (client *baseClient) HSet(key string, values map[string]string) (int64, error) { result, err := client.executeCommand(C.HSet, utils.ConvertMapToKeyValueStringArray(key, values)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -415,7 +415,7 @@ func (client *baseClient) HSetNX(key string, field string, value string) (Result func (client *baseClient) HDel(key string, fields []string) (int64, error) { result, err := client.executeCommand(C.HDel, append([]string{key}, fields...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -424,7 +424,7 @@ func (client *baseClient) HDel(key string, fields []string) (int64, error) { func (client *baseClient) HLen(key string) (int64, error) { result, err := client.executeCommand(C.HLen, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -460,7 +460,7 @@ func (client *baseClient) HKeys(key string) ([]Result[string], error) { func (client *baseClient) HStrLen(key string, field string) (int64, error) { result, err := client.executeCommand(C.HStrlen, []string{key, field}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -469,7 +469,7 @@ func (client *baseClient) HStrLen(key string, field string) (int64, error) { func (client *baseClient) HIncrBy(key string, field string, increment int64) (int64, error) { result, err := client.executeCommand(C.HIncrBy, []string{key, field, utils.IntToString(increment)}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -512,7 +512,7 @@ func (client *baseClient) HScanWithOptions( func (client *baseClient) LPush(key string, elements []string) (int64, error) { result, err := client.executeCommand(C.LPush, append([]string{key}, elements...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -583,7 +583,7 @@ func (client *baseClient) LPosCountWithOptions( func (client *baseClient) RPush(key string, elements []string) (int64, error) { result, err := client.executeCommand(C.RPush, append([]string{key}, elements...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -592,7 +592,7 @@ func (client *baseClient) RPush(key string, elements []string) (int64, error) { func (client *baseClient) SAdd(key string, members []string) (int64, error) { result, err := client.executeCommand(C.SAdd, append([]string{key}, members...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -601,7 +601,7 @@ func (client *baseClient) SAdd(key string, members []string) (int64, error) { func (client *baseClient) SRem(key string, members []string) (int64, error) { result, err := client.executeCommand(C.SRem, append([]string{key}, members...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -610,7 +610,7 @@ func (client *baseClient) SRem(key string, members []string) (int64, error) { func (client *baseClient) SUnionStore(destination string, keys []string) (int64, error) { result, err := client.executeCommand(C.SUnionStore, append([]string{destination}, keys...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -628,7 +628,7 @@ func (client *baseClient) SMembers(key string) (map[Result[string]]struct{}, err func (client *baseClient) SCard(key string) (int64, error) { result, err := client.executeCommand(C.SCard, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -655,7 +655,7 @@ func (client *baseClient) SDiff(keys []string) (map[Result[string]]struct{}, err func (client *baseClient) SDiffStore(destination string, keys []string) (int64, error) { result, err := client.executeCommand(C.SDiffStore, append([]string{destination}, keys...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -673,7 +673,7 @@ func (client *baseClient) SInter(keys []string) (map[Result[string]]struct{}, er func (client *baseClient) SInterStore(destination string, keys []string) (int64, error) { result, err := client.executeCommand(C.SInterStore, append([]string{destination}, keys...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -682,7 +682,7 @@ func (client *baseClient) SInterStore(destination string, keys []string) (int64, func (client *baseClient) SInterCard(keys []string) (int64, error) { result, err := client.executeCommand(C.SInterCard, append([]string{strconv.Itoa(len(keys))}, keys...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -693,7 +693,7 @@ func (client *baseClient) SInterCardLimit(keys []string, limit int64) (int64, er result, err := client.executeCommand(C.SInterCard, args) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -798,7 +798,7 @@ func (client *baseClient) LTrim(key string, start int64, end int64) (Result[stri func (client *baseClient) LLen(key string) (int64, error) { result, err := client.executeCommand(C.LLen, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -807,7 +807,7 @@ func (client *baseClient) LLen(key string) (int64, error) { func (client *baseClient) LRem(key string, count int64, element string) (int64, error) { result, err := client.executeCommand(C.LRem, []string{key, utils.IntToString(count), element}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -839,7 +839,7 @@ func (client *baseClient) LInsert( ) (int64, error) { insertPositionStr, err := insertPosition.toString() if err != nil { - return 0, err + return defaultIntResponse, err } result, err := client.executeCommand( @@ -847,7 +847,7 @@ func (client *baseClient) LInsert( []string{key, insertPositionStr, pivot, element}, ) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -874,7 +874,7 @@ func (client *baseClient) BRPop(keys []string, timeoutSecs float64) ([]Result[st func (client *baseClient) RPushX(key string, elements []string) (int64, error) { result, err := client.executeCommand(C.RPushX, append([]string{key}, elements...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -883,7 +883,7 @@ func (client *baseClient) RPushX(key string, elements []string) (int64, error) { func (client *baseClient) LPushX(key string, elements []string) (int64, error) { result, err := client.executeCommand(C.LPushX, append([]string{key}, elements...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1088,7 +1088,7 @@ func (client *baseClient) PingWithMessage(message string) (string, error) { func (client *baseClient) Del(keys []string) (int64, error) { result, err := client.executeCommand(C.Del, keys) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1097,7 +1097,7 @@ func (client *baseClient) Del(keys []string) (int64, error) { func (client *baseClient) Exists(keys []string) (int64, error) { result, err := client.executeCommand(C.Exists, keys) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1206,7 +1206,7 @@ func (client *baseClient) PExpireAtWithOptions( func (client *baseClient) ExpireTime(key string) (int64, error) { result, err := client.executeCommand(C.ExpireTime, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1215,7 +1215,7 @@ func (client *baseClient) ExpireTime(key string) (int64, error) { func (client *baseClient) PExpireTime(key string) (int64, error) { result, err := client.executeCommand(C.PExpireTime, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1224,7 +1224,7 @@ func (client *baseClient) PExpireTime(key string) (int64, error) { func (client *baseClient) TTL(key string) (int64, error) { result, err := client.executeCommand(C.TTL, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1233,7 +1233,7 @@ func (client *baseClient) TTL(key string) (int64, error) { func (client *baseClient) PTTL(key string) (int64, error) { result, err := client.executeCommand(C.PTTL, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1242,7 +1242,7 @@ func (client *baseClient) PTTL(key string) (int64, error) { func (client *baseClient) PfAdd(key string, elements []string) (int64, error) { result, err := client.executeCommand(C.PfAdd, append([]string{key}, elements...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1251,7 +1251,7 @@ func (client *baseClient) PfAdd(key string, elements []string) (int64, error) { func (client *baseClient) PfCount(keys []string) (int64, error) { result, err := client.executeCommand(C.PfCount, keys) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1260,7 +1260,7 @@ func (client *baseClient) PfCount(keys []string) (int64, error) { func (client *baseClient) Unlink(keys []string) (int64, error) { result, err := client.executeCommand(C.Unlink, keys) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1277,7 +1277,7 @@ func (client *baseClient) Type(key string) (Result[string], error) { func (client *baseClient) Touch(keys []string) (int64, error) { result, err := client.executeCommand(C.Touch, keys) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1341,7 +1341,7 @@ func (client *baseClient) ZAdd( append([]string{key}, utils.ConvertMapToValueKeyStringArray(membersScoreMap)...), ) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1354,7 +1354,7 @@ func (client *baseClient) ZAddWithOptions( ) (int64, error) { optionArgs, err := opts.ToArgs() if err != nil { - return 0, err + return defaultIntResponse, err } commandArgs := append([]string{key}, optionArgs...) result, err := client.executeCommand( @@ -1362,7 +1362,7 @@ func (client *baseClient) ZAddWithOptions( append(commandArgs, utils.ConvertMapToValueKeyStringArray(membersScoreMap)...), ) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1453,7 +1453,7 @@ func (client *baseClient) ZPopMaxWithCount(key string, count int64) (map[Result[ func (client *baseClient) ZRem(key string, members []string) (int64, error) { result, err := client.executeCommand(C.ZRem, append([]string{key}, members...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) } @@ -1461,7 +1461,7 @@ func (client *baseClient) ZRem(key string, members []string) (int64, error) { func (client *baseClient) ZCard(key string) (int64, error) { result, err := client.executeCommand(C.ZCard, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) @@ -1614,11 +1614,11 @@ func (client *baseClient) ZRevRankWithScore(key string, member string) (Result[i func (client *baseClient) XTrim(key string, options *options.XTrimOptions) (int64, error) { xTrimArgs, err := options.ToArgs() if err != nil { - return 0, err + return defaultIntResponse, err } result, err := client.executeCommand(C.XTrim, append([]string{key}, xTrimArgs...)) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) } @@ -1626,7 +1626,7 @@ func (client *baseClient) XTrim(key string, options *options.XTrimOptions) (int6 func (client *baseClient) XLen(key string) (int64, error) { result, err := client.executeCommand(C.XLen, []string{key}) if err != nil { - return 0, err + return defaultIntResponse, err } return handleLongResponse(result) } diff --git a/go/api/response_types.go b/go/api/response_types.go index 6172c4ff2b..279e64ba4e 100644 --- a/go/api/response_types.go +++ b/go/api/response_types.go @@ -2,6 +2,9 @@ package api +// A value to return alongside with error in case if command failed +var defaultIntResponse int64 + type Result[T any] struct { val T isNil bool