Skip to content

Commit 2fd1585

Browse files
committed
simplify parser, only have a single parser.
1 parent c4328b4 commit 2fd1585

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

packages/client/lib/client/parser.ts

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@ import { RedisArgument, RespVersions } from "../..";
22
import { RedisVariadicArgument } from "../commands/generic-transformers";
33

44
export interface CommandParser {
5-
redisArgs: Array<RedisArgument>;
5+
redisArgs: ReadonlyArray<RedisArgument>;
6+
keys: ReadonlyArray<RedisArgument>;
7+
firstKey: RedisArgument | undefined;
68
respVersion: RespVersions;
79
preserve: unknown;
10+
cachable: boolean;
811

912
push: (arg: RedisArgument) => unknown;
1013
pushVariadic: (vals: RedisVariadicArgument) => unknown;
14+
pushVariadicNumber: (vals: number | Array<number>) => unknown;
1115
pushKey: (key: RedisArgument) => unknown; // normal push of keys
1216
pushKeys: (keys: RedisVariadicArgument) => unknown; // push multiple keys at a time
17+
pushKeysLength: (keys: RedisVariadicArgument) => unknown; // push multiple keys at a time
1318
setCachable: () => unknown;
1419
setPreserve: (val: unknown) => unknown;
1520
}
1621

17-
export abstract class AbstractCommandParser implements CommandParser {
22+
export class BasicCommandParser implements CommandParser {
1823
#redisArgs: Array<RedisArgument> = [];
24+
#keys: Array<RedisArgument> = [];
1925
#respVersion: RespVersions;
2026
#preserve: unknown;
27+
#cachable: boolean = false;
2128

2229
constructor(respVersion: RespVersions = 2) {
2330
this.#respVersion = respVersion;
@@ -27,6 +34,14 @@ export abstract class AbstractCommandParser implements CommandParser {
2734
return this.#redisArgs;
2835
}
2936

37+
get keys() {
38+
return this.#keys;
39+
}
40+
41+
get firstKey() {
42+
return this.#keys.length != 0 ? this.#keys[0] : undefined;
43+
}
44+
3045
get respVersion() {
3146
return this.#respVersion;
3247
}
@@ -35,9 +50,12 @@ export abstract class AbstractCommandParser implements CommandParser {
3550
return this.#preserve;
3651
}
3752

53+
get cachable() {
54+
return this.#cachable
55+
}
56+
3857
push(arg: RedisArgument) {
3958
this.#redisArgs.push(arg);
40-
4159
};
4260

4361
pushVariadic(vals: RedisVariadicArgument) {
@@ -50,14 +68,36 @@ export abstract class AbstractCommandParser implements CommandParser {
5068
}
5169
}
5270

71+
pushVariadicNumber(vals: number | number[]) {
72+
if (Array.isArray(vals)) {
73+
for (const val of vals) {
74+
this.push(val.toString());
75+
}
76+
} else {
77+
this.push(vals.toString());
78+
}
79+
}
80+
5381
pushKey(key: RedisArgument) {
82+
this.#keys.push(key);
5483
this.#redisArgs.push(key);
5584
};
5685

86+
pushKeysLength(keys: RedisVariadicArgument) {
87+
if (Array.isArray(keys)) {
88+
this.#redisArgs.push(keys.length.toString());
89+
} else {
90+
this.#redisArgs.push('1');
91+
}
92+
this.pushKeys(keys);
93+
}
94+
5795
pushKeys(keys: RedisVariadicArgument) {
5896
if (Array.isArray(keys)) {
97+
this.#keys.push(...keys);
5998
this.#redisArgs.push(...keys);
6099
} else {
100+
this.#keys.push(keys);
61101
this.#redisArgs.push(keys);
62102
}
63103
}
@@ -66,27 +106,7 @@ export abstract class AbstractCommandParser implements CommandParser {
66106
this.#preserve = val;
67107
}
68108

69-
setCachable() {};
70-
}
71-
72-
/* Note: I do it this way, where BasicCommandParser extends Abstract without any changes,
73-
and CachedCommandParser extends Abstract with changes, to enable them to be easily
74-
distinguishable at runtime. If Cached extended Basic, then Cached would also be a Basic,
75-
thereby making them harder to distinguish.
76-
*/
77-
export class BasicCommandParser extends AbstractCommandParser {};
78-
79-
export interface ClusterCommandParser extends CommandParser {
80-
firstKey: RedisArgument | undefined;
81-
}
82-
83-
export class BasicClusterCommandParser extends BasicCommandParser implements ClusterCommandParser {
84-
firstKey: RedisArgument | undefined;
85-
86-
override pushKey(key: RedisArgument): void {
87-
if (!this.firstKey) {
88-
this.firstKey = key;
89-
}
90-
super.pushKey(key);
91-
}
109+
setCachable() {
110+
this.#cachable = true;
111+
};
92112
}

packages/client/lib/cluster/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { PubSubListener } from '../client/pub-sub';
1010
import { ErrorReply } from '../errors';
1111
import { RedisTcpSocketOptions } from '../client/socket';
1212
import ASKING from '../commands/ASKING';
13-
import { BasicClusterCommandParser, ClusterCommandParser } from '../client/parser';
13+
import { BasicCommandParser, CommandParser } from '../client/parser';
14+
;
1415

1516
interface ClusterCommander<
1617
M extends RedisModules,
@@ -408,8 +409,8 @@ export default class RedisCluster<
408409
return this._self.#slots.isOpen;
409410
}
410411

411-
#newCommandParser(resp: RespVersions): ClusterCommandParser {
412-
return new BasicClusterCommandParser(resp);
412+
#newCommandParser(resp: RespVersions): CommandParser {
413+
return new BasicCommandParser(resp);
413414
}
414415

415416
constructor(options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>) {

0 commit comments

Comments
 (0)