Skip to content

Commit

Permalink
Fix processing cluster response.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Sep 5, 2024
1 parent b8bf37b commit 77e5794
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 127 deletions.
75 changes: 47 additions & 28 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,63 @@ function convertGlideRecordForZSet(

/**
* @internal
* Downcast `GlideRecord` to `Record`. Use if you are 146% aware that `data` keys are always strings.
* Recursevly downcast `GlideRecord` to `Record`. Use if you are 146% aware that `data` keys are always strings.
*/
export function glideRecordToRecord<T>(
data: GlideRecord<T>,
): Record<string, T> {
const res: Record<string, T> = {};

for (const pair of data) {
res[pair.key as string] = pair.value;
let newVal = pair.value;

if (isGlideRecord(pair.value)) {
newVal = glideRecordToRecord(
pair.value as GlideRecord<unknown>,
) as T;
} else if (isGlideRecordArray(pair.value)) {
newVal = (pair.value as GlideRecord<unknown>[]).map(
glideRecordToRecord,
) as T;
}

res[pair.key as string] = newVal;
}

return res;
}

/**
* @internal
* Check whether an object is of type `GlideRecord`.
*/
function isGlideRecord(obj?: unknown): boolean {
return (
obj !== undefined &&
obj !== null &&
Array.isArray(obj) &&
obj.length > 0 &&
typeof obj[0] === "object" &&
!Array.isArray(obj[0]) &&
"key" in obj[0] &&
"value" in obj[0]
);
}

/**
* @internal
* Check whether an object is of type `GlideRecord[]`.
*/
function isGlideRecordArray(obj?: unknown): boolean {
return (
obj !== undefined &&
obj !== null &&
Array.isArray(obj) &&
obj.length > 0 &&
isGlideRecord(obj[0])
);
}

/** Represents the return type of {@link xinfoStream} command. */
export type ReturnTypeXinfoStream = Record<
string,
Expand Down Expand Up @@ -5665,32 +5708,8 @@ export class BaseClient {
| GlideRecord<StreamEntries | GlideRecord<StreamEntries>[]>[]
>
>(createXInfoStream(key, options?.fullOptions ?? false), options).then(
(xinfoStream) => {
const converted = glideRecordToRecord(xinfoStream);

if (options?.fullOptions) {
const groups = (
converted["groups"] as GlideRecord<
| GlideRecord<StreamEntries>[]
| Record<string, StreamEntries>[]
>[]
).map(glideRecordToRecord);

for (const group of groups) {
group.consumers = (
group.consumers as GlideRecord<StreamEntries>[]
).map(glideRecordToRecord);
}

(converted as ReturnTypeXinfoStream).groups =
groups as Record<
string,
Record<string, StreamEntries>[]
>[];
}

return converted as ReturnTypeXinfoStream;
},
(xinfoStream) =>
glideRecordToRecord(xinfoStream) as ReturnTypeXinfoStream,
);
}

Expand Down
2 changes: 1 addition & 1 deletion node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,7 @@ export type FunctionListOptions = {
/** Type of the response of `FUNCTION LIST` command. */
export type FunctionListResponse = Record<
string,
GlideString | Record<string, GlideString | GlideString[]>[]
GlideString | Record<string, GlideString | null | GlideString[]>[]
>[];

/**
Expand Down
45 changes: 28 additions & 17 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ import {
BaseClientConfiguration,
Decoder,
DecoderOption,
GlideRecord,
glideRecordToRecord,
GlideString,
PubSubMsg,
ReadFrom, // eslint-disable-line @typescript-eslint/no-unused-vars
ReturnType,
} from "./BaseClient";
import {
FlushMode,
FunctionListOptions,
FunctionListResponse,
FunctionRestorePolicy,
FunctionStatsFullResponse,
InfoOptions,
LolwutOptions,
SortOptions,
createClientGetName,
createClientId,
createConfigGet,
Expand Down Expand Up @@ -54,6 +48,14 @@ import {
createSortReadOnly,
createTime,
createUnWatch,
FlushMode,
FunctionListOptions,
FunctionListResponse,
FunctionRestorePolicy,
FunctionStatsFullResponse,
InfoOptions,
LolwutOptions,
SortOptions,
} from "./Commands";
import { connection_request } from "./ProtobufMessage";
import { Transaction } from "./Transaction";
Expand Down Expand Up @@ -397,9 +399,12 @@ export class GlideClient extends BaseClient {
parameters: string[],
decoder?: Decoder,
): Promise<Record<string, GlideString>> {
return this.createWritePromise(createConfigGet(parameters), {
decoder: decoder,
});
return this.createWritePromise<GlideRecord<GlideString>>(
createConfigGet(parameters),
{
decoder,
},
).then(glideRecordToRecord);
}

/**
Expand Down Expand Up @@ -654,9 +659,12 @@ export class GlideClient extends BaseClient {
public async functionList(
options?: FunctionListOptions & DecoderOption,
): Promise<FunctionListResponse> {
return this.createWritePromise(createFunctionList(options), {
decoder: options?.decoder,
});
return this.createWritePromise<GlideRecord<unknown>[]>(
createFunctionList(options),
{
decoder: options?.decoder,
},
).then((res) => res.map(glideRecordToRecord) as FunctionListResponse);
}

/**
Expand Down Expand Up @@ -708,9 +716,12 @@ export class GlideClient extends BaseClient {
public async functionStats(
decoder?: Decoder,
): Promise<FunctionStatsFullResponse> {
return this.createWritePromise(createFunctionStats(), {
decoder,
});
return this.createWritePromise<GlideRecord<unknown>>(
createFunctionStats(),
{
decoder,
},
).then((res) => glideRecordToRecord(res) as FunctionStatsFullResponse);
}

/**
Expand Down
Loading

0 comments on commit 77e5794

Please sign in to comment.