Skip to content

Commit

Permalink
transformTuplesReply can be a BlobStringReply or SimpleStringReply
Browse files Browse the repository at this point in the history
  • Loading branch information
sjpotter committed Oct 9, 2024
1 parent bf11c35 commit 1a5ac15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions packages/client/lib/commands/generic-transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,42 +103,46 @@ export const transformNullableDoubleReply = {
3: undefined as unknown as () => DoubleReply | NullReply
};

export function createTransformTuplesReplyFunc(preserve?: any, typeMapping?: TypeMapping) {
return (reply: ArrayReply<BlobStringReply>) => {
return transformTuplesReply(reply, preserve, typeMapping);
export interface Stringable {
toString(): string;
}

export function createTransformTuplesReplyFunc<T extends Stringable>(preserve?: any, typeMapping?: TypeMapping) {
return (reply: ArrayReply<T>) => {
return transformTuplesReply<T>(reply, preserve, typeMapping);
};
}

export function transformTuplesReply(
reply: ArrayReply<BlobStringReply>,
export function transformTuplesReply<T extends Stringable>(
reply: ArrayReply<T>,
preserve?: any,
typeMapping?: TypeMapping
): MapReply<BlobStringReply, BlobStringReply> {
): MapReply<T , T> {
const mapType = typeMapping ? typeMapping[RESP_TYPES.MAP] : undefined;

const inferred = reply as unknown as UnwrapReply<typeof reply>

switch (mapType) {
case Array: {
return reply as unknown as MapReply<BlobStringReply, BlobStringReply>;
return reply as unknown as MapReply<T, T>;
}
case Map: {
const ret = new Map<string, BlobStringReply>;

for (let i = 0; i < inferred.length; i += 2) {
ret.set(inferred[i].toString(), inferred[i + 1]);
ret.set(inferred[i].toString(), inferred[i + 1] as any);
}

return ret as unknown as MapReply<BlobStringReply, BlobStringReply>;;
return ret as unknown as MapReply<T, T>;;
}
default: {
const ret: Record<string, BlobStringReply> = Object.create(null);

for (let i = 0; i < inferred.length; i += 2) {
ret[inferred[i].toString()] = inferred[i + 1];
ret[inferred[i].toString()] = inferred[i + 1] as any;
}

return ret as unknown as MapReply<BlobStringReply, BlobStringReply>;;
return ret as unknown as MapReply<T, T>;;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/sentinel/commands/SENTINEL_MASTER.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
return ['SENTINEL', 'MASTER', dbname];
},
transformReply: {
2: transformTuplesReply,
2: transformTuplesReply<BlobStringReply>,
3: undefined as unknown as () => MapReply<BlobStringReply, BlobStringReply>
}
} as const satisfies Command;

0 comments on commit 1a5ac15

Please sign in to comment.