RPop returns and removes the rightmost value of a list associated with a key. If the key does not exist, an error is returned. If the list is empty, an error is returned.
(key string)
| 193 | // If the key does not exist, an error is returned. |
| 194 | // If the list is empty, an error is returned. |
| 195 | func (l *ListStructure) RPop(key string) (interface{}, error) { |
| 196 | // Get the list |
| 197 | lst, _, err := l.getListFromDB(key, false) |
| 198 | if err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | // Return error if the list is empty |
| 202 | if lst.Length == 0 { |
| 203 | return nil, ErrListEmpty |
| 204 | } else if lst.Length == 1 { |
| 205 | popValue := lst.Head.Value |
| 206 | lst.Head = nil |
| 207 | lst.Length = 0 |
| 208 | return popValue, l.setListToDB(key, lst, 0) |
| 209 | } |
| 210 | |
| 211 | // Find the new tail |
| 212 | newTail := lst.Head |
| 213 | for i := 0; i < lst.Length-2; i++ { |
| 214 | newTail = newTail.Next |
| 215 | } |
| 216 | popValue := newTail.Next.Value |
| 217 | newTail.Next = nil |
| 218 | lst.Length-- |
| 219 | |
| 220 | // Store in the database |
| 221 | return popValue, l.setListToDB(key, lst, 0) |
| 222 | } |
| 223 | |
| 224 | // LRange returns a range of elements from a list associated with a key. |
| 225 | // The range is inclusive, including both the start and stop indices. |
nothing calls this directly
no test coverage detected