Skip to content

Commit 59b77a0

Browse files
committed
wip - removing transformArguments
1 parent e374a08 commit 59b77a0

File tree

373 files changed

+448
-1303
lines changed

Some content is hidden

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

373 files changed

+448
-1303
lines changed

packages/client/lib/RESP/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CommandParser } from '../client/parser';
2+
import { ParametersOmitFirst } from '../commands/generic-transformers';
23
import { BlobError, SimpleError } from '../errors';
34
import { RedisScriptConfig, SHA1 } from '../lua-script';
45
import { RESP_TYPES } from './decoder';
@@ -273,8 +274,7 @@ export type Command = {
273274
*/
274275
IS_FORWARD_COMMAND?: boolean;
275276
// POLICIES?: CommandPolicies;
276-
parseCommand?(this: void, parser: CommandParser, ...args: Array<any>): void;
277-
transformArguments(this: void, ...args: Array<any>): CommandArguments;
277+
parseCommand(this: void, parser: CommandParser, ...args: Array<any>): void;
278278
TRANSFORM_LEGACY_REPLY?: boolean;
279279
transformReply: TransformReply | Record<RespVersions, TransformReply>;
280280
};
@@ -354,7 +354,7 @@ export type CommandSignature<
354354
COMMAND extends Command,
355355
RESP extends RespVersions,
356356
TYPE_MAPPING extends TypeMapping
357-
> = (...args: Parameters<COMMAND['transformArguments']>) => Promise<ReplyWithTypeMapping<CommandReply<COMMAND, RESP>, TYPE_MAPPING>>;
357+
> = (...args: ParametersOmitFirst<Parameters<COMMAND['parseCommand']>>) => Promise<ReplyWithTypeMapping<CommandReply<COMMAND, RESP>, TYPE_MAPPING>>;
358358

359359
// export type CommandWithPoliciesSignature<
360360
// COMMAND extends Command,

packages/client/lib/client/index.ts

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import HELLO, { HelloOptions } from '../commands/HELLO';
1414
import { ScanOptions, ScanCommonOptions } from '../commands/SCAN';
1515
import { RedisLegacyClient, RedisLegacyClientType } from './legacy-mode';
1616
import { RedisPoolOptions, RedisClientPool } from './pool';
17-
import { RedisVariadicArgument, pushVariadicArguments } from '../commands/generic-transformers';
17+
import { RedisVariadicArgument, parseArgs, pushVariadicArguments } from '../commands/generic-transformers';
1818
import { BasicCommandParser, CommandParser } from './parser';
1919

2020
export interface RedisClientOptions<
@@ -154,38 +154,22 @@ export default class RedisClient<
154154
const transformReply = getTransformReply(command, resp);
155155

156156
return async function (this: ProxyClient, ...args: Array<unknown>) {
157-
if (command.parseCommand) {
158-
const parser = new BasicCommandParser(resp);
159-
160-
command.parseCommand(parser, ...args);
161-
162-
return this.executeCommand(parser, this._commandOptions, transformReply);
163-
} else {
164-
const redisArgs = command.transformArguments(...args),
165-
reply = await this.sendCommand(redisArgs, this._commandOptions);
166-
return transformReply ?
167-
transformReply(reply, redisArgs.preserve) :
168-
reply;
169-
};
157+
const parser = new BasicCommandParser(resp);
158+
159+
command.parseCommand(parser, ...args);
160+
161+
return this.executeCommand(parser, this._commandOptions, transformReply);
170162
}
171163
}
172164

173165
static #createModuleCommand(command: Command, resp: RespVersions) {
174166
const transformReply = getTransformReply(command, resp);
175167

176168
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
177-
if (command.parseCommand) {
178-
const parser = new BasicCommandParser(resp);
179-
command.parseCommand(parser, ...args);
180-
181-
return this._self.executeCommand(parser, this._self._commandOptions, transformReply);
182-
} else {
183-
const redisArgs = command.transformArguments(...args),
184-
reply = await this._self.sendCommand(redisArgs, this._self._commandOptions);
185-
return transformReply ?
186-
transformReply(reply, redisArgs.preserve) :
187-
reply;
188-
}
169+
const parser = new BasicCommandParser(resp);
170+
command.parseCommand(parser, ...args);
171+
172+
return this._self.executeCommand(parser, this._self._commandOptions, transformReply);
189173
};
190174
}
191175

@@ -194,22 +178,11 @@ export default class RedisClient<
194178
const transformReply = getTransformReply(fn, resp);
195179

196180
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
197-
if (fn.parseCommand) {
198-
const parser = new BasicCommandParser(resp);
199-
parser.pushVariadic(prefix);
200-
fn.parseCommand(parser, ...args);
201-
202-
return this._self.executeCommand(parser, this._self._commandOptions, transformReply);
203-
} else {
204-
const fnArgs = fn.transformArguments(...args),
205-
reply = await this._self.sendCommand(
206-
prefix.concat(fnArgs),
207-
this._self._commandOptions
208-
);
209-
return transformReply ?
210-
transformReply(reply, fnArgs.preserve) :
211-
reply;
212-
}
181+
const parser = new BasicCommandParser(resp);
182+
parser.pushVariadic(prefix);
183+
fn.parseCommand(parser, ...args);
184+
185+
return this._self.executeCommand(parser, this._self._commandOptions, transformReply);
213186
};
214187
}
215188

