Skip to content

Commit 9180b91

Browse files
authored
fix #1906 - implement BITFIELD_RO (#1988)
* fix #1906 - implement BITFIELD_RO * set bitfield_ro min version to 6.2
1 parent ac7d50c commit 9180b91

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

packages/client/lib/cluster/commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import * as APPEND from '../commands/APPEND';
33
import * as BITCOUNT from '../commands/BITCOUNT';
4+
import * as BITFIELD_RO from '../commands/BITFIELD_RO';
45
import * as BITFIELD from '../commands/BITFIELD';
56
import * as BITOP from '../commands/BITOP';
67
import * as BITPOS from '../commands/BITPOS';
@@ -181,6 +182,8 @@ export default {
181182
append: APPEND,
182183
BITCOUNT,
183184
bitCount: BITCOUNT,
185+
BITFIELD_RO,
186+
bitFieldRo: BITFIELD_RO,
184187
BITFIELD,
185188
bitField: BITFIELD,
186189
BITOP,

packages/client/lib/commands/BITFIELD.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ describe('BITFIELD', () => {
1010
behavior: 'WRAP'
1111
}, {
1212
operation: 'GET',
13-
type: 'i8',
13+
encoding: 'i8',
1414
offset: 0
1515
}, {
1616
operation: 'OVERFLOW',
1717
behavior: 'SAT'
1818
}, {
1919
operation: 'SET',
20-
type: 'i16',
20+
encoding: 'i16',
2121
offset: 1,
2222
value: 0
2323
}, {
2424
operation: 'OVERFLOW',
2525
behavior: 'FAIL'
2626
}, {
2727
operation: 'INCRBY',
28-
type: 'i32',
28+
encoding: 'i32',
2929
offset: 2,
3030
increment: 1
3131
}]),
@@ -35,8 +35,12 @@ describe('BITFIELD', () => {
3535

3636
testUtils.testWithClient('client.bitField', async client => {
3737
assert.deepEqual(
38-
await client.bitField('key', []),
39-
[]
38+
await client.bitField('key', [{
39+
operation: 'GET',
40+
encoding: 'i8',
41+
offset: 0
42+
}]),
43+
[0]
4044
);
4145
}, GLOBAL.SERVERS.OPEN);
4246
});

packages/client/lib/commands/BITFIELD.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
export const FIRST_KEY_INDEX = 1;
22

3-
export const IS_READ_ONLY = true;
3+
export type BitFieldEncoding = `${'i' | 'u'}${number}`;
44

5-
type BitFieldType = string; // TODO 'i[1-64]' | 'u[1-63]'
6-
7-
interface BitFieldOperation<S extends string> {
5+
export interface BitFieldOperation<S extends string> {
86
operation: S;
97
}
108

11-
interface BitFieldGetOperation extends BitFieldOperation<'GET'> {
12-
type: BitFieldType;
9+
export interface BitFieldGetOperation extends BitFieldOperation<'GET'> {
10+
encoding: BitFieldEncoding;
1311
offset: number | string;
1412
}
1513

1614
interface BitFieldSetOperation extends BitFieldOperation<'SET'> {
17-
type: BitFieldType;
15+
encoding: BitFieldEncoding;
1816
offset: number | string;
1917
value: number;
2018
}
2119

2220
interface BitFieldIncrByOperation extends BitFieldOperation<'INCRBY'> {
23-
type: BitFieldType;
21+
encoding: BitFieldEncoding;
2422
offset: number | string;
2523
increment: number;
2624
}
@@ -44,15 +42,15 @@ export function transformArguments(key: string, operations: BitFieldOperations):
4442
case 'GET':
4543
args.push(
4644
'GET',
47-
options.type,
45+
options.encoding,
4846
options.offset.toString()
4947
);
5048
break;
5149

5250
case 'SET':
5351
args.push(
5452
'SET',
55-
options.type,
53+
options.encoding,
5654
options.offset.toString(),
5755
options.value.toString()
5856
);
@@ -61,7 +59,7 @@ export function transformArguments(key: string, operations: BitFieldOperations):
6159
case 'INCRBY':
6260
args.push(
6361
'INCRBY',
64-
options.type,
62+
options.encoding,
6563
options.offset.toString(),
6664
options.increment.toString()
6765
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './BITFIELD_RO';
4+
5+
describe('BITFIELD RO', () => {
6+
testUtils.isVersionGreaterThanHook([6, 2]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments('key', [{
11+
encoding: 'i8',
12+
offset: 0
13+
}]),
14+
['BITFIELD_RO', 'key', 'GET', 'i8', '0']
15+
);
16+
});
17+
18+
testUtils.testWithClient('client.bitFieldRo', async client => {
19+
assert.deepEqual(
20+
await client.bitFieldRo('key', [{
21+
encoding: 'i8',
22+
offset: 0
23+
}]),
24+
[0]
25+
);
26+
}, GLOBAL.SERVERS.OPEN);
27+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { BitFieldGetOperation } from './BITFIELD';
2+
3+
export const FIRST_KEY_INDEX = 1;
4+
5+
export const IS_READ_ONLY = true;
6+
7+
type BitFieldRoOperations = Array<
8+
Omit<BitFieldGetOperation, 'operation'> &
9+
Partial<Pick<BitFieldGetOperation, 'operation'>>
10+
>;
11+
12+
export function transformArguments(key: string, operations: BitFieldRoOperations): Array<string> {
13+
const args = ['BITFIELD_RO', key];
14+
15+
for (const operation of operations) {
16+
args.push(
17+
'GET',
18+
operation.encoding,
19+
operation.offset.toString()
20+
);
21+
}
22+
23+
return args;
24+
}
25+
26+
export declare function transformReply(): Array<number | null>;

0 commit comments

Comments
 (0)