You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
fix reply parshing
This commit is contained in:
@@ -4,7 +4,7 @@ import { transformArguments } from './COMMAND_DOCS';
|
|||||||
|
|
||||||
describe('COMMAND DOCS', () => {
|
describe('COMMAND DOCS', () => {
|
||||||
testUtils.isVersionGreaterThanHook([7, 0]);
|
testUtils.isVersionGreaterThanHook([7, 0]);
|
||||||
|
|
||||||
it('transformArguments', () => {
|
it('transformArguments', () => {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
transformArguments('SORT'),
|
transformArguments('SORT'),
|
||||||
@@ -13,18 +13,35 @@ describe('COMMAND DOCS', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
testUtils.testWithClient('client.commandDocs', async client => {
|
testUtils.testWithClient('client.commandDocs', async client => {
|
||||||
assert.deepEqual(
|
const docs = await client.commandDocs()
|
||||||
await client.commandDocs('sort'),
|
assert.equal(typeof docs, 'object');
|
||||||
[[
|
|
||||||
'sort',
|
for (const item of Object.values(docs)) {
|
||||||
{
|
if (item.summary) assert.equal(typeof item.summary, 'string');
|
||||||
summary: 'Sort the elements in a list, set or sorted set',
|
if (item.since) assert.equal(typeof item.since, 'string');
|
||||||
since: '1.0.0',
|
if (item.group) assert.equal(typeof item.group, 'string');
|
||||||
group: 'generic',
|
if (item.complexity) assert.equal(typeof item.complexity, 'string');
|
||||||
complexity: 'O(N+M*log(M)) where N is the number of elements in the list or set to sort, and M the number of returned elements. When the elements are not sorted, complexity is O(N).',
|
if (item.history) assert.ok(Array.isArray(item.history));
|
||||||
history: null
|
if (item.arguments) {
|
||||||
|
assert.equal(typeof item.arguments, 'object');
|
||||||
|
for (const argument of Object.values(item.arguments)) {
|
||||||
|
if (argument.keySpecIndex) assert.equal(typeof argument.keySpecIndex, 'number');
|
||||||
|
if (argument.type) assert.equal(typeof argument.type, 'string');
|
||||||
|
if (argument.token) assert.equal(typeof argument.token, 'string');
|
||||||
|
if (argument.summary) assert.equal(typeof argument.summary, 'string');
|
||||||
|
if (argument.flags) assert.ok(Array.isArray(argument.flags));
|
||||||
}
|
}
|
||||||
]]
|
}
|
||||||
);
|
if (item.subCommands) assert.equal(typeof item.subCommands, 'object');
|
||||||
|
}
|
||||||
|
|
||||||
|
// check with commands names
|
||||||
|
const specifiedDocs = await client.commandDocs('sort', 'get', 'cluster')
|
||||||
|
assert.equal(typeof specifiedDocs, 'object');
|
||||||
|
|
||||||
|
// check with non existing command
|
||||||
|
const nonExistingCommandDocs = await client.commandDocs('foo')
|
||||||
|
assert.equal(typeof specifiedDocs, 'object');
|
||||||
|
|
||||||
}, GLOBAL.SERVERS.OPEN);
|
}, GLOBAL.SERVERS.OPEN);
|
||||||
});
|
});
|
||||||
|
@@ -3,36 +3,126 @@ import { pushVerdictArguments } from './generic-transformers';
|
|||||||
|
|
||||||
export const IS_READ_ONLY = true;
|
export const IS_READ_ONLY = true;
|
||||||
|
|
||||||
export function transformArguments(keys: string | Array<string>): RedisCommandArguments {
|
export function transformArguments(...commandNames: Array<string>): RedisCommandArguments {
|
||||||
return pushVerdictArguments(['COMMAND', 'DOCS'], keys);
|
const args = ['COMMAND', 'DOCS'];
|
||||||
}
|
|
||||||
|
|
||||||
type CommandDocumentation = {
|
if (commandNames) {
|
||||||
summary: string;
|
pushVerdictArguments(args, commandNames);
|
||||||
since: string;
|
|
||||||
group: string;
|
|
||||||
complexity: string;
|
|
||||||
history?: Array<[version: string, feature: string]>;
|
|
||||||
};
|
|
||||||
|
|
||||||
type CommandDocsReply = Array<[CommandName: string, CommandDocumentation: CommandDocumentation]>;
|
|
||||||
|
|
||||||
export function transformReply(rawReply: Array<any>): CommandDocsReply {
|
|
||||||
const replyArray:CommandDocsReply = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < rawReply.length; i++) {
|
|
||||||
|
|
||||||
replyArray.push([
|
|
||||||
rawReply[i++], // The name of the command
|
|
||||||
{
|
|
||||||
summary: rawReply[i][1],
|
|
||||||
since: rawReply[i][3],
|
|
||||||
group: rawReply[i][5],
|
|
||||||
complexity: rawReply[i][7],
|
|
||||||
history: rawReply[i][8] == 'history' ? rawReply[i][9] : null
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return replyArray;
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ArgumentsDoc {
|
||||||
|
keySpecIndex?: number;
|
||||||
|
type?: string;
|
||||||
|
token?: string;
|
||||||
|
summary?: string;
|
||||||
|
flags?: Array<string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ArgumentsDocsReply {
|
||||||
|
[name: string]: ArgumentsDoc;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Doc {
|
||||||
|
summary?: string;
|
||||||
|
since?: string;
|
||||||
|
group?: string;
|
||||||
|
complexity?: string;
|
||||||
|
history?: Array<{
|
||||||
|
version: string;
|
||||||
|
description: string;
|
||||||
|
}>;
|
||||||
|
arguments?: ArgumentsDocsReply;
|
||||||
|
subCommands?: CommandDocsReply;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CommandDocsReply {
|
||||||
|
[command: string]: Doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function transformReply(rawReply: Array<any>): CommandDocsReply {
|
||||||
|
const reply:CommandDocsReply = {};
|
||||||
|
|
||||||
|
for (let i = 0; i < rawReply.length; i++) {
|
||||||
|
reply[rawReply[i++]] = createDocumentationInterface(rawReply[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createDocumentationInterface(rawDocumentation: Array<any>): Doc {
|
||||||
|
const doc:Doc = {}
|
||||||
|
|
||||||
|
for (let j = 0; j < rawDocumentation.length; j++) {
|
||||||
|
switch (rawDocumentation[j++]) {
|
||||||
|
case 'summary':
|
||||||
|
doc['summary'] = rawDocumentation[j];
|
||||||
|
break;
|
||||||
|
case 'since':
|
||||||
|
doc['since'] = rawDocumentation[j];
|
||||||
|
break;
|
||||||
|
case 'group':
|
||||||
|
doc['group'] = rawDocumentation[j];
|
||||||
|
break;
|
||||||
|
case 'complexity':
|
||||||
|
doc['complexity'] = rawDocumentation[j];
|
||||||
|
break;
|
||||||
|
case 'history':
|
||||||
|
const historyArray = []
|
||||||
|
for (let k = 0; k < rawDocumentation[j].length; k++) {
|
||||||
|
historyArray.push({
|
||||||
|
version: rawDocumentation[j][k][0],
|
||||||
|
description: rawDocumentation[j][k][1]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
doc['history'] = historyArray;
|
||||||
|
break;
|
||||||
|
case 'arguments':
|
||||||
|
doc['arguments'] = createArgumentsDocumentation(rawDocumentation[j]);
|
||||||
|
break;
|
||||||
|
case 'subcommands':
|
||||||
|
doc['subCommands'] = transformReply(rawDocumentation[j]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createArgumentsDocumentation(rawDocumentation: Array<any>): ArgumentsDocsReply {
|
||||||
|
const doc:ArgumentsDocsReply = {}
|
||||||
|
|
||||||
|
for (let k = 0; k < rawDocumentation.length; k++) {
|
||||||
|
let argumentName = "";
|
||||||
|
const argumentData:ArgumentsDoc = {};
|
||||||
|
|
||||||
|
for (let l = 0; l < rawDocumentation[k].length; l++) {
|
||||||
|
switch (rawDocumentation[k][l++]) {
|
||||||
|
case 'name':
|
||||||
|
argumentName = rawDocumentation[k][l];
|
||||||
|
break;
|
||||||
|
case 'type':
|
||||||
|
argumentData['type'] = rawDocumentation[k][l];
|
||||||
|
break;
|
||||||
|
case 'token':
|
||||||
|
argumentData['token'] = rawDocumentation[k][l];
|
||||||
|
break;
|
||||||
|
case 'summary':
|
||||||
|
argumentData['summary'] = rawDocumentation[k][l];
|
||||||
|
break;
|
||||||
|
case 'flags':
|
||||||
|
argumentData['flags'] = rawDocumentation[k][l];
|
||||||
|
break;
|
||||||
|
case 'key_spec_index':
|
||||||
|
argumentData['keySpecIndex'] = rawDocumentation[k][l];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
doc[argumentName] = argumentData;
|
||||||
|
}
|
||||||
|
|
||||||
|
return doc;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user