@@ -218,20 +191,11 @@ export default class RedisClient<
218191
const transformReply = getTransformReply(script, resp);
219192

220193
return async function (this: ProxyClient, ...args: Array<unknown>) {
221-
if (script.parseCommand) {
222-
const parser = new BasicCommandParser(resp);
223-
parser.pushVariadic(prefix);
224-
script.parseCommand(parser, ...args);
225-
226-
return this.executeCommand(parser, this._commandOptions, transformReply);
227-
} else {
228-
const scriptArgs = script.transformArguments(...args),
229-
redisArgs = prefix.concat(scriptArgs),
230-
reply = await this.executeScript(script, redisArgs, this._commandOptions);
231-
return transformReply ?
232-
transformReply(reply, scriptArgs.preserve) :
233-
reply;
234-
};
194+
const parser = new BasicCommandParser(resp);
195+
parser.pushVariadic(prefix);
196+
script.parseCommand(parser, ...args);
197+
198+
return this.executeCommand(parser, this._commandOptions, transformReply);
235199
}
236200
}
237201

@@ -400,12 +364,12 @@ export default class RedisClient<
400364
}
401365

402366
commands.push(
403-
HELLO.transformArguments(this.#options.RESP, hello)
367+
parseArgs(HELLO, this.#options.RESP, hello)
404368
);
405369
} else {
406370
if (this.#options?.username || this.#options?.password) {
407371
commands.push(
408-
COMMANDS.AUTH.transformArguments({
372+
parseArgs(COMMANDS.AUTH, {
409373
username: this.#options.username,
410374
password: this.#options.password ?? ''
411375
})
@@ -414,7 +378,7 @@ export default class RedisClient<
414378

415379
if (this.#options?.name) {
416380
commands.push(
417-
COMMANDS.CLIENT_SETNAME.transformArguments(this.#options.name)
381+
parseArgs(COMMANDS.CLIENT_SETNAME, this.#options.name)
418382
);
419383
}
420384
}
@@ -425,7 +389,7 @@ export default class RedisClient<
425389

426390
if (this.#options?.readonly) {
427391
commands.push(
428-
COMMANDS.READONLY.transformArguments()
392+
parseArgs(COMMANDS.READONLY)
429393
);
430394
}
431395

packages/client/lib/client/multi-command.ts

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType, RedisMultiQ
33
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, TransformReply, RedisScript, RedisFunction, TypeMapping } from '../RESP/types';
44
import { attachConfig, functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
55
import { BasicCommandParser } from './parser';
6+
import { ParametersOmitFirst } from '../commands/generic-transformers';
67

78
type CommandSignature<
89
REPLIES extends Array<unknown>,
@@ -12,7 +13,7 @@ type CommandSignature<
1213
S extends RedisScripts,
1314
RESP extends RespVersions,
1415
TYPE_MAPPING extends TypeMapping
15-
> = (...args: Parameters<C['transformArguments']>) => RedisClientMultiCommandType<
16+
> = (...args: ParametersOmitFirst<Parameters<C['parseCommand']>>) => RedisClientMultiCommandType<
1617
[...REPLIES, ReplyWithTypeMapping<CommandReply<C, RESP>, TYPE_MAPPING>],
1718
M,
1819
F,
@@ -93,15 +94,11 @@ export default class RedisClientMultiCommand<REPLIES = []> {
9394
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
9495
let redisArgs: CommandArguments;
9596

96-
if (command.parseCommand) {
97-
const parser = new BasicCommandParser(resp);
98-
command.parseCommand(parser, ...args);
97+
const parser = new BasicCommandParser(resp);
98+
command.parseCommand(parser, ...args);
9999

100-
redisArgs = parser.redisArgs;
101-
redisArgs.preserve = parser.preserve;
102-
} else {
103-
redisArgs = command.transformArguments(...args);
104-
}
100+
redisArgs = parser.redisArgs;
101+
redisArgs.preserve = parser.preserve;
105102

106103
return this.addCommand(
107104
redisArgs,
@@ -116,15 +113,11 @@ export default class RedisClientMultiCommand<REPLIES = []> {
116113
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
117114
let redisArgs: CommandArguments;
118115

119-
if (command.parseCommand) {
120-
const parser = new BasicCommandParser(resp);
121-
command.parseCommand(parser, ...args);
116+
const parser = new BasicCommandParser(resp);
117+
command.parseCommand(parser, ...args);
122118

123-
redisArgs = parser.redisArgs;
124-
redisArgs.preserve = parser.preserve;
125-
} else {
126-
redisArgs = command.transformArguments(...args);
127-
}
119+
redisArgs = parser.redisArgs;
120+
redisArgs.preserve = parser.preserve;
128121

129122
return this._self.addCommand(
130123
redisArgs,
@@ -140,16 +133,12 @@ export default class RedisClientMultiCommand<REPLIES = []> {
140133
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
141134
let fnArgs: CommandArguments;
142135

143-
if (fn.parseCommand) {
144-
const parser = new BasicCommandParser(resp);
145-
parser.pushVariadic(prefix);
146-
fn.parseCommand(parser, ...args);
136+
const parser = new BasicCommandParser(resp);
137+
parser.pushVariadic(prefix);
138+
fn.parseCommand(parser, ...args);
147139

148-
fnArgs = parser.redisArgs;
149-
fnArgs.preserve = parser.preserve;
150-
} else {
151-
fnArgs = fn.transformArguments(...args);
152-
}
140+
fnArgs = parser.redisArgs;
141+
fnArgs.preserve = parser.preserve;
153142

154143
const redisArgs: CommandArguments = prefix.concat(fnArgs);
155144
redisArgs.preserve = fnArgs.preserve;
@@ -168,17 +157,12 @@ export default class RedisClientMultiCommand<REPLIES = []> {
168157
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
169158
let redisArgs: CommandArguments;
170159

171-
if (script.parseCommand) {
172-
const parser = new BasicCommandParser(resp);
173-
parser.pushVariadic(prefix);
174-
script.parseCommand(parser, ...args);
175-
176-
redisArgs = parser.redisArgs;
177-
redisArgs.preserve = parser.preserve;
178-
} else {
179-
redisArgs = prefix;
180-
redisArgs.push(...script.transformArguments(...args));
181-
}
160+
const parser = new BasicCommandParser(resp);
161+
parser.pushVariadic(prefix);
162+
script.parseCommand(parser, ...args);
163+
164+
redisArgs = parser.redisArgs;
165+
redisArgs.preserve = parser.preserve;
182166

183167
return this.addCommand(
184168
redisArgs,

packages/client/lib/client/pool.ts

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,21 @@ export class RedisClientPool<
6363
const transformReply = getTransformReply(command, resp);
6464

6565
return async function (this: ProxyPool, ...args: Array<unknown>) {
66-
if (command.parseCommand) {
67-
const parser = new BasicCommandParser(resp);
68-
command.parseCommand(parser, ...args);
69-
70-
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply))
71-
} else {
72-
const redisArgs = command.transformArguments(...args),
73-
reply = await this.sendCommand(redisArgs, this._commandOptions);
74-
return transformReply ?
75-
transformReply(reply, redisArgs.preserve) :
76-
reply;
77-
}
66+
const parser = new BasicCommandParser(resp);
67+
command.parseCommand(parser, ...args);
68+
69+
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply))
7870
};
7971
}
8072

8173
static #createModuleCommand(command: Command, resp: RespVersions) {
8274
const transformReply = getTransformReply(command, resp);
8375

8476
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
85-
if (command.parseCommand) {
86-
const parser = new BasicCommandParser(resp);
87-
command.parseCommand(parser, ...args);
88-
89-
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply))
90-
} else {
91-
const redisArgs = command.transformArguments(...args),
92-
reply = await this._self.sendCommand(redisArgs, this._self._commandOptions);
93-
return transformReply ?
94-
transformReply(reply, redisArgs.preserve) :
95-
reply;
96-
}
77+
const parser = new BasicCommandParser(resp);
78+
command.parseCommand(parser, ...args);
79+
80+
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply))
9781
};
9882
}
9983

@@ -102,22 +86,11 @@ export class RedisClientPool<
10286
const transformReply = getTransformReply(fn, resp);
10387

10488
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
105-
if (fn.parseCommand) {
106-
const parser = new BasicCommandParser(resp);
107-
parser.pushVariadic(prefix);
108-
fn.parseCommand(parser, ...args);
109-
110-
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply))
111-
} else {
112-
const fnArgs = fn.transformArguments(...args),
113-
reply = await this._self.sendCommand(
114-
prefix.concat(fnArgs),
115-
this._self._commandOptions
116-
);
117-
return transformReply ?
118-
transformReply(reply, fnArgs.preserve) :
119-
reply;
120-
}
89+
const parser = new BasicCommandParser(resp);
90+
parser.pushVariadic(prefix);
91+
fn.parseCommand(parser, ...args);
92+
93+
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply))
12194
};
12295
}
12396

@@ -126,20 +99,11 @@ export class RedisClientPool<
12699
const transformReply = getTransformReply(script, resp);
127100

128101
return async function (this: ProxyPool, ...args: Array<unknown>) {
129-
if (script.parseCommand) {
130-
const parser = new BasicCommandParser(resp);
131-
parser.pushVariadic(prefix);
132-
script.parseCommand(parser, ...args);
133-
134-
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply))
135-
} else {
136-
const scriptArgs = script.transformArguments(...args),
137-
redisArgs = prefix.concat(scriptArgs),
138-
reply = await this.executeScript(script, redisArgs, this._commandOptions);
139-
return transformReply ?
140-
transformReply(reply, scriptArgs.preserve) :
141-
reply;
142-
}
102+
const parser = new BasicCommandParser(resp);
103+
parser.pushVariadic(prefix);
104+
script.parseCommand(parser, ...args);
105+
106+
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply))
143107
};
144108
}
145109

0 commit comments

Comments
 (0)