Skip to content

Commit 7827988

Browse files
committed
partially parse tips and key specs [skip ci]
for now we only extract: - request and response policy -> from tips - is the command keyless -> from key specs
1 parent dec9f67 commit 7827988

File tree

4 files changed

+162
-36
lines changed

4 files changed

+162
-36
lines changed

packages/client/lib/cluster/request-response-policies/dynamic-policy-resolver-factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class DynamicPolicyResolverFactory {
8989
*/
9090
static #buildCommandPolicies(command: CommandReply): CommandPolicies {
9191
// Determine if command is keyless based on keySpecification
92-
const isKeyless = command.keySpecifications === 'keyless';
92+
const isKeyless = command.isKeyless
9393

9494
// Determine default policies based on key specification
9595
const defaultRequest = isKeyless

packages/client/lib/cluster/request-response-policies/dynamic-policy-resolver.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ describe('DynamicPolicyResolverFactory', () => {
4949
lastKeyIndex: 0,
5050
step: 0,
5151
categories: new Set(),
52-
// policies: { request: undefined, response: undefined },
53-
keySpecifications: 'keyless'
52+
policies: { request: undefined, response: undefined },
53+
isKeyless: true
5454
}
5555
];
5656

@@ -75,8 +75,8 @@ describe('DynamicPolicyResolverFactory', () => {
7575
lastKeyIndex: 1,
7676
step: 1,
7777
categories: new Set(),
78-
// policies: { request: undefined, response: undefined },
79-
keySpecifications: [Buffer.from('key')] as any
78+
policies: { request: undefined, response: undefined },
79+
isKeyless: false
8080
}
8181
];
8282

@@ -101,8 +101,8 @@ describe('DynamicPolicyResolverFactory', () => {
101101
lastKeyIndex: 0,
102102
step: 0,
103103
categories: new Set(),
104-
// policies: { request: 'all_shards', response: 'agg_sum' },
105-
keySpecifications: 'keyless'
104+
policies: { request: 'all_shards', response: 'agg_sum' },
105+
isKeyless: true
106106
}
107107
];
108108

@@ -127,8 +127,8 @@ describe('DynamicPolicyResolverFactory', () => {
127127
lastKeyIndex: 1,
128128
step: 1,
129129
categories: new Set(),
130-
// policies: { request: 'all_shards', response: 'special' },
131-
keySpecifications: [Buffer.from('key')] as any
130+
policies: { request: 'all_shards', response: 'special' },
131+
isKeyless: false
132132
}
133133
];
134134

@@ -153,8 +153,8 @@ describe('DynamicPolicyResolverFactory', () => {
153153
lastKeyIndex: 0,
154154
step: 0,
155155
categories: new Set(),
156-
// policies: { request: undefined, response: undefined },
157-
keySpecifications: 'keyless'
156+
policies: { request: undefined, response: undefined },
157+
isKeyless: true
158158
}
159159
];
160160

@@ -181,8 +181,8 @@ describe('DynamicPolicyResolverFactory', () => {
181181
lastKeyIndex: 0,
182182
step: 0,
183183
categories: new Set(),
184-
// policies: { request: undefined, response: undefined },
185-
keySpecifications: 'keyless'
184+
policies: { request: undefined, response: undefined },
185+
isKeyless: true
186186
}
187187
];
188188

@@ -232,8 +232,8 @@ describe('DynamicPolicyResolverFactory', () => {
232232
lastKeyIndex: 0,
233233
step: 0,
234234
categories: new Set(),
235-
// policies: { request: 'all_nodes', response: undefined },
236-
keySpecifications: [Buffer.from('key')] as any
235+
policies: { request: 'all_nodes', response: undefined },
236+
isKeyless: false
237237
},
238238
{
239239
name: 'partial-response',
@@ -243,8 +243,8 @@ describe('DynamicPolicyResolverFactory', () => {
243243
lastKeyIndex: 0,
244244
step: 0,
245245
categories: new Set(),
246-
// policies: { request: undefined, response: 'agg_sum' },
247-
keySpecifications: 'keyless'
246+
policies: { request: undefined, response: 'agg_sum' },
247+
isKeyless: true
248248
}
249249
];
250250

Lines changed: 123 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,125 @@
1-
// import { strict as assert } from 'node:assert';
2-
// import testUtils, { GLOBAL } from '../test-utils';
3-
// import { transformArguments } from './COMMAND';
4-
// import { assertPingCommand } from './COMMAND_INFO.spec';
1+
import { strict as assert } from 'node:assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { parseArgs, transformCommandReply, CommandFlags, CommandCategories, CommandRawReply } from './generic-transformers';
4+
import COMMAND from './COMMAND';
55

6-
// describe('COMMAND', () => {
7-
// it('transformArguments', () => {
8-
// assert.deepEqual(
9-
// transformArguments(),
10-
// ['COMMAND']
11-
// );
12-
// });
6+
describe('COMMAND', () => {
7+
it('transformArguments', () => {
8+
assert.deepEqual(
9+
parseArgs(COMMAND),
10+
['COMMAND']
11+
);
12+
});
1313

14-
// testUtils.testWithClient('client.command', async client => {
15-
// assertPingCommand((await client.command()).find(command => command.name === 'ping'));
16-
// }, GLOBAL.SERVERS.OPEN);
17-
// });
14+
describe('transformCommandReply', () => {
15+
const testCases = [
16+
{
17+
name: 'without policies',
18+
input: ['ping', -1, [CommandFlags.STALE], 0, 0, 0, [CommandCategories.FAST], [], []] satisfies CommandRawReply,
19+
expected: {
20+
name: 'ping',
21+
arity: -1,
22+
flags: new Set([CommandFlags.STALE]),
23+
firstKeyIndex: 0,
24+
lastKeyIndex: 0,
25+
step: 0,
26+
categories: new Set([CommandCategories.FAST]),
27+
policies: { request: undefined, response: undefined },
28+
isKeyless: true
29+
}
30+
},
31+
{
32+
name: 'with valid policies',
33+
input: ['dbsize', 1, [], 0, 0, 0, [], ['request_policy:all_shards', 'response_policy:agg_sum'], []] satisfies CommandRawReply,
34+
expected: {
35+
name: 'dbsize',
36+
arity: 1,
37+
flags: new Set([]),
38+
firstKeyIndex: 0,
39+
lastKeyIndex: 0,
40+
step: 0,
41+
categories: new Set([]),
42+
policies: { request: 'all_shards', response: 'agg_sum' },
43+
isKeyless: true
44+
}
45+
},
46+
{
47+
name: 'with invalid policies',
48+
input: ['test', 0, [], 0, 0, 0, [], ['request_policy:invalid', 'response_policy:invalid'], ['some key specification']] satisfies CommandRawReply,
49+
expected: {
50+
name: 'test',
51+
arity: 0,
52+
flags: new Set([]),
53+
firstKeyIndex: 0,
54+
lastKeyIndex: 0,
55+
step: 0,
56+
categories: new Set([]),
57+
policies: { request: undefined, response: undefined },
58+
isKeyless: false
59+
}
60+
},
61+
{
62+
name: 'with request policy only',
63+
input: ['test', 0, [], 0, 0, 0, [], ['request_policy:all_nodes'], ['some key specification']] satisfies CommandRawReply,
64+
expected: {
65+
name: 'test',
66+
arity: 0,
67+
flags: new Set([]),
68+
firstKeyIndex: 0,
69+
lastKeyIndex: 0,
70+
step: 0,
71+
categories: new Set([]),
72+
policies: { request: 'all_nodes', response: undefined },
73+
isKeyless: false
74+
}
75+
},
76+
{
77+
name: 'with response policy only',
78+
input: ['test', 0, [], 0, 0, 0, [], ['', 'response_policy:agg_max'], []] satisfies CommandRawReply,
79+
expected: {
80+
name: 'test',
81+
arity: 0,
82+
flags: new Set([]),
83+
firstKeyIndex: 0,
84+
lastKeyIndex: 0,
85+
step: 0,
86+
categories: new Set([]),
87+
policies: { request: undefined, response: 'agg_max' },
88+
isKeyless: true
89+
}
90+
},
91+
{
92+
name: 'with response policy only',
93+
input: ['test', 0, [], 0, 0, 0, [], ['', 'response_policy:agg_max'], []] satisfies CommandRawReply,
94+
expected: {
95+
name: 'test',
96+
arity: 0,
97+
flags: new Set([]),
98+
firstKeyIndex: 0,
99+
lastKeyIndex: 0,
100+
step: 0,
101+
categories: new Set([]),
102+
policies: { request: undefined, response: 'agg_max' },
103+
isKeyless: true
104+
}
105+
}
106+
];
107+
108+
testCases.forEach(testCase => {
109+
it(testCase.name, () => {
110+
assert.deepEqual(
111+
transformCommandReply(testCase.input),
112+
testCase.expected
113+
);
114+
});
115+
});
116+
});
117+
118+
testUtils.testWithClient('client.command', async client => {
119+
const result = ((await client.command()).find(command => command.name === 'dbsize'));
120+
assert.equal(result?.name, 'dbsize');
121+
assert.equal(result?.arity, 1);
122+
assert.equal(result?.policies?.request, 'all_shards');
123+
assert.equal(result?.policies?.response, 'agg_sum');
124+
}, GLOBAL.SERVERS.OPEN);
125+
});

packages/client/lib/commands/generic-transformers.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BasicCommandParser, CommandParser } from '../client/parser';
2+
import { REQUEST_POLICIES_WITH_DEFAULTS, RequestPolicyWithDefaults, RESPONSE_POLICIES_WITH_DEFAULTS, ResponsePolicyWithDefaults } from '../cluster/request-response-policies';
23
import { RESP_TYPES } from '../RESP/decoder';
34
import { UnwrapReply, ArrayReply, BlobStringReply, BooleanReply, CommandArguments, DoubleReply, NullReply, NumberReply, RedisArgument, TuplesReply, MapReply, TypeMapping, Command } from '../RESP/types';
45

@@ -331,9 +332,10 @@ export type CommandRawReply = [
331332
step: number,
332333
categories: Array<CommandCategories>,
333334
tips: Array<string>,
334-
keySpecifications: string
335+
keySpecifications: Array<string>
335336
];
336337

338+
337339
export type CommandReply = {
338340
name: string,
339341
arity: number,
@@ -342,13 +344,25 @@ export type CommandReply = {
342344
lastKeyIndex: number,
343345
step: number,
344346
categories: Set<CommandCategories>,
345-
keySpecifications: string
347+
policies: { request: RequestPolicyWithDefaults | undefined, response: ResponsePolicyWithDefaults | undefined }
348+
isKeyless: boolean
346349
};
347350

348351
export function transformCommandReply(
349352
this: void,
350-
[name, arity, flags, firstKeyIndex, lastKeyIndex, step, categories, _tips, keySpecifications]: CommandRawReply
353+
[name, arity, flags, firstKeyIndex, lastKeyIndex, step, categories, tips, keySpecifications]: CommandRawReply
351354
): CommandReply {
355+
356+
const requestPolicyRaw = tips[0]?.replace('request_policy:', '');
357+
const requestPolicy = requestPolicyRaw && Object.values(REQUEST_POLICIES_WITH_DEFAULTS).includes(requestPolicyRaw as RequestPolicyWithDefaults)
358+
? requestPolicyRaw as RequestPolicyWithDefaults
359+
: undefined;
360+
361+
const responsePolicyRaw = tips[1]?.replace('response_policy:', '');
362+
const responsePolicy = responsePolicyRaw && Object.values(RESPONSE_POLICIES_WITH_DEFAULTS).includes(responsePolicyRaw as ResponsePolicyWithDefaults)
363+
? responsePolicyRaw as ResponsePolicyWithDefaults
364+
: undefined;
365+
352366
return {
353367
name,
354368
arity,
@@ -357,7 +371,11 @@ export function transformCommandReply(
357371
lastKeyIndex,
358372
step,
359373
categories: new Set(categories),
360-
keySpecifications
374+
policies: {
375+
request: requestPolicy,
376+
response: responsePolicy
377+
},
378+
isKeyless: keySpecifications.length === 0
361379
};
362380
}
363381

0 commit comments

Comments
 (0)