Skip to content

Commit d3252e8

Browse files
Merge pull request #92 from EchoVault/fix/list-module
Updated list module commands to return expected results.
2 parents ffa8988 + f86d67d commit d3252e8

File tree

7 files changed

+672
-477
lines changed

7 files changed

+672
-477
lines changed

echovault/api_list.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ func (server *EchoVault) LLen(key string) (int, error) {
5555
// Errors:
5656
//
5757
// "LRange command on non-list item" - when the provided key exists but is not a list.
58-
//
59-
// "start index must be within list boundary" - when the start index is not within the list boundaries.
60-
//
61-
// "end index must be within list range or -1" - when end index is not within the list boundaries.
6258
func (server *EchoVault) LRange(key string, start, end int) ([]string, error) {
6359
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"LRANGE", key, strconv.Itoa(start), strconv.Itoa(end)}), nil, false, true)
6460
if err != nil {
@@ -80,8 +76,6 @@ func (server *EchoVault) LRange(key string, start, end int) ([]string, error) {
8076
// Errors:
8177
//
8278
// "LIndex command on non-list item" - when the provided key exists but is not a list.
83-
//
84-
// "index must be within list range" - when the index is not within the list boundary.
8579
func (server *EchoVault) LIndex(key string, index uint) (string, error) {
8680
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"LINDEX", key, strconv.Itoa(int(index))}), nil, false, true)
8781
if err != nil {
@@ -139,23 +133,22 @@ func (server *EchoVault) LTrim(key string, start int, end int) (bool, error) {
139133
//
140134
// `value` - string - the element to remove.
141135
//
142-
// Returns: true if the removal was successful.
136+
// Returns: An integer representing the number of elements removed.
143137
//
144138
// Errors:
145139
//
146140
// "LRem command on non-list item" - when the provided key exists but is not a list.
147-
func (server *EchoVault) LRem(key string, count int, value string) (bool, error) {
141+
func (server *EchoVault) LRem(key string, count int, value string) (int, error) {
148142
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{
149143
"LREM", key, strconv.Itoa(count), value}),
150144
nil,
151145
false,
152146
true,
153147
)
154148
if err != nil {
155-
return false, err
149+
return 0, err
156150
}
157-
s, err := internal.ParseStringResponse(b)
158-
return strings.EqualFold(s, "ok"), err
151+
return internal.ParseIntegerResponse(b)
159152
}
160153

161154
// LMove moves an element from one list to another.
@@ -194,17 +187,23 @@ func (server *EchoVault) LMove(source, destination, whereFrom, whereTo string) (
194187
//
195188
// `key` - string - the key to the list.
196189
//
197-
// Returns: The popped element as a string.
190+
// Returns: A string slice containing the popped elements.
198191
//
199192
// Errors:
200193
//
201-
// "LPop command on non-list item" - when the provided key is not a list.
202-
func (server *EchoVault) LPop(key string) (string, error) {
203-
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"LPOP", key}), nil, false, true)
194+
// "LPOP command on non-list item" - when the provided key is not a list.
195+
func (server *EchoVault) LPop(key string, count uint) ([]string, error) {
196+
b, err := server.handleCommand(
197+
server.context,
198+
internal.EncodeCommand([]string{"LPOP", key, strconv.Itoa(int(count))}),
199+
nil,
200+
false,
201+
true,
202+
)
204203
if err != nil {
205-
return "", err
204+
return []string{}, err
206205
}
207-
return internal.ParseStringResponse(b)
206+
return internal.ParseStringArrayResponse(b)
208207
}
209208

210209
// RPop pops an element from the end of the list and return it.
@@ -213,17 +212,23 @@ func (server *EchoVault) LPop(key string) (string, error) {
213212
//
214213
// `key` - string - the key to the list.
215214
//
216-
// Returns: The popped element as a string.
215+
// Returns: A string slice containing the popped elements.
217216
//
218217
// Errors:
219218
//
220-
// "RPop command on non-list item" - when the provided key is not a list.
221-
func (server *EchoVault) RPop(key string) (string, error) {
222-
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"RPOP", key}), nil, false, true)
219+
// "RPOP command on non-list item" - when the provided key is not a list.
220+
func (server *EchoVault) RPop(key string, count uint) ([]string, error) {
221+
b, err := server.handleCommand(
222+
server.context,
223+
internal.EncodeCommand([]string{"RPOP", key, strconv.Itoa(int(count))}),
224+
nil,
225+
false,
226+
true,
227+
)
223228
if err != nil {
224-
return "", err
229+
return []string{}, err
225230
}
226-
return internal.ParseStringResponse(b)
231+
return internal.ParseStringArrayResponse(b)
227232
}
228233

229234
// LPush pushed 1 or more values to the beginning of a list. If the list does not exist, a new list is created

0 commit comments

Comments
 (0)