1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

fix #1738 - add support for buffers in HSET

This commit is contained in:
leibale
2021-11-26 22:57:19 -05:00
parent 14b16b9e17
commit c96a0dd904
2 changed files with 47 additions and 19 deletions

View File

@@ -1,24 +1,27 @@
import { RedisCommandArguments } from '.';
type HSETObject = Record<string | number, string | number>;
type Types = string | number | Buffer;
type HSETMap = Map<string | number, string | number>;
type HSETObject = Record<string | number, Types>;
type HSETTuples = Array<[string, string]> | Array<string>;
type HSETMap = Map<Types, Types>;
type HSETTuples = Array<[Types, Types]> | Array<Types>;
export const FIRST_KEY_INDEX = 1;
type GenericArguments = [key: string];
type GenericArguments = [key: string | Buffer];
type SingleFieldArguments = [...generic: GenericArguments, field: string, value: string];
type SingleFieldArguments = [...generic: GenericArguments, field: Types, value: Types];
type MultipleFieldsArguments = [...generic: GenericArguments, value: HSETObject | HSETMap | HSETTuples];
export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArguments | MultipleFieldsArguments): RedisCommandArguments {
const args = ['HSET', key];
const args: RedisCommandArguments = ['HSET', key];
if (typeof value === 'string') {
args.push(value, fieldValue!);
if (typeof value === 'string' || typeof value === 'number' || Buffer.isBuffer(value)) {
pushValue(args, value);
pushValue(args, fieldValue!);
} else if (value instanceof Map) {
pushMap(args, value);
} else if (Array.isArray(value)) {
@@ -30,20 +33,36 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg
return args;
}
function pushMap(args: Array<string>, map: HSETMap): void {
function pushMap(args: RedisCommandArguments, map: HSETMap): void {
for (const [key, value] of map.entries()) {
args.push(key.toString(), value.toString());
pushValue(args, key);
pushValue(args, value);
}
}
function pushTuples(args: Array<string>, tuples: HSETTuples): void {
args.push(...tuples.flat());
function pushTuples(args: RedisCommandArguments, tuples: HSETTuples): void {
for (const tuple of tuples) {
if (Array.isArray(tuple)) {
pushTuples(args, tuple);
continue;
}
pushValue(args, tuple);
}
}
function pushObject(args: Array<string>, object: HSETObject): void {
function pushObject(args: RedisCommandArguments, object: HSETObject): void {
for (const key of Object.keys(object)) {
args.push(key.toString(), object[key].toString());
}
}
function pushValue(args: RedisCommandArguments, value: Types): void {
args.push(
typeof value === 'number' ?
value.toString() :
value
);
}
export declare function transformReply(): number;