-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DX-997: Make cursor field in scan commands string (#1115)
* fix: make return type of cursor commands string scan commands can return a '0' or a long number as a string. If we deserialize, we get 0 from '0' and a truncated number from the long number string. Second one is non-ideal. In this case, we want to return the cursor as a string. This means excluding the cursor from the deserialization, hence the new deserializeScanResponse method. * fix: reorder deserialize and commandOptions this way, overwriting deserialize will be possible.
- Loading branch information
Showing
10 changed files
with
86 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
import { deserializeScanResponse } from "../util"; | ||
import { Command, CommandOptions } from "./command"; | ||
import { ScanCommandOptions } from "./scan"; | ||
|
||
/** | ||
* @see https://redis.io/commands/hscan | ||
*/ | ||
export class HScanCommand extends Command< | ||
[number, (string | number)[]], | ||
[number, (string | number)[]] | ||
[string, (string | number)[]], | ||
[string, (string | number)[]] | ||
> { | ||
constructor( | ||
[key, cursor, cmdOpts]: [key: string, cursor: number, cmdOpts?: ScanCommandOptions], | ||
opts?: CommandOptions<[number, (string | number)[]], [number, (string | number)[]]>, | ||
[key, cursor, cmdOpts]: [key: string, cursor: string | number, cmdOpts?: ScanCommandOptions], | ||
opts?: CommandOptions<[string, (string | number)[]], [string, (string | number)[]]>, | ||
) { | ||
const command = ["hscan", key, cursor]; | ||
const command: (number | string)[] = ["hscan", key, cursor]; | ||
if (cmdOpts?.match) { | ||
command.push("match", cmdOpts.match); | ||
} | ||
if (typeof cmdOpts?.count === "number") { | ||
command.push("count", cmdOpts.count); | ||
} | ||
|
||
super(command, opts); | ||
super(command, { | ||
deserialize: deserializeScanResponse, | ||
...opts, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
import { keygen, newHttpClient, randomID } from "../test-utils"; | ||
|
||
import { afterAll, expect, test } from "bun:test"; | ||
import { afterAll, describe, expect, test } from "bun:test"; | ||
import { SAddCommand } from "./sadd"; | ||
import { SScanCommand } from "./sscan"; | ||
const client = newHttpClient(); | ||
|
||
const { newKey, cleanup } = keygen(); | ||
|
||
afterAll(cleanup); | ||
test("without options", () => { | ||
describe("without options", () => { | ||
test("returns cursor and members", async () => { | ||
const key = newKey(); | ||
const member = randomID(); | ||
await new SAddCommand([key, member]).exec(client); | ||
const res = await new SScanCommand([key, 0]).exec(client); | ||
|
||
expect(res.length).toBe(2); | ||
expect(typeof res[0]).toBe("number"); | ||
expect(typeof res[0]).toBe("string"); | ||
expect(res![1].length > 0).toBe(true); | ||
}); | ||
}); | ||
|
||
test("with match", () => { | ||
describe("with match", () => { | ||
test("returns cursor and members", async () => { | ||
const key = newKey(); | ||
const member = randomID(); | ||
await new SAddCommand([key, member]).exec(client); | ||
const res = await new SScanCommand([key, 0, { match: member }]).exec(client); | ||
const res = await new SScanCommand([key, "0", { match: member }]).exec(client); | ||
|
||
expect(res.length).toBe(2); | ||
expect(typeof res[0]).toBe("number"); | ||
expect(typeof res[0]).toBe("string"); | ||
expect(res![1].length > 0).toBe(true); | ||
}); | ||
}); | ||
|
||
test("with count", () => { | ||
describe("with count", () => { | ||
test("returns cursor and members", async () => { | ||
const key = newKey(); | ||
const member = randomID(); | ||
await new SAddCommand([key, member]).exec(client); | ||
const res = await new SScanCommand([key, 0, { count: 1 }]).exec(client); | ||
const res = await new SScanCommand([key, "0", { count: 1 }]).exec(client); | ||
|
||
expect(res.length).toBe(2); | ||
expect(typeof res[0]).toBe("number"); | ||
expect(typeof res[0]).toBe("string"); | ||
expect(res![1].length > 0).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
import { deserializeScanResponse } from "../util"; | ||
import { Command, CommandOptions } from "./command"; | ||
import { ScanCommandOptions } from "./scan"; | ||
|
||
/** | ||
* @see https://redis.io/commands/sscan | ||
*/ | ||
export class SScanCommand extends Command< | ||
[number, (string | number)[]], | ||
[number, (string | number)[]] | ||
[string, (string | number)[]], | ||
[string, (string | number)[]] | ||
> { | ||
constructor( | ||
[key, cursor, opts]: [key: string, cursor: number, opts?: ScanCommandOptions], | ||
cmdOpts?: CommandOptions<[number, (string | number)[]], [number, (string | number)[]]>, | ||
[key, cursor, opts]: [key: string, cursor: string | number, opts?: ScanCommandOptions], | ||
cmdOpts?: CommandOptions<[string, (string | number)[]], [string, (string | number)[]]>, | ||
) { | ||
const command = ["sscan", key, cursor]; | ||
const command: (number | string)[] = ["sscan", key, cursor]; | ||
if (opts?.match) { | ||
command.push("match", opts.match); | ||
} | ||
if (typeof opts?.count === "number") { | ||
command.push("count", opts.count); | ||
} | ||
|
||
super(command, cmdOpts); | ||
super( | ||
command, | ||
{ | ||
deserialize: deserializeScanResponse, | ||
...cmdOpts, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
import { deserializeScanResponse } from "../util"; | ||
import { Command, CommandOptions } from "./command"; | ||
import { ScanCommandOptions } from "./scan"; | ||
|
||
/** | ||
* @see https://redis.io/commands/zscan | ||
*/ | ||
export class ZScanCommand extends Command< | ||
[number, (string | number)[]], | ||
[number, (string | number)[]] | ||
[string, (string | number)[]], | ||
[string, (string | number)[]] | ||
> { | ||
constructor( | ||
[key, cursor, opts]: [key: string, cursor: number, opts?: ScanCommandOptions], | ||
cmdOpts?: CommandOptions<[number, (string | number)[]], [number, (string | number)[]]>, | ||
[key, cursor, opts]: [key: string, cursor: string | number, opts?: ScanCommandOptions], | ||
cmdOpts?: CommandOptions<[string, (string | number)[]], [string, (string | number)[]]>, | ||
) { | ||
const command = ["zscan", key, cursor]; | ||
const command: (number | string)[] = ["zscan", key, cursor]; | ||
if (opts?.match) { | ||
command.push("match", opts.match); | ||
} | ||
if (typeof opts?.count === "number") { | ||
command.push("count", opts.count); | ||
} | ||
|
||
super(command, cmdOpts); | ||
super( | ||
command, | ||
{ | ||
deserialize: deserializeScanResponse, | ||
...cmdOpts, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters