Skip to content

Commit d896b09

Browse files
committed
attempt to implement leibele's suggestion to clean up code
1 parent 6c34d6b commit d896b09

File tree

6 files changed

+49
-47
lines changed

6 files changed

+49
-47
lines changed

packages/time-series/lib/commands/MGET.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CommandArguments, Command, BlobStringReply, ArrayReply, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { RawLabels, SampleRawReply2, SampleRawReply3, SampleReply2, SampleReply3, transformSampleReply } from '.';
3+
import { RawLabels, SampleRawReply, transformSampleReply } from '.';
4+
import { Resp2Reply } from '@redis/client/dist/lib/RESP/types';
45

56
export interface TsMGetOptions {
67
LATEST?: boolean;
@@ -22,23 +23,23 @@ export function pushFilterArgument(args: CommandArguments, filter: RedisVariadic
2223
export type MGetRawReply2 = ArrayReply<[
2324
key: BlobStringReply,
2425
labels: RawLabels,
25-
sample: SampleRawReply2
26+
sample: Resp2Reply<SampleRawReply>
2627
]>;
2728

2829
export type MGetRawReply3 = ArrayReply<[
2930
key: BlobStringReply,
3031
labels: RawLabels,
31-
sample: SampleRawReply3
32+
sample: SampleRawReply
3233
]>;
3334

3435
export interface MGetReply2 {
3536
key: BlobStringReply;
36-
sample: SampleReply2;
37+
sample: ReturnType<typeof transformSampleReply[2]>;
3738
}
3839

3940
export interface MGetReply3 {
4041
key: BlobStringReply;
41-
sample: SampleReply3;
42+
sample: ReturnType<typeof transformSampleReply[3]>;
4243
}
4344

4445
export default {
@@ -52,13 +53,13 @@ export default {
5253
2(reply: UnwrapReply<MGetRawReply2>): Array<MGetReply2> {
5354
return reply.map(([key, _, sample]) => ({
5455
key,
55-
sample: transformSampleReply[2](sample as unknown as UnwrapReply<SampleRawReply2>)
56+
sample: transformSampleReply[2](sample as unknown as Resp2Reply<SampleRawReply>)
5657
}));
5758
},
5859
3(reply: UnwrapReply<MGetRawReply3>): Array<MGetReply3> {
5960
return reply.map(([key, _, sample]) => ({
6061
key,
61-
sample: transformSampleReply[3](sample as unknown as UnwrapReply<SampleRawReply3>)
62+
sample: transformSampleReply[3](sample as unknown as SampleRawReply)
6263
}));
6364
}
6465
}

packages/time-series/lib/commands/MGET_WITHLABELS.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Command, ReplyUnion, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
33
import { TsMGetOptions, pushLatestArgument, pushFilterArgument, MGetReply2, MGetRawReply2 } from './MGET';
4-
import { Labels, SampleRawReply2, pushWithLabelsArgument, transformLablesReply, transformSampleReply } from '.';
4+
import { Labels, SampleRawReply, pushWithLabelsArgument, transformLablesReply, transformSampleReply } from '.';
5+
import { Resp2Reply } from '@redis/client/dist/lib/RESP/types';
56

67
export interface TsMGetWithLabelsOptions extends TsMGetOptions {
78
SELECTED_LABELS?: RedisVariadicArgument;
@@ -24,7 +25,7 @@ export default {
2425
return reply.map(([key, labels, sample]) => ({
2526
key,
2627
labels: transformLablesReply(labels),
27-
sample: transformSampleReply[2](sample as unknown as UnwrapReply<SampleRawReply2>)
28+
sample: transformSampleReply[2](sample as unknown as Resp2Reply<SampleRawReply>)
2829
}));
2930
},
3031
3: undefined as unknown as () => ReplyUnion

packages/time-series/lib/commands/MRANGE.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { RedisArgument, Command, CommandArguments, ReplyUnion, UnwrapReply, ArrayReply, BlobStringReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { RawLabels, SampleRawReply2, SampleReply2, Timestamp, transformSampleReply } from '.';
3+
import { RawLabels, SampleRawReply, SamplesRawReply, Timestamp, transformSampleReply, transformSamplesReply } from '.';
44
import { TsRangeOptions, pushRangeArguments } from './RANGE';
55
import { pushFilterArgument } from './MGET';
6+
import { Resp2Reply } from '@redis/client/dist/lib/RESP/types';
67

78
export const TIME_SERIES_REDUCERS = {
89
AVG: 'AVG',
@@ -61,12 +62,12 @@ export function transformMRangeArguments(
6162
export type MRangeRawReply2 = ArrayReply<[
6263
key: BlobStringReply,
6364
labels: RawLabels,
64-
samples: ArrayReply<SampleRawReply2>
65+
samples: ArrayReply<Resp2Reply<SampleRawReply>>
6566
]>;
6667

6768
export interface MRangeReplyItem2 {
6869
key: BlobStringReply;
69-
samples: Array<SampleReply2>;
70+
samples: Array<ReturnType<typeof transformSampleReply[2]>>;
7071
}
7172

7273
export default {
@@ -78,11 +79,9 @@ export default {
7879
const args = [];
7980

8081
for (const [key, _, samples] of reply) {
81-
const uSamples = samples as unknown as UnwrapReply<ArrayReply<SampleRawReply2>>;
82-
8382
args.push({
8483
key,
85-
samples: uSamples.map(sample => transformSampleReply['2'](sample as unknown as UnwrapReply<SampleRawReply2>))
84+
samples: transformSamplesReply[2](samples as unknown as Resp2Reply<SamplesRawReply>)
8685
});
8786
}
8887

packages/time-series/lib/commands/MRANGE_WITHLABELS.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RedisArgument, Command, ReplyUnion, UnwrapReply, ArrayReply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, Command, ReplyUnion, UnwrapReply, Resp2Reply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
33
import { MRangeRawReply2, MRangeReplyItem2, TsMRangeOptions, pushGroupByArgument } from './MRANGE';
4-
import { Labels, SampleRawReply2, Timestamp, pushWithLabelsArgument, transformLablesReply, transformSampleReply } from '.';
4+
import { Labels, SamplesRawReply, Timestamp, pushWithLabelsArgument, transformLablesReply, transformSamplesReply } from '.';
55
import { pushFilterArgument } from './MGET';
66
import { pushRangeArguments } from './RANGE';
77

@@ -34,13 +34,11 @@ export default {
3434
2(reply: UnwrapReply<MRangeRawReply2>): Array<MRangeWithLabelsReplyItem2> {
3535
const args = [];
3636

37-
for (const [key, labels, samples] of reply) {
38-
const uSamples = samples as unknown as UnwrapReply<ArrayReply<SampleRawReply2>>;
39-
37+
for (const [key, labels, samples] of reply) {
4038
args.push({
4139
key,
4240
labels: transformLablesReply(labels),
43-
samples: uSamples.map(sample => transformSampleReply['2'](sample as unknown as UnwrapReply<SampleRawReply2>))
41+
samples: transformSamplesReply[2](samples as unknown as Resp2Reply<SamplesRawReply>)
4442
});
4543
}
4644

packages/time-series/lib/commands/RANGE.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { CommandArguments, RedisArgument, ArrayReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types';
2-
import { Timestamp, transformTimestampArgument, transformSampleReply, SampleRawReply2, SampleRawReply3 } from '.';
1+
import { CommandArguments, RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
2+
import { Timestamp, transformTimestampArgument, SamplesRawReply, transformSamplesReply } from '.';
33
import { TimeSeriesAggregationType } from './CREATERULE';
4+
import { Resp2Reply } from '@redis/client/dist/lib/RESP/types';
45

56
export const TIME_SERIES_BUCKET_TIMESTAMP = {
67
LOW: '-',
@@ -108,11 +109,11 @@ export default {
108109
IS_READ_ONLY: true,
109110
transformArguments: transformRangeArguments.bind(undefined, 'TS.RANGE'),
110111
transformReply: {
111-
2(reply: UnwrapReply<ArrayReply<SampleRawReply2>>) {
112-
return reply.map(sample => transformSampleReply['2'](sample as unknown as UnwrapReply<SampleRawReply2>));
112+
2(reply: SamplesRawReply) {
113+
return transformSamplesReply[2](reply as unknown as Resp2Reply<SamplesRawReply>);
113114
},
114-
3(reply: UnwrapReply<ArrayReply<SampleRawReply3>>) {
115-
return reply.map(sample => transformSampleReply['3'](sample as unknown as UnwrapReply<SampleRawReply3>));
115+
3(reply: SamplesRawReply) {
116+
return transformSamplesReply[3](reply);
116117
}
117118
}
118119
} as const satisfies Command;

packages/time-series/lib/commands/index.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BlobStringReply, CommandArguments, DoubleReply, NumberReply, RedisArgument, RedisCommands, TuplesReply, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
1+
import type { CommandArguments, DoubleReply, NumberReply, RedisArgument, RedisCommands, TuplesReply, UnwrapReply, Resp2Reply, ArrayReply } from '@redis/client/dist/lib/RESP/types';
22
import ADD from './ADD';
33
import ALTER from './ALTER';
44
import CREATE from './CREATE';
@@ -139,35 +139,37 @@ export function pushLabelsArgument(args: Array<RedisArgument>, labels?: Labels)
139139
return args;
140140
}
141141

142-
export type SampleRawReply2 = TuplesReply<[timestamp: NumberReply, value: BlobStringReply]>;
143-
144-
export type SampleRawReply3 = TuplesReply<[timestamp: NumberReply, value: DoubleReply]>;
145-
146-
export interface SampleReply2 {
147-
timestamp: NumberReply;
148-
value: number;
149-
}
150-
151-
export interface SampleReply3 {
152-
timestamp: NumberReply;
153-
value: DoubleReply;
154-
}
142+
export type SampleRawReply = TuplesReply<[timestamp: NumberReply, value: DoubleReply]>;
155143

156144
export const transformSampleReply = {
157-
2(reply: UnwrapReply<SampleRawReply2>): SampleReply2 {
145+
2(reply: Resp2Reply<SampleRawReply>) {
146+
const [ timestamp, value ] = reply as unknown as UnwrapReply<typeof reply>;
158147
return {
159-
timestamp: reply[0],
160-
value: Number(reply[1])
148+
timestamp,
149+
value: Number(value)
161150
};
162151
},
163-
3(reply: UnwrapReply<SampleRawReply3>): SampleReply3 {
152+
3(reply: SampleRawReply) {
153+
const [ timestamp, value ] = reply as unknown as UnwrapReply<typeof reply>;
164154
return {
165-
timestamp: reply[0],
166-
value: reply[1]
155+
timestamp,
156+
value
167157
};
168158
}
169159
};
170160

161+
export type SamplesRawReply = ArrayReply<SampleRawReply>;
162+
163+
export const transformSamplesReply = {
164+
2(reply: Resp2Reply<SamplesRawReply>) {
165+
return (reply as unknown as UnwrapReply<typeof reply>)
166+
.map(sample => transformSampleReply[2](sample));
167+
},
168+
3(reply: SamplesRawReply) {
169+
return (reply as unknown as UnwrapReply<typeof reply>)
170+
.map(sample => transformSampleReply[3](sample)); }
171+
};
172+
171173
export function transformLablesReply(reply: RawLabels): Labels {
172174
const labels: Labels = {};
173175

0 commit comments

Comments
 (0)