-
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.
* feat: add bitfield resolves #1150 * fix: bitfield tests not running * fix: type constraints and protected props * fix: type constraints * feat: add bitfield to pipeline (also addresses pr comments) * fix: code complexity opt to pass private properties / methods from pipeline directly into the constructor for the bitfield command. no need to create two separate classes for command and pipeline
- Loading branch information
Showing
7 changed files
with
153 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { afterAll, describe, expect, test } from "bun:test"; | ||
import { keygen, newHttpClient } from "../test-utils"; | ||
import { BitFieldCommand } from "./bitfield"; | ||
|
||
const client = newHttpClient(); | ||
|
||
const { newKey, cleanup } = keygen(); | ||
afterAll(cleanup); | ||
|
||
describe("when key is not set", () => { | ||
test("returns 0", async () => { | ||
const key = newKey(); | ||
const res = await new BitFieldCommand([key], client).get("u4", "#0").exec(); | ||
expect(res).toEqual([0]); | ||
}); | ||
}); | ||
|
||
describe("when key is set", () => { | ||
test("sets / gets value", async () => { | ||
const key = newKey(); | ||
const value = 42; | ||
const res = await new BitFieldCommand([key], client) | ||
.set("u8", "#0", value) | ||
.get("u8", "#0") | ||
.exec(); | ||
expect(res).toEqual([0, value]); | ||
}); | ||
|
||
test("increments value", async () => { | ||
const key = newKey(); | ||
const value = 42; | ||
const increment = 10; | ||
const res = await new BitFieldCommand([key], client) | ||
.set("u8", "#0", value) | ||
.incrby("u8", "#0", increment) | ||
.exec(); | ||
expect(res).toEqual([0, value + increment]); | ||
}); | ||
|
||
test("overflows", async () => { | ||
const key = newKey(); | ||
const value = 255; | ||
const bitWidth = 8; | ||
const res = await new BitFieldCommand([key], client) | ||
.set(`u${bitWidth}`, "#0", value) | ||
.incrby(`u${bitWidth}`, "#0", 10) | ||
.overflow("WRAP") | ||
.exec(); | ||
expect(res).toEqual([0, (value + 10) % 2 ** bitWidth]); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { type Requester } from "../http"; | ||
import { Command, type CommandOptions } from "./command"; | ||
|
||
type SubCommandArgs<TRest extends unknown[] = []> = [ | ||
encoding: string, // u1 - u63 | i1 - i64 | ||
offset: number | string, // <int> | #<int> | ||
...TRest, | ||
]; | ||
|
||
/** | ||
* @see https://redis.io/commands/bitfield | ||
*/ | ||
export class BitFieldCommand<T = Promise<number[]>> { | ||
private command: (string | number)[]; | ||
|
||
constructor( | ||
args: [key: string], | ||
private client: Requester, | ||
private opts?: CommandOptions<number[], number[]>, | ||
private execOperation = (command: Command<number[], number[]>) => | ||
command.exec(this.client) as T, | ||
) { | ||
this.command = ["bitfield", ...args]; | ||
} | ||
|
||
private chain(...args: typeof this.command) { | ||
this.command.push(...args); | ||
return this; | ||
} | ||
|
||
get(...args: SubCommandArgs) { | ||
return this.chain("get", ...args); | ||
} | ||
|
||
set(...args: SubCommandArgs<[value: number]>) { | ||
return this.chain("set", ...args); | ||
} | ||
|
||
incrby(...args: SubCommandArgs<[increment: number]>) { | ||
return this.chain("incrby", ...args); | ||
} | ||
|
||
overflow(overflow: "WRAP" | "SAT" | "FAIL") { | ||
return this.chain("overflow", overflow); | ||
} | ||
|
||
exec() { | ||
const command = new Command(this.command, this.opts); | ||
return this.execOperation(command); | ||
} | ||
} |
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
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