All files / src/resp/command/list lset-command.ts

37.03% Statements 10/27
0% Branches 0/6
50% Functions 1/2
37.03% Lines 10/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 691x   1x     1x 1x                               1x 4x   4x   4x   4x   4x                                                                          
import { Logger } from "../../../logger";
import { IRequest } from "../../../server/request";
import { DataType } from "../../data/data-type";
import { Database } from "../../data/database";
import { DatabaseValue } from "../../data/database-value";
import { RedisToken } from "../../protocol/redis-token";
import { IRespCommand } from "../resp-command";
 
/**
 * ### Available since 1.0.0.
 * ### LSET key index element
 * Sets the list element at index to element. For more information on the index argument,
 * see {@link resp/command/list/lindex-command.LIndexCommand | LINDEX}.
 *
 * **unit-redisness-test** follows the observed behavior of Redis - if the key does not exist
 * then we return 'ERR no such key'
 *
 * An error is returned for out of range indexes.
 *
 * ###Return value
 * Simple string reply
 */
export class LSetCommand extends IRespCommand {
    public DbDataType = DataType.LIST
 
    public maxParams = 3
 
    public minParams = 3
 
    public name = "lindex"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const key: string = request.getParam(0),
            list: DatabaseValue = db.get(key);
        this.logger.debug(`Getting list "${key}"`);
        if (!list) {
            this.logger.debug(`LIST ${key} does not exist.`);
            return RedisToken.error("ERR no such key");
        }
 
        // Validate the index exists.  Positive index from ZERO, Negative index from .length
        const index: string = request.getParam(1);
        this.logger.debug(`Validating list index ${index}`);
        if (isNaN(Number(index)) || Math.abs(Number(index)) >= list.getList().length) {
            this.logger.debug(`Index ${index} is invalid for ${key}`);
            return RedisToken.error("ERR value is not an integer or out of range");
        }
 
        const value: string = request.getParam(2);
        this.logger.debug(`Setting element ${index} of key ${key} to ${value}`);
        list.getList().splice(
            Number(index),
            1,
            value
        );
        db.put(
            key,
            list
        );
        return RedisToken.responseOk();
    }
}