Skip to content

fix #1906 - implement BITFIELD_RO #1988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/client/lib/cluster/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import * as APPEND from '../commands/APPEND';
import * as BITCOUNT from '../commands/BITCOUNT';
import * as BITFIELD_RO from '../commands/BITFIELD_RO';
import * as BITFIELD from '../commands/BITFIELD';
import * as BITOP from '../commands/BITOP';
import * as BITPOS from '../commands/BITPOS';
Expand Down Expand Up @@ -181,6 +182,8 @@ export default {
append: APPEND,
BITCOUNT,
bitCount: BITCOUNT,
BITFIELD_RO,
bitFieldRo: BITFIELD_RO,
BITFIELD,
bitField: BITFIELD,
BITOP,
Expand Down
14 changes: 9 additions & 5 deletions packages/client/lib/commands/BITFIELD.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ describe('BITFIELD', () => {
behavior: 'WRAP'
}, {
operation: 'GET',
type: 'i8',
encoding: 'i8',
offset: 0
}, {
operation: 'OVERFLOW',
behavior: 'SAT'
}, {
operation: 'SET',
type: 'i16',
encoding: 'i16',
offset: 1,
value: 0
}, {
operation: 'OVERFLOW',
behavior: 'FAIL'
}, {
operation: 'INCRBY',
type: 'i32',
encoding: 'i32',
offset: 2,
increment: 1
}]),
Expand All @@ -35,8 +35,12 @@ describe('BITFIELD', () => {

testUtils.testWithClient('client.bitField', async client => {
assert.deepEqual(
await client.bitField('key', []),
[]
await client.bitField('key', [{
operation: 'GET',
encoding: 'i8',
offset: 0
}]),
[0]
);
}, GLOBAL.SERVERS.OPEN);
});
20 changes: 9 additions & 11 deletions packages/client/lib/commands/BITFIELD.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
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]'

interface BitFieldOperation<S extends string> {
export interface BitFieldOperation<S extends string> {
operation: S;
}

interface BitFieldGetOperation extends BitFieldOperation<'GET'> {
type: BitFieldType;
export interface BitFieldGetOperation extends BitFieldOperation<'GET'> {
encoding: BitFieldEncoding;
offset: number | string;
}

interface BitFieldSetOperation extends BitFieldOperation<'SET'> {
type: BitFieldType;
encoding: BitFieldEncoding;
offset: number | string;
value: number;
}

interface BitFieldIncrByOperation extends BitFieldOperation<'INCRBY'> {
type: BitFieldType;
encoding: BitFieldEncoding;
offset: number | string;
increment: number;
}
Expand All @@ -44,15 +42,15 @@ export function transformArguments(key: string, operations: BitFieldOperations):
case 'GET':
args.push(
'GET',
options.type,
options.encoding,
options.offset.toString()
);
break;

case 'SET':
args.push(
'SET',
options.type,
options.encoding,
options.offset.toString(),
options.value.toString()
);
Expand All @@ -61,7 +59,7 @@ export function transformArguments(key: string, operations: BitFieldOperations):
case 'INCRBY':
args.push(
'INCRBY',
options.type,
options.encoding,
options.offset.toString(),
options.increment.toString()
);
Expand Down
27 changes: 27 additions & 0 deletions packages/client/lib/commands/BITFIELD_RO.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
26 changes: 26 additions & 0 deletions packages/client/lib/commands/BITFIELD_RO.ts
Original file line number Diff line number Diff line change
@@ -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>;