Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go:Implementing List commands #2434

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,186 @@ func (client *baseClient) LInsert(
return handleLongResponse(result)
}

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

return handleStringArrayOrNullResponse(result)
}

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

return handleStringArrayOrNullResponse(result)
}

func (client *baseClient) RPushX(key string, elements []string) (Result[int64], error) {
result, err := client.executeCommand(C.RPushX, append([]string{key}, elements...))
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}

func (client *baseClient) LPushX(key string, elements []string) (Result[int64], error) {
result, err := client.executeCommand(C.LPushX, append([]string{key}, elements...))
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}

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

args := make([]string, 0, len(keys)+2)
janhavigupta007 marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, strconv.Itoa(len(keys)))
args = append(args, keys...)
args = append(args, listDirectionStr)
result, err := client.executeCommand(C.LMPop, args)
if err != nil {
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
}

func (client *baseClient) LMPopCount(
keys []string,
listDirection ListDirection,
count int64,
) (map[Result[string]][]Result[string], error) {
listDirectionStr, err := listDirection.toString()
if err != nil {
return nil, err
janhavigupta007 marked this conversation as resolved.
Show resolved Hide resolved
}

args := make([]string, 0, len(keys)+4)
args = append(args, strconv.Itoa(len(keys)))
janhavigupta007 marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, keys...)
args = append(args, listDirectionStr, CountKeyword, utils.IntToString(count))
result, err := client.executeCommand(C.LMPop, args)
if err != nil {
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
}

func (client *baseClient) BLMPop(
keys []string,
listDirection ListDirection,
timeout float64,
) (map[Result[string]][]Result[string], error) {
listDirectionStr, err := listDirection.toString()
if err != nil {
return nil, err
}

args := make([]string, 0, len(keys)+3)
janhavigupta007 marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, utils.FloatToString(timeout), strconv.Itoa(len(keys)))
args = append(args, keys...)
args = append(args, listDirectionStr)
result, err := client.executeCommand(C.BLMPop, args)
if err != nil {
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
}

func (client *baseClient) BLMPopCount(
keys []string,
listDirection ListDirection,
count int64,
timeout float64,
) (map[Result[string]][]Result[string], error) {
listDirectionStr, err := listDirection.toString()
if err != nil {
return nil, err
}

args := make([]string, 0, len(keys)+5)
janhavigupta007 marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, utils.FloatToString(timeout), strconv.Itoa(len(keys)))
args = append(args, keys...)
args = append(args, listDirectionStr, CountKeyword, utils.IntToString(count))
result, err := client.executeCommand(C.BLMPop, args)
if err != nil {
return nil, err
}

return handleStringToStringArrayMapOrNullResponse(result)
}

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

return handleStringResponse(result)
}

func (client *baseClient) LMove(
source string,
destination string,
whereFrom ListDirection,
whereTo ListDirection,
) (Result[string], error) {
whereFromStr, err := whereFrom.toString()
if err != nil {
return CreateNilStringResult(), err
}
whereToStr, err := whereTo.toString()
if err != nil {
return CreateNilStringResult(), err
}

result, err := client.executeCommand(C.LMove, []string{source, destination, whereFromStr, whereToStr})
if err != nil {
return CreateNilStringResult(), err
}

return handleStringOrNullResponse(result)
}

func (client *baseClient) BLMove(
source string,
destination string,
whereFrom ListDirection,
whereTo ListDirection,
timeout float64,
) (Result[string], error) {
whereFromStr, err := whereFrom.toString()
if err != nil {
return CreateNilStringResult(), err
}
whereToStr, err := whereTo.toString()
if err != nil {
return CreateNilStringResult(), err
}

result, err := client.executeCommand(
C.BLMove,
[]string{source, destination, whereFromStr, whereToStr, utils.FloatToString(timeout)},
)
if err != nil {
return CreateNilStringResult(), err
}

return handleStringOrNullResponse(result)
}

func (client *baseClient) Ping() (string, error) {
result, err := client.executeCommand(C.Ping, []string{})
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,24 @@ func (insertPosition InsertPosition) toString() (string, error) {
return "", &RequestError{"Invalid insert position"}
}
}

// Enumeration representing element popping or adding direction for the [api.ListCommands].
type ListDirection string

const (
// Represents the option that elements should be popped from or added to the left side of a list.
Left ListDirection = "LEFT"
// Represents the option that elements should be popped from or added to the right side of a list.
janhavigupta007 marked this conversation as resolved.
Show resolved Hide resolved
Right ListDirection = "RIGHT"
)

func (listDirection ListDirection) toString() (string, error) {
switch listDirection {
case Left:
return string(Left), nil
case Right:
return string(Right), nil
default:
return "", &RequestError{"Invalid list direction"}
}
}
Loading
Loading