1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

fix all FT.SUGGET variations

This commit is contained in:
Leibale
2023-10-11 17:04:13 -04:00
parent 77308ed670
commit 42b05b025b
8 changed files with 218 additions and 162 deletions

View File

@@ -1,4 +1,4 @@
import { ArrayReply, BlobStringReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types'; import { NullReply, ArrayReply, BlobStringReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
export interface FtSugGetOptions { export interface FtSugGetOptions {
FUZZY?: boolean; FUZZY?: boolean;
@@ -21,5 +21,5 @@ export default {
return args; return args;
}, },
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply> transformReply: undefined as unknown as () => NullReply | ArrayReply<BlobStringReply>
} as const satisfies Command; } as const satisfies Command;

View File

@@ -1,33 +1,35 @@
import { strict as assert } from 'node:assert'; import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './SUGGET_WITHPAYLOADS'; import SUGGET_WITHPAYLOADS from './SUGGET_WITHPAYLOADS';
describe('SUGGET WITHPAYLOADS', () => { describe('FT.SUGGET WITHPAYLOADS', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key', 'prefix'), SUGGET_WITHPAYLOADS.transformArguments('key', 'prefix'),
['FT.SUGGET', 'key', 'prefix', 'WITHPAYLOADS'] ['FT.SUGGET', 'key', 'prefix', 'WITHPAYLOADS']
); );
}); });
describe('client.ft.sugGetWithPayloads', () => { describe('client.ft.sugGetWithPayloads', () => {
testUtils.testWithClient('null', async client => { testUtils.testWithClient('null', async client => {
assert.equal( assert.equal(
await client.ft.sugGetWithPayloads('key', 'prefix'), await client.ft.sugGetWithPayloads('key', 'prefix'),
null null
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('with suggestions', async client => { testUtils.testWithClient('with suggestions', async client => {
await client.ft.sugAdd('key', 'string', 1, { PAYLOAD: 'payload' }); const [, reply] = await Promise.all([
client.ft.sugAdd('key', 'string', 1, {
PAYLOAD: 'payload'
}),
client.ft.sugGetWithPayloads('key', 'string')
]);
assert.deepEqual( assert.deepEqual(reply, [{
await client.ft.sugGetWithPayloads('key', 'string'), suggestion: 'string',
[{ payload: 'payload'
suggestion: 'string', }]);
payload: 'payload' }, GLOBAL.SERVERS.OPEN);
}] });
);
}, GLOBAL.SERVERS.OPEN);
});
}); });

View File

@@ -1,29 +1,31 @@
import { SugGetOptions, transformArguments as transformSugGetArguments } from './SUGGET'; import { NullReply, ArrayReply, BlobStringReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types';
import { isNullReply } from '@redis/client/dist/lib/commands/generic-transformers';
import SUGGET from './SUGGET';
export { IS_READ_ONLY } from './SUGGET'; export default {
FIRST_KEY_INDEX: SUGGET.FIRST_KEY_INDEX,
IS_READ_ONLY: SUGGET.IS_READ_ONLY,
transformArguments(...args: Parameters<typeof SUGGET.transformArguments>) {
const transformedArguments = SUGGET.transformArguments(...args);
transformedArguments.push('WITHPAYLOADS');
return transformedArguments;
},
transformReply(reply: NullReply | UnwrapReply<ArrayReply<BlobStringReply>>) {
if (isNullReply(reply)) return null;
export function transformArguments(key: string, prefix: string, options?: SugGetOptions): Array<string> { const transformedReply: Array<{
return [ suggestion: BlobStringReply;
...transformSugGetArguments(key, prefix, options), payload: BlobStringReply;
'WITHPAYLOADS' }> = new Array(reply.length / 2);
]; let replyIndex = 0,
} arrIndex = 0;
while (replyIndex < reply.length) {
export interface SuggestionWithPayload { transformedReply[arrIndex++] = {
suggestion: string; suggestion: reply[replyIndex++],
payload: string | null; payload: reply[replyIndex++]
} };
export function transformReply(rawReply: Array<string | null> | null): Array<SuggestionWithPayload> | null {
if (rawReply === null) return null;
const transformedReply = [];
for (let i = 0; i < rawReply.length; i += 2) {
transformedReply.push({
suggestion: rawReply[i]!,
payload: rawReply[i + 1]
});
} }
return transformedReply; return transformedReply;
} }
} as const satisfies Command;

View File

@@ -1,33 +1,33 @@
import { strict as assert } from 'node:assert'; import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './SUGGET_WITHSCORES'; import SUGGET_WITHSCORES from './SUGGET_WITHSCORES';
describe('SUGGET WITHSCORES', () => { describe('FT.SUGGET WITHSCORES', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key', 'prefix'), SUGGET_WITHSCORES.transformArguments('key', 'prefix'),
['FT.SUGGET', 'key', 'prefix', 'WITHSCORES'] ['FT.SUGGET', 'key', 'prefix', 'WITHSCORES']
); );
}); });
describe('client.ft.sugGetWithScores', () => { describe('client.ft.sugGetWithScores', () => {
testUtils.testWithClient('null', async client => { testUtils.testWithClient('null', async client => {
assert.equal( assert.equal(
await client.ft.sugGetWithScores('key', 'prefix'), await client.ft.sugGetWithScores('key', 'prefix'),
null null
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('with suggestions', async client => { testUtils.testWithClient('with suggestions', async client => {
await client.ft.sugAdd('key', 'string', 1); const [, reply] = await Promise.all([
client.ft.sugAdd('key', 'string', 1),
client.ft.sugGetWithScores('key', 's')
]);
assert.deepEqual( assert.ok(Array.isArray(reply));
await client.ft.sugGetWithScores('key', 'string'), assert.equal(reply.length, 1);
[{ assert.equal(reply[0].suggestion, 'string');
suggestion: 'string', assert.equal(typeof reply[0].score, 'number');
score: 2147483648 }, GLOBAL.SERVERS.OPEN);
}] });
);
}, GLOBAL.SERVERS.OPEN);
});
}); });

View File

@@ -1,29 +1,51 @@
import { SugGetOptions, transformArguments as transformSugGetArguments } from './SUGGET'; import { NullReply, ArrayReply, BlobStringReply, DoubleReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types';
import { isNullReply } from '@redis/client/dist/lib/commands/generic-transformers';
import SUGGET from './SUGGET';
export { IS_READ_ONLY } from './SUGGET'; export default {
FIRST_KEY_INDEX: SUGGET.FIRST_KEY_INDEX,
IS_READ_ONLY: SUGGET.IS_READ_ONLY,
transformArguments(...args: Parameters<typeof SUGGET.transformArguments>) {
const transformedArguments = SUGGET.transformArguments(...args);
transformedArguments.push('WITHSCORES');
return transformedArguments;
},
transformReply: {
2(reply: NullReply | UnwrapReply<ArrayReply<BlobStringReply>>) {
if (isNullReply(reply)) return null;
export function transformArguments(key: string, prefix: string, options?: SugGetOptions): Array<string> { const transformedReply: Array<{
return [ suggestion: BlobStringReply;
...transformSugGetArguments(key, prefix, options), score: number;
'WITHSCORES' }> = new Array(reply.length / 2);
]; let replyIndex = 0,
} arrIndex = 0;
while (replyIndex < reply.length) {
transformedReply[arrIndex++] = {
suggestion: reply[replyIndex++],
score: Number(reply[replyIndex++])
};
}
export interface SuggestionWithScores { return transformedReply;
suggestion: string; },
score: number; 3(reply: UnwrapReply<ArrayReply<BlobStringReply | DoubleReply>>) {
} if (isNullReply(reply)) return null;
export function transformReply(rawReply: Array<string> | null): Array<SuggestionWithScores> | null { const transformedReply: Array<{
if (rawReply === null) return null; suggestion: BlobStringReply;
score: DoubleReply;
}> = new Array(reply.length / 2);
let replyIndex = 0,
arrIndex = 0;
while (replyIndex < reply.length) {
transformedReply[arrIndex++] = {
suggestion: reply[replyIndex++] as BlobStringReply,
score: reply[replyIndex++] as DoubleReply
};
}
const transformedReply = []; return transformedReply;
for (let i = 0; i < rawReply.length; i += 2) {
transformedReply.push({
suggestion: rawReply[i],
score: Number(rawReply[i + 1])
});
} }
}
return transformedReply; } as const satisfies Command;
}

View File

@@ -1,34 +1,36 @@
import { strict as assert } from 'node:assert'; import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './SUGGET_WITHSCORES_WITHPAYLOADS'; import SUGGET_WITHSCORES_WITHPAYLOADS from './SUGGET_WITHSCORES_WITHPAYLOADS';
describe('SUGGET WITHSCORES WITHPAYLOADS', () => { describe('FT.SUGGET WITHSCORES WITHPAYLOADS', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key', 'prefix'), SUGGET_WITHSCORES_WITHPAYLOADS.transformArguments('key', 'prefix'),
['FT.SUGGET', 'key', 'prefix', 'WITHSCORES', 'WITHPAYLOADS'] ['FT.SUGGET', 'key', 'prefix', 'WITHSCORES', 'WITHPAYLOADS']
); );
}); });
describe('client.ft.sugGetWithScoresWithPayloads', () => { describe('client.ft.sugGetWithScoresWithPayloads', () => {
testUtils.testWithClient('null', async client => { testUtils.testWithClient('null', async client => {
assert.equal( assert.equal(
await client.ft.sugGetWithScoresWithPayloads('key', 'prefix'), await client.ft.sugGetWithScoresWithPayloads('key', 'prefix'),
null null
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('with suggestions', async client => { testUtils.testWithClient('with suggestions', async client => {
await client.ft.sugAdd('key', 'string', 1, { PAYLOAD: 'payload' }); const [, reply] = await Promise.all([
client.ft.sugAdd('key', 'string', 1, {
PAYLOAD: 'payload'
}),
client.ft.sugGetWithScoresWithPayloads('key', 'string')
]);
assert.deepEqual( assert.ok(Array.isArray(reply));
await client.ft.sugGetWithScoresWithPayloads('key', 'string'), assert.equal(reply.length, 1);
[{ assert.equal(reply[0].suggestion, 'string');
suggestion: 'string', assert.equal(typeof reply[0].score, 'number');
score: 2147483648, assert.equal(reply[0].payload, 'payload');
payload: 'payload' }, GLOBAL.SERVERS.OPEN);
}] });
);
}, GLOBAL.SERVERS.OPEN);
});
}); });

View File

@@ -1,30 +1,58 @@
import { SugGetOptions, transformArguments as transformSugGetArguments } from './SUGGET'; import { NullReply, ArrayReply, BlobStringReply, DoubleReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types';
import { SuggestionWithPayload } from './SUGGET_WITHPAYLOADS'; import { isNullReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { SuggestionWithScores } from './SUGGET_WITHSCORES'; import SUGGET from './SUGGET';
export { IS_READ_ONLY } from './SUGGET'; export default {
FIRST_KEY_INDEX: SUGGET.FIRST_KEY_INDEX,
IS_READ_ONLY: SUGGET.IS_READ_ONLY,
transformArguments(...args: Parameters<typeof SUGGET.transformArguments>) {
const transformedArguments = SUGGET.transformArguments(...args);
transformedArguments.push(
'WITHSCORES',
'WITHPAYLOADS'
);
return transformedArguments;
},
transformReply: {
2(reply: NullReply | UnwrapReply<ArrayReply<BlobStringReply>>) {
if (isNullReply(reply)) return null;
export function transformArguments(key: string, prefix: string, options?: SugGetOptions): Array<string> { const transformedReply: Array<{
return [ suggestion: BlobStringReply;
...transformSugGetArguments(key, prefix, options), score: number;
'WITHSCORES', payload: BlobStringReply;
'WITHPAYLOADS' }> = new Array(reply.length / 3);
]; let replyIndex = 0,
} arrIndex = 0;
while (replyIndex < reply.length) {
transformedReply[arrIndex++] = {
suggestion: reply[replyIndex++],
score: Number(reply[replyIndex++]),
payload: reply[replyIndex++]
};
}
type SuggestionWithScoresAndPayloads = SuggestionWithScores & SuggestionWithPayload; return transformedReply;
},
3(reply: NullReply | UnwrapReply<ArrayReply<BlobStringReply | DoubleReply>>) {
if (isNullReply(reply)) return null;
export function transformReply(rawReply: Array<string | null> | null): Array<SuggestionWithScoresAndPayloads> | null { const transformedReply: Array<{
if (rawReply === null) return null; suggestion: BlobStringReply;
score: DoubleReply;
payload: BlobStringReply;
}> = new Array(reply.length / 3);
let replyIndex = 0,
arrIndex = 0;
while (replyIndex < reply.length) {
transformedReply[arrIndex++] = {
suggestion: reply[replyIndex++] as BlobStringReply,
score: reply[replyIndex++] as DoubleReply,
payload: reply[replyIndex++] as BlobStringReply
};
}
const transformedReply = []; return transformedReply;
for (let i = 0; i < rawReply.length; i += 3) {
transformedReply.push({
suggestion: rawReply[i]!,
score: Number(rawReply[i + 1]!),
payload: rawReply[i + 2]
});
} }
}
return transformedReply; } as const satisfies Command;
}

View File

@@ -23,9 +23,9 @@ import EXPLAINCLI from './EXPLAINCLI';
import SPELLCHECK from './SPELLCHECK'; import SPELLCHECK from './SPELLCHECK';
import SUGADD from './SUGADD'; import SUGADD from './SUGADD';
import SUGDEL from './SUGDEL'; import SUGDEL from './SUGDEL';
// import SUGGET_WITHPAYLOADS from './SUGGET_WITHPAYLOADS'; import SUGGET_WITHPAYLOADS from './SUGGET_WITHPAYLOADS';
// import SUGGET_WITHSCORES_WITHPAYLOADS from './SUGGET_WITHSCORES_WITHPAYLOADS'; import SUGGET_WITHSCORES_WITHPAYLOADS from './SUGGET_WITHSCORES_WITHPAYLOADS';
// import SUGGET_WITHSCORES from './SUGGET_WITHSCORES'; import SUGGET_WITHSCORES from './SUGGET_WITHSCORES';
import SUGGET from './SUGGET'; import SUGGET from './SUGGET';
import SUGLEN from './SUGLEN'; import SUGLEN from './SUGLEN';
import SYNDUMP from './SYNDUMP'; import SYNDUMP from './SYNDUMP';
@@ -87,12 +87,12 @@ export default {
sugAdd: SUGADD, sugAdd: SUGADD,
SUGDEL, SUGDEL,
sugDel: SUGDEL, sugDel: SUGDEL,
// SUGGET_WITHPAYLOADS, SUGGET_WITHPAYLOADS,
// sugGetWithPayloads: SUGGET_WITHPAYLOADS, sugGetWithPayloads: SUGGET_WITHPAYLOADS,
// SUGGET_WITHSCORES_WITHPAYLOADS, SUGGET_WITHSCORES_WITHPAYLOADS,
// sugGetWithScoresWithPayloads: SUGGET_WITHSCORES_WITHPAYLOADS, sugGetWithScoresWithPayloads: SUGGET_WITHSCORES_WITHPAYLOADS,
// SUGGET_WITHSCORES, SUGGET_WITHSCORES,
// sugGetWithScores: SUGGET_WITHSCORES, sugGetWithScores: SUGGET_WITHSCORES,
SUGGET, SUGGET,
sugGet: SUGGET, sugGet: SUGGET,
SUGLEN, SUGLEN,