Skip to content

Commit 4708736

Browse files
authored
new "transform arguments" API for better key and metadata extraction (#2733)
* Parser support with all commands * remove "dist" from all imports for consistency * address most of my review comments * small tweak to multi type mapping handling * tweak multi commands / fix addScript cases * nits * addressed all in person review comments * revert addCommand/addScript changes to multi-commands addCommand needs to be there for sendCommand like ability within a multi. If its there, it might as well be used by createCommand() et al, to avoid repeating code. addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
1 parent 5ace34b commit 4708736

File tree

1,016 files changed

+6345
-6540
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,016 files changed

+6345
-6540
lines changed

package-lock.json

+754-1,757
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/bloom/lib/commands/bloom/ADD.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import ADD from './ADD';
4+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
45

56
describe('BF.ADD', () => {
67
it('transformArguments', () => {
78
assert.deepEqual(
8-
ADD.transformArguments('key', 'item'),
9+
parseArgs(ADD, 'key', 'item'),
910
['BF.ADD', 'key', 'item']
1011
);
1112
});
+7-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
2-
import { transformBooleanReply } from '@redis/client/dist/lib/commands/generic-transformers';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
3+
import { transformBooleanReply } from '@redis/client/lib/commands/generic-transformers';
34

45
export default {
5-
FIRST_KEY_INDEX: 1,
66
IS_READ_ONLY: false,
7-
transformArguments(key: RedisArgument, item: RedisArgument) {
8-
return ['BF.ADD', key, item];
7+
parseCommand(parser: CommandParser, key: RedisArgument, item: RedisArgument) {
8+
parser.push('BF.ADD');
9+
parser.pushKey(key);
10+
parser.push(item);
911
},
1012
transformReply: transformBooleanReply
1113
} as const satisfies Command;

packages/bloom/lib/commands/bloom/CARD.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import CARD from './CARD';
4+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
45

56
describe('BF.CARD', () => {
67
it('transformArguments', () => {
78
assert.deepEqual(
8-
CARD.transformArguments('bloom'),
9+
parseArgs(CARD, 'bloom'),
910
['BF.CARD', 'bloom']
1011
);
1112
});
+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { RedisArgument, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, NumberReply, Command } from '@redis/client/lib/RESP/types';
23

34
export default {
4-
FIRST_KEY_INDEX: 1,
55
IS_READ_ONLY: true,
6-
transformArguments(key: RedisArgument) {
7-
return ['BF.CARD', key];
6+
parseCommand(parser: CommandParser, key: RedisArgument) {
7+
parser.push('BF.CARD');
8+
parser.pushKey(key);
89
},
910
transformReply: undefined as unknown as () => NumberReply
1011
} as const satisfies Command;

packages/bloom/lib/commands/bloom/EXISTS.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import EXISTS from './EXISTS';
4+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
45

56
describe('BF.EXISTS', () => {
67
it('transformArguments', () => {
78
assert.deepEqual(
8-
EXISTS.transformArguments('key', 'item'),
9+
parseArgs(EXISTS, 'key', 'item'),
910
['BF.EXISTS', 'key', 'item']
1011
);
1112
});
+7-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
2-
import { transformBooleanReply } from '@redis/client/dist/lib/commands/generic-transformers';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
3+
import { transformBooleanReply } from '@redis/client/lib/commands/generic-transformers';
34

45
export default {
5-
FIRST_KEY_INDEX: 1,
66
IS_READ_ONLY: true,
7-
transformArguments(key: RedisArgument, item: RedisArgument) {
8-
return ['BF.EXISTS', key, item];
7+
parseCommand(parser: CommandParser, key: RedisArgument, item: RedisArgument) {
8+
parser.push('BF.EXISTS');
9+
parser.pushKey(key);
10+
parser.push(item);
911
},
1012
transformReply: transformBooleanReply
1113
} as const satisfies Command;

packages/bloom/lib/commands/bloom/INFO.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import INFO from './INFO';
4+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
45

56
describe('BF.INFO', () => {
67
it('transformArguments', () => {
78
assert.deepEqual(
8-
INFO.transformArguments('bloom'),
9+
parseArgs(INFO, 'bloom'),
910
['BF.INFO', 'bloom']
1011
);
1112
});

packages/bloom/lib/commands/bloom/INFO.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RedisArgument, Command, UnwrapReply, NullReply, NumberReply, TuplesToMapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, Command, UnwrapReply, NullReply, NumberReply, TuplesToMapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/lib/RESP/types';
23
import { transformInfoV2Reply } from '.';
34

45
export type BfInfoReplyMap = TuplesToMapReply<[
@@ -9,19 +10,11 @@ export type BfInfoReplyMap = TuplesToMapReply<[
910
[SimpleStringReply<'Expansion rate'>, NullReply | NumberReply]
1011
]>;
1112

12-
export interface BfInfoReply {
13-
capacity: NumberReply;
14-
size: NumberReply;
15-
numberOfFilters: NumberReply;
16-
numberOfInsertedItems: NumberReply;
17-
expansionRate: NullReply | NumberReply;
18-
}
19-
2013
export default {
21-
FIRST_KEY_INDEX: 1,
2214
IS_READ_ONLY: true,
23-
transformArguments(key: RedisArgument) {
24-
return ['BF.INFO', key];
15+
parseCommand(parser: CommandParser, key: RedisArgument) {
16+
parser.push('BF.INFO');
17+
parser.pushKey(key);
2518
},
2619
transformReply: {
2720
2: (reply: UnwrapReply<Resp2Reply<BfInfoReplyMap>>, _, typeMapping?: TypeMapping): BfInfoReplyMap => {

packages/bloom/lib/commands/bloom/INSERT.spec.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import INSERT from './INSERT';
4+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
45

56
describe('BF.INSERT', () => {
67
describe('transformArguments', () => {
78
it('simple', () => {
89
assert.deepEqual(
9-
INSERT.transformArguments('key', 'item'),
10+
parseArgs(INSERT, 'key', 'item'),
1011
['BF.INSERT', 'key', 'ITEMS', 'item']
1112
);
1213
});
1314

1415
it('with CAPACITY', () => {
1516
assert.deepEqual(
16-
INSERT.transformArguments('key', 'item', { CAPACITY: 100 }),
17+
parseArgs(INSERT, 'key', 'item', { CAPACITY: 100 }),
1718
['BF.INSERT', 'key', 'CAPACITY', '100', 'ITEMS', 'item']
1819
);
1920
});
2021

2122
it('with ERROR', () => {
2223
assert.deepEqual(
23-
INSERT.transformArguments('key', 'item', { ERROR: 0.01 }),
24+
parseArgs(INSERT, 'key', 'item', { ERROR: 0.01 }),
2425
['BF.INSERT', 'key', 'ERROR', '0.01', 'ITEMS', 'item']
2526
);
2627
});
2728

2829
it('with EXPANSION', () => {
2930
assert.deepEqual(
30-
INSERT.transformArguments('key', 'item', { EXPANSION: 1 }),
31+
parseArgs(INSERT, 'key', 'item', { EXPANSION: 1 }),
3132
['BF.INSERT', 'key', 'EXPANSION', '1', 'ITEMS', 'item']
3233
);
3334
});
3435

3536
it('with NOCREATE', () => {
3637
assert.deepEqual(
37-
INSERT.transformArguments('key', 'item', { NOCREATE: true }),
38+
parseArgs(INSERT, 'key', 'item', { NOCREATE: true }),
3839
['BF.INSERT', 'key', 'NOCREATE', 'ITEMS', 'item']
3940
);
4041
});
4142

4243
it('with NONSCALING', () => {
4344
assert.deepEqual(
44-
INSERT.transformArguments('key', 'item', { NONSCALING: true }),
45+
parseArgs(INSERT, 'key', 'item', { NONSCALING: true }),
4546
['BF.INSERT', 'key', 'NONSCALING', 'ITEMS', 'item']
4647
);
4748
});
4849

4950
it('with CAPACITY, ERROR, EXPANSION, NOCREATE and NONSCALING', () => {
5051
assert.deepEqual(
51-
INSERT.transformArguments('key', 'item', {
52+
parseArgs(INSERT, 'key', 'item', {
5253
CAPACITY: 100,
5354
ERROR: 0.01,
5455
EXPANSION: 1,
+15-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
2-
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { transformBooleanArrayReply } from '@redis/client/dist/lib/commands/generic-transformers';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
3+
import { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
4+
import { transformBooleanArrayReply } from '@redis/client/lib/commands/generic-transformers';
45

56
export interface BfInsertOptions {
67
CAPACITY?: number;
@@ -11,37 +12,38 @@ export interface BfInsertOptions {
1112
}
1213

1314
export default {
14-
FIRST_KEY_INDEX: 1,
1515
IS_READ_ONLY: false,
16-
transformArguments(
16+
parseCommand(
17+
parser: CommandParser,
1718
key: RedisArgument,
1819
items: RedisVariadicArgument,
1920
options?: BfInsertOptions
2021
) {
21-
const args = ['BF.INSERT', key];
22+
parser.push('BF.INSERT');
23+
parser.pushKey(key);
2224

2325
if (options?.CAPACITY !== undefined) {
24-
args.push('CAPACITY', options.CAPACITY.toString());
26+
parser.push('CAPACITY', options.CAPACITY.toString());
2527
}
2628

2729
if (options?.ERROR !== undefined) {
28-
args.push('ERROR', options.ERROR.toString());
30+
parser.push('ERROR', options.ERROR.toString());
2931
}
3032

3133
if (options?.EXPANSION !== undefined) {
32-
args.push('EXPANSION', options.EXPANSION.toString());
34+
parser.push('EXPANSION', options.EXPANSION.toString());
3335
}
3436

3537
if (options?.NOCREATE) {
36-
args.push('NOCREATE');
38+
parser.push('NOCREATE');
3739
}
3840

3941
if (options?.NONSCALING) {
40-
args.push('NONSCALING');
42+
parser.push('NONSCALING');
4143
}
4244

43-
args.push('ITEMS');
44-
return pushVariadicArguments(args, items);
45+
parser.push('ITEMS');
46+
parser.pushVariadic(items);
4547
},
4648
transformReply: transformBooleanArrayReply
4749
} as const satisfies Command;

packages/bloom/lib/commands/bloom/LOADCHUNK.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import LOADCHUNK from './LOADCHUNK';
44
import { RESP_TYPES } from '@redis/client';
5+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
56

67
describe('BF.LOADCHUNK', () => {
78
it('transformArguments', () => {
89
assert.deepEqual(
9-
LOADCHUNK.transformArguments('key', 0, ''),
10+
parseArgs(LOADCHUNK, 'key', 0, ''),
1011
['BF.LOADCHUNK', 'key', '0', '']
1112
);
1213
});
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/lib/RESP/types';
23

34
export default {
4-
FIRST_KEY_INDEX: 1,
55
IS_READ_ONLY: false,
6-
transformArguments(key: RedisArgument, iterator: number, chunk: RedisArgument) {
7-
return ['BF.LOADCHUNK', key, iterator.toString(), chunk];
6+
parseCommand(parser: CommandParser, key: RedisArgument, iterator: number, chunk: RedisArgument) {
7+
parser.push('BF.LOADCHUNK');
8+
parser.pushKey(key);
9+
parser.push(iterator.toString(), chunk);
810
},
911
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
1012
} as const satisfies Command;

packages/bloom/lib/commands/bloom/MADD.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import MADD from './MADD';
4+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
45

56
describe('BF.MADD', () => {
67
it('transformArguments', () => {
78
assert.deepEqual(
8-
MADD.transformArguments('key', ['1', '2']),
9+
parseArgs(MADD, 'key', ['1', '2']),
910
['BF.MADD', 'key', '1', '2']
1011
);
1112
});
+8-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
2-
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { transformBooleanArrayReply } from '@redis/client/dist/lib/commands/generic-transformers';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
3+
import { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
4+
import { transformBooleanArrayReply } from '@redis/client/lib/commands/generic-transformers';
45

56
export default {
6-
FIRST_KEY_INDEX: 1,
77
IS_READ_ONLY: false,
8-
transformArguments(key: RedisArgument, items: RedisVariadicArgument) {
9-
return pushVariadicArguments(['BF.MADD', key], items);
8+
parseCommand(parser: CommandParser, key: RedisArgument, items: RedisVariadicArgument) {
9+
parser.push('BF.MADD');
10+
parser.pushKey(key);
11+
parser.pushVariadic(items);
1012
},
1113
transformReply: transformBooleanArrayReply
1214
} as const satisfies Command;

packages/bloom/lib/commands/bloom/MEXISTS.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../../test-utils';
33
import MEXISTS from './MEXISTS';
4+
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
45

56
describe('BF.MEXISTS', () => {
67
it('transformArguments', () => {
78
assert.deepEqual(
8-
MEXISTS.transformArguments('key', ['1', '2']),
9+
parseArgs(MEXISTS, 'key', ['1', '2']),
910
['BF.MEXISTS', 'key', '1', '2']
1011
);
1112
});
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
2-
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { transformBooleanArrayReply } from '@redis/client/dist/lib/commands/generic-transformers';
1+
import { CommandParser } from '@redis/client/lib/client/parser';
2+
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
3+
import { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
4+
import { transformBooleanArrayReply } from '@redis/client/lib/commands/generic-transformers';
45

56
export default {
6-
FIRST_KEY_INDEX: 1,
77
IS_READ_ONLY: true,
8-
transformArguments(key: RedisArgument, items: RedisVariadicArgument) {
9-
return pushVariadicArguments(['BF.MEXISTS', key], items);
8+
parseCommand(parser: CommandParser, key: RedisArgument, items: RedisVariadicArgument) {
9+
parser.push('BF.MEXISTS');
10+
parser.pushKey(key);
11+
parser.pushVariadic(items);
1012
},
1113
transformReply: transformBooleanArrayReply
1214
} as const satisfies Command;

0 commit comments

Comments
 (0)