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

fix #1906 - implement BITFIELD_RO (#1988)

* fix #1906 - implement BITFIELD_RO

* set bitfield_ro min version to 6.2
This commit is contained in:
Leibale Eidelman
2022-03-02 05:29:42 -05:00
committed by GitHub
parent ac7d50c731
commit 9180b91047
5 changed files with 74 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
import * as APPEND from '../commands/APPEND'; import * as APPEND from '../commands/APPEND';
import * as BITCOUNT from '../commands/BITCOUNT'; import * as BITCOUNT from '../commands/BITCOUNT';
import * as BITFIELD_RO from '../commands/BITFIELD_RO';
import * as BITFIELD from '../commands/BITFIELD'; import * as BITFIELD from '../commands/BITFIELD';
import * as BITOP from '../commands/BITOP'; import * as BITOP from '../commands/BITOP';
import * as BITPOS from '../commands/BITPOS'; import * as BITPOS from '../commands/BITPOS';
@@ -181,6 +182,8 @@ export default {
append: APPEND, append: APPEND,
BITCOUNT, BITCOUNT,
bitCount: BITCOUNT, bitCount: BITCOUNT,
BITFIELD_RO,
bitFieldRo: BITFIELD_RO,
BITFIELD, BITFIELD,
bitField: BITFIELD, bitField: BITFIELD,
BITOP, BITOP,

View File

@@ -10,14 +10,14 @@ describe('BITFIELD', () => {
behavior: 'WRAP' behavior: 'WRAP'
}, { }, {
operation: 'GET', operation: 'GET',
type: 'i8', encoding: 'i8',
offset: 0 offset: 0
}, { }, {
operation: 'OVERFLOW', operation: 'OVERFLOW',
behavior: 'SAT' behavior: 'SAT'
}, { }, {
operation: 'SET', operation: 'SET',
type: 'i16', encoding: 'i16',
offset: 1, offset: 1,
value: 0 value: 0
}, { }, {
@@ -25,7 +25,7 @@ describe('BITFIELD', () => {
behavior: 'FAIL' behavior: 'FAIL'
}, { }, {
operation: 'INCRBY', operation: 'INCRBY',
type: 'i32', encoding: 'i32',
offset: 2, offset: 2,
increment: 1 increment: 1
}]), }]),
@@ -35,8 +35,12 @@ describe('BITFIELD', () => {
testUtils.testWithClient('client.bitField', async client => { testUtils.testWithClient('client.bitField', async client => {
assert.deepEqual( assert.deepEqual(
await client.bitField('key', []), await client.bitField('key', [{
[] operation: 'GET',
encoding: 'i8',
offset: 0
}]),
[0]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -1,26 +1,24 @@
export const FIRST_KEY_INDEX = 1; export const FIRST_KEY_INDEX = 1;
export const IS_READ_ONLY = true; export type BitFieldEncoding = `${'i' | 'u'}${number}`;
type BitFieldType = string; // TODO 'i[1-64]' | 'u[1-63]' export interface BitFieldOperation<S extends string> {
interface BitFieldOperation<S extends string> {
operation: S; operation: S;
} }
interface BitFieldGetOperation extends BitFieldOperation<'GET'> { export interface BitFieldGetOperation extends BitFieldOperation<'GET'> {
type: BitFieldType; encoding: BitFieldEncoding;
offset: number | string; offset: number | string;
} }
interface BitFieldSetOperation extends BitFieldOperation<'SET'> { interface BitFieldSetOperation extends BitFieldOperation<'SET'> {
type: BitFieldType; encoding: BitFieldEncoding;
offset: number | string; offset: number | string;
value: number; value: number;
} }
interface BitFieldIncrByOperation extends BitFieldOperation<'INCRBY'> { interface BitFieldIncrByOperation extends BitFieldOperation<'INCRBY'> {
type: BitFieldType; encoding: BitFieldEncoding;
offset: number | string; offset: number | string;
increment: number; increment: number;
} }
@@ -44,7 +42,7 @@ export function transformArguments(key: string, operations: BitFieldOperations):
case 'GET': case 'GET':
args.push( args.push(
'GET', 'GET',
options.type, options.encoding,
options.offset.toString() options.offset.toString()
); );
break; break;
@@ -52,7 +50,7 @@ export function transformArguments(key: string, operations: BitFieldOperations):
case 'SET': case 'SET':
args.push( args.push(
'SET', 'SET',
options.type, options.encoding,
options.offset.toString(), options.offset.toString(),
options.value.toString() options.value.toString()
); );
@@ -61,7 +59,7 @@ export function transformArguments(key: string, operations: BitFieldOperations):
case 'INCRBY': case 'INCRBY':
args.push( args.push(
'INCRBY', 'INCRBY',
options.type, options.encoding,
options.offset.toString(), options.offset.toString(),
options.increment.toString() options.increment.toString()
); );

View File

@@ -0,0 +1,27 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './BITFIELD_RO';
describe('BITFIELD RO', () => {
testUtils.isVersionGreaterThanHook([6, 2]);
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', [{
encoding: 'i8',
offset: 0
}]),
['BITFIELD_RO', 'key', 'GET', 'i8', '0']
);
});
testUtils.testWithClient('client.bitFieldRo', async client => {
assert.deepEqual(
await client.bitFieldRo('key', [{
encoding: 'i8',
offset: 0
}]),
[0]
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,26 @@
import { BitFieldGetOperation } from './BITFIELD';
export const FIRST_KEY_INDEX = 1;
export const IS_READ_ONLY = true;
type BitFieldRoOperations = Array<
Omit<BitFieldGetOperation, 'operation'> &
Partial<Pick<BitFieldGetOperation, 'operation'>>
>;
export function transformArguments(key: string, operations: BitFieldRoOperations): Array<string> {
const args = ['BITFIELD_RO', key];
for (const operation of operations) {
args.push(
'GET',
operation.encoding,
operation.offset.toString()
);
}
return args;
}
export declare function transformReply(): Array<number | null>;