Skip to content

Commit f0af1eb

Browse files
committed
redo time series commands to work with resp3 correctly
1 parent 4f74a86 commit f0af1eb

File tree

7 files changed

+174
-41
lines changed

7 files changed

+174
-41
lines changed

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommandArguments, Command, BlobStringReply, ArrayReply, UnwrapReply, Resp2Reply } from '@redis/client/dist/lib/RESP/types';
1+
import { CommandArguments, Command, BlobStringReply, ArrayReply, UnwrapReply, Resp2Reply, MapReply, TuplesReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
33
import { RawLabels, SampleRawReply, transformSampleReply } from '.';
44

@@ -25,19 +25,15 @@ export type MGetRawReply2 = ArrayReply<[
2525
sample: Resp2Reply<SampleRawReply>
2626
]>;
2727

28-
export type MGetRawReply3 = ArrayReply<[
29-
key: BlobStringReply,
30-
labels: RawLabels,
31-
sample: SampleRawReply
32-
]>;
28+
export type MGetRawReply3 = MapReply<BlobStringReply, TuplesReply<[labels: RawLabels, sample: SampleRawReply]>>;
3329

3430
export interface MGetReply2 {
35-
key: BlobStringReply;
31+
key: string | BlobStringReply;
3632
sample: ReturnType<typeof transformSampleReply[2]>;
3733
}
3834

3935
export interface MGetReply3 {
40-
key: BlobStringReply;
36+
key: string;
4137
sample: ReturnType<typeof transformSampleReply[3]>;
4238
}
4339

@@ -51,15 +47,37 @@ export default {
5147
transformReply: {
5248
2(reply: UnwrapReply<MGetRawReply2>): Array<MGetReply2> {
5349
return reply.map(([key, _, sample]) => ({
54-
key,
50+
key: key.toString(),
5551
sample: transformSampleReply[2](sample)
5652
}));
5753
},
58-
3(reply: UnwrapReply<MGetRawReply3>): Array<MGetReply3> {
59-
return reply.map(([key, _, sample]) => ({
60-
key,
61-
sample: transformSampleReply[3](sample)
62-
}));
54+
3(reply: UnwrapReply<MapReply<any, any>>): Array<MGetReply3> {
55+
const args: Array<MGetReply3> = [];
56+
57+
if (reply instanceof Array) {
58+
for (const [key, value] of reply) {
59+
args.push({
60+
key,
61+
sample: transformSampleReply[3](value[1])
62+
});
63+
}
64+
} else if (reply instanceof Map) {
65+
for (const [key, value] of reply) {
66+
args.push({
67+
key,
68+
sample: transformSampleReply[3](value[1])
69+
});
70+
}
71+
} else {
72+
for (const [key, value] of Object.entries(reply)) {
73+
args.push({
74+
key,
75+
sample: transformSampleReply[3](value[1])
76+
});
77+
}
78+
}
79+
80+
return args;
6381
}
6482
}
6583
} as const satisfies Command;

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Command, ReplyUnion, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
1+
import { Command, MapReply, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { TsMGetOptions, pushLatestArgument, pushFilterArgument, MGetReply2, MGetRawReply2 } from './MGET';
4-
import { Labels, pushWithLabelsArgument, transformLablesReply, transformSampleReply } from '.';
3+
import { TsMGetOptions, pushLatestArgument, pushFilterArgument, MGetReply2, MGetRawReply2, MGetReply3 } from './MGET';
4+
import { Labels, pushWithLabelsArgument, transformLablesReply2, transformLablesReply3, transformSampleReply } from '.';
55

66
export interface TsMGetWithLabelsOptions extends TsMGetOptions {
77
SELECTED_LABELS?: RedisVariadicArgument;
@@ -11,6 +11,10 @@ export interface MGetWithLabelsReply2 extends MGetReply2 {
1111
labels: Labels;
1212
};
1313

14+
export interface MGetWithLabelsReply3 extends MGetReply3 {
15+
labels: Labels;
16+
};
17+
1418
export default {
1519
FIRST_KEY_INDEX: undefined,
1620
IS_READ_ONLY: true,
@@ -23,11 +27,40 @@ export default {
2327
2: (reply: UnwrapReply<MGetRawReply2>): Array<MGetWithLabelsReply2> => {
2428
return reply.map(([key, labels, sample]) => ({
2529
key,
26-
labels: transformLablesReply(labels),
30+
labels: transformLablesReply2(labels),
2731
sample: transformSampleReply[2](sample)
2832
}));
2933
},
30-
3: undefined as unknown as () => ReplyUnion
34+
3: (reply: UnwrapReply<MapReply<any, any>>): Array<MGetWithLabelsReply3> => {
35+
const args: Array<MGetWithLabelsReply3> = [];
36+
37+
if (reply instanceof Array) {
38+
for (const [key, value] of reply) {
39+
args.push({
40+
key,
41+
labels: transformLablesReply3(value[0]),
42+
sample: transformSampleReply[3](value[1])
43+
});
44+
}
45+
} else if (reply instanceof Map) {
46+
for (const [key, value] of reply) {
47+
args.push({
48+
key,
49+
labels: transformLablesReply3(value[0]),
50+
sample: transformSampleReply[3](value[1])
51+
});
52+
}
53+
} else {
54+
for (const [key, value] of Object.entries(reply)) {
55+
args.push({
56+
key,
57+
labels: transformLablesReply3(value[0]),
58+
sample: transformSampleReply[3](value[1])
59+
});
60+
}
61+
}
62+
63+
return args;
64+
},
3165
},
32-
unstableResp3Module: true
3366
} as const satisfies Command;

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RedisArgument, Command, CommandArguments, ReplyUnion, UnwrapReply, ArrayReply, BlobStringReply, Resp2Reply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, Command, CommandArguments, UnwrapReply, ArrayReply, BlobStringReply, Resp2Reply, MapReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
33
import { RawLabels, SampleRawReply, Timestamp, transformSampleReply, transformSamplesReply } from '.';
44
import { TsRangeOptions, pushRangeArguments } from './RANGE';
@@ -69,24 +69,55 @@ export interface MRangeReplyItem2 {
6969
samples: Array<ReturnType<typeof transformSampleReply[2]>>;
7070
}
7171

72+
export interface MRangeReplyItem3 {
73+
key: BlobStringReply;
74+
samples: Array<ReturnType<typeof transformSampleReply[3]>>;
75+
}
76+
7277
export default {
7378
FIRST_KEY_INDEX: undefined,
7479
IS_READ_ONLY: true,
7580
transformArguments: transformMRangeArguments.bind(undefined, 'TS.MRANGE'),
7681
transformReply: {
7782
2: (reply: UnwrapReply<MRangeRawReply2>): Array<MRangeReplyItem2> => {
7883
const args = [];
79-
84+
8085
for (const [key, _, samples] of reply) {
8186
args.push({
8287
key,
8388
samples: transformSamplesReply[2](samples)
8489
});
8590
}
86-
91+
8792
return args;
8893
},
89-
3: undefined as unknown as () => ReplyUnion
94+
3: (reply: UnwrapReply<MapReply<any, any>>): Array<MRangeReplyItem3> => {
95+
const args = [];
96+
97+
if (reply instanceof Array) {
98+
for (const [key, _, samples] of reply) {
99+
args.push({
100+
key,
101+
samples: transformSamplesReply[3](samples)
102+
});
103+
}
104+
} else if (reply instanceof Map) {
105+
for (const [key, value] of reply) {
106+
args.push({
107+
key,
108+
samples: transformSamplesReply[3](value[2])
109+
})
110+
}
111+
} else {
112+
for (const [key, value] of Object.entries(reply)) {
113+
args.push({
114+
key,
115+
samples: transformSamplesReply[3](value[2])
116+
})
117+
}
118+
}
119+
120+
return args;
121+
}
90122
},
91-
unstableResp3Module: true
92123
} as const satisfies Command;

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

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

@@ -26,25 +26,58 @@ export interface MRangeWithLabelsReplyItem2 extends MRangeReplyItem2 {
2626
labels: Labels;
2727
}
2828

29+
export interface MRangeWithLabelsReplyItem3 extends MRangeReplyItem3 {
30+
labels: Labels;
31+
}
32+
2933
export default {
3034
FIRST_KEY_INDEX: undefined,
3135
IS_READ_ONLY: true,
3236
transformArguments: transformMRangeWithLabelsArguments.bind(undefined, 'TS.MRANGE'),
3337
transformReply: {
34-
2(reply: UnwrapReply<MRangeRawReply2>): Array<MRangeWithLabelsReplyItem2> {
38+
2: (reply: UnwrapReply<MRangeRawReply2>): Array<MRangeWithLabelsReplyItem2> => {
3539
const args = [];
36-
37-
for (const [key, labels, samples] of reply) {
40+
41+
for (const [key, labels, samples] of reply) {
3842
args.push({
3943
key,
40-
labels: transformLablesReply(labels),
44+
labels: transformLablesReply2(labels),
4145
samples: transformSamplesReply[2](samples)
4246
});
4347
}
44-
48+
4549
return args;
4650
},
47-
3: undefined as unknown as () => ReplyUnion
51+
3: (reply: UnwrapReply<MapReply<any, any>>): Array<MRangeReplyItem3> => {
52+
const args = [];
53+
54+
if (reply instanceof Array) {
55+
for (const [key, labels, samples] of reply) {
56+
args.push({
57+
key,
58+
labels: transformLablesReply3(labels),
59+
samples: transformSamplesReply[3](samples)
60+
});
61+
}
62+
} else if (reply instanceof Map) {
63+
for (const [key, value] of reply) {
64+
args.push({
65+
key,
66+
labels: transformLablesReply3(value[0]),
67+
samples: transformSamplesReply[3](value[2])
68+
})
69+
}
70+
} else {
71+
for (const [key, value] of Object.entries(reply)) {
72+
args.push({
73+
key,
74+
labels: transformLablesReply3(value[0]),
75+
samples: transformSamplesReply[3](value[2])
76+
})
77+
}
78+
}
79+
80+
return args;
81+
}
4882
},
49-
unstableResp3Module: true
5083
} as const satisfies Command;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ export default {
66
IS_READ_ONLY: MRANGE.IS_READ_ONLY,
77
transformArguments: transformMRangeArguments.bind(undefined, 'TS.MREVRANGE'),
88
transformReply: MRANGE.transformReply,
9-
unstableResp3Module: MRANGE.unstableResp3Module
109
} as const satisfies Command;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ export default {
66
IS_READ_ONLY: MRANGE_WITHLABELS.IS_READ_ONLY,
77
transformArguments: transformMRangeWithLabelsArguments.bind(undefined, 'TS.MREVRANGE'),
88
transformReply: MRANGE_WITHLABELS.transformReply,
9-
unstableResp3Module: MRANGE_WITHLABELS.unstableResp3Module
109
} as const satisfies Command;

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CommandArguments, DoubleReply, NumberReply, RedisArgument, RedisCommands, TuplesReply, UnwrapReply, Resp2Reply, ArrayReply } from '@redis/client/dist/lib/RESP/types';
1+
import type { CommandArguments, DoubleReply, NumberReply, RedisArgument, RedisCommands, TuplesReply, UnwrapReply, Resp2Reply, ArrayReply, BlobStringReply, MapReply } from '@redis/client/dist/lib/RESP/types';
22
import ADD from './ADD';
33
import ALTER from './ALTER';
44
import CREATE from './CREATE';
@@ -121,7 +121,7 @@ export function transformTimestampArgument(timestamp: Timestamp): string {
121121
).toString();
122122
}
123123

124-
export type RawLabels = Array<[label: string, value: string]>;
124+
export type RawLabels = Array<[label: BlobStringReply, value: BlobStringReply]>;
125125

126126
export type Labels = {
127127
[label: string]: string;
@@ -170,11 +170,31 @@ export const transformSamplesReply = {
170170
.map(sample => transformSampleReply[3](sample)); }
171171
};
172172

173-
export function transformLablesReply(reply: RawLabels): Labels {
173+
export function transformLablesReply2(reply: RawLabels): Labels {
174174
const labels: Labels = {};
175175

176-
for (const [key, value] of reply) {
176+
for (const [k, v] of reply) {
177+
const key = k as unknown as UnwrapReply<BlobStringReply>;
178+
const value = v as unknown as UnwrapReply<BlobStringReply>;
179+
labels[key.toString()] = value.toString()
180+
}
181+
182+
return labels
183+
}
184+
185+
export function transformLablesReply3(reply: UnwrapReply<MapReply<any, any>>): Labels {
186+
const labels: Labels = {};
187+
188+
if (reply instanceof Map) {
189+
for (const [key, value] of reply) {
190+
labels[key.toString()] = value.toString();
191+
}
192+
} else if (reply instanceof Array) {
193+
return transformLablesReply2(reply);
194+
} else {
195+
for (const [key, value] of Object.entries(reply)) {
177196
labels[key] = value;
197+
}
178198
}
179199

180200
return labels

0 commit comments

Comments
 (0)