Skip to content

Commit

Permalink
BREAKING: Change BulkNil from undefined to null (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored Aug 29, 2022
1 parent e8299a5 commit 21242fe
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 49 deletions.
4 changes: 2 additions & 2 deletions protocol/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class BulkReply extends BaseReply {
}

override bulk() {
return this.#body ? decoder.decode(this.#body) : undefined;
return this.#body ? decoder.decode(this.#body) : null;
}

override buffer() {
Expand Down Expand Up @@ -223,7 +223,7 @@ export async function readArrayReplyBody(
const argCount = parseSize(line);
if (argCount === -1) {
// `-1` indicates a null array
return undefined;
return null;
}

const array: types.ConditionalArray = [];
Expand Down
4 changes: 2 additions & 2 deletions protocol/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export type Bulk = BulkString | BulkNil;
export type BulkString = string;

/**
* @description Represents the **null bulk string** in the RESP2 protocol.
* @description Represents the **null bulk string** and **null array** in the RESP2 protocol.
*/
export type BulkNil = undefined;
export type BulkNil = null;

/**
* @description Represents the some type in the RESP2 protocol.
Expand Down
38 changes: 22 additions & 16 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1713,11 +1713,13 @@ class RedisImpl implements Redis {
);

return {
length: rawnum(data.get("length")),
radixTreeKeys: rawnum(data.get("radix-tree-keys")),
radixTreeNodes: rawnum(data.get("radix-tree-nodes")),
groups: rawnum(data.get("groups")),
lastGeneratedId: parseXId(rawstr(data.get("last-generated-id"))),
length: rawnum(data.get("length") ?? null),
radixTreeKeys: rawnum(data.get("radix-tree-keys") ?? null),
radixTreeNodes: rawnum(data.get("radix-tree-nodes") ?? null),
groups: rawnum(data.get("groups") ?? null),
lastGeneratedId: parseXId(
rawstr(data.get("last-generated-id") ?? null),
),
firstEntry,
lastEntry,
};
Expand Down Expand Up @@ -1746,10 +1748,12 @@ class RedisImpl implements Redis {
raw: Raw,
) => parseXMessage(raw as XReadIdData));
return {
length: rawnum(data.get("length")),
radixTreeKeys: rawnum(data.get("radix-tree-keys")),
radixTreeNodes: rawnum(data.get("radix-tree-nodes")),
lastGeneratedId: parseXId(rawstr(data.get("last-generated-id"))),
length: rawnum(data.get("length") ?? null),
radixTreeKeys: rawnum(data.get("radix-tree-keys") ?? null),
radixTreeNodes: rawnum(data.get("radix-tree-nodes") ?? null),
lastGeneratedId: parseXId(
rawstr(data.get("last-generated-id") ?? null),
),
entries,
groups: parseXGroupDetail(data.get("groups") as ConditionalArray),
};
Expand All @@ -1763,10 +1767,12 @@ class RedisImpl implements Redis {
raws.map((raw) => {
const data = convertMap(raw);
return {
name: rawstr(data.get("name")),
consumers: rawnum(data.get("consumers")),
pending: rawnum(data.get("pending")),
lastDeliveredId: parseXId(rawstr(data.get("last-delivered-id"))),
name: rawstr(data.get("name") ?? null),
consumers: rawnum(data.get("consumers") ?? null),
pending: rawnum(data.get("pending") ?? null),
lastDeliveredId: parseXId(
rawstr(data.get("last-delivered-id") ?? null),
),
};
}),
);
Expand All @@ -1783,9 +1789,9 @@ class RedisImpl implements Redis {
raws.map((raw) => {
const data = convertMap(raw);
return {
name: rawstr(data.get("name")),
pending: rawnum(data.get("pending")),
idle: rawnum(data.get("idle")),
name: rawstr(data.get("name") ?? null),
pending: rawnum(data.get("pending") ?? null),
idle: rawnum(data.get("idle") ?? null),
};
}),
);
Expand Down
14 changes: 8 additions & 6 deletions stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,11 @@ export function parseXGroupDetail(rawGroups: ConditionalArray): XGroupDetail[] {

out.push(
{
name: rawstr(data.get("name")),
lastDeliveredId: parseXId(rawstr(data.get("last-delivered-id"))),
pelCount: rawnum(data.get("pel-count")),
name: rawstr(data.get("name") ?? null),
lastDeliveredId: parseXId(
rawstr(data.get("last-delivered-id") ?? null),
),
pelCount: rawnum(data.get("pel-count") ?? null),
pending: parseXPendingCounts(
data.get("pending") as ConditionalArray,
),
Expand Down Expand Up @@ -352,9 +354,9 @@ export function parseXConsumerDetail(
);

const r = {
name: rawstr(data.get("name")),
seenTime: rawnum(data.get("seen-time")),
pelCount: rawnum(data.get("pel-count")),
name: rawstr(data.get("name") ?? null),
seenTime: rawnum(data.get("seen-time") ?? null),
pelCount: rawnum(data.get("pel-count") ?? null),
pending,
};

Expand Down
2 changes: 1 addition & 1 deletion tests/commands/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function generalTests(
afterAll(() => client.close());

it("conccurent", async () => {
let promises: Promise<string | undefined>[] = [];
let promises: Promise<string | null>[] = [];
for (const key of ["a", "b", "c"]) {
promises.push(client.set(key, key));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/commands/geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function geoTests(
Catania: [15.087269, 37.502669],
});
const resp = await client.geohash("Sicily", "Palermo", "Catania", "Enna");
assertEquals(resp, ["sqc8b49rny0", "sqdtr74hyu0", undefined]);
assertEquals(resp, ["sqc8b49rny0", "sqdtr74hyu0", null]);
});

it("geopos", async () => {
Expand All @@ -62,7 +62,7 @@ export function geoTests(
assertEquals(resp, [
["13.36138933897018433", "38.11555639549629859"],
["15.08726745843887329", "37.50266842333162032"],
undefined,
null,
]);
});

Expand All @@ -74,7 +74,7 @@ export function geoTests(
let resp = await client.geodist("Sicily", "Palermo", "Catania");
assertEquals(resp, "166274.1516");
resp = await client.geodist("Sicily", "Palermo", "Enna");
assertEquals(resp, undefined);
assertEquals(resp, null);
});

it("georadius", async () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function hashTests(
it("hget", async () => {
await client.hset("key", "f1", "1");
assertEquals(await client.hget("key", "f1"), "1");
assertEquals(await client.hget("key", "f2"), undefined);
assertEquals(await client.hget("key", "f2"), null);
});

it("hgetall", async () => {
Expand Down Expand Up @@ -75,7 +75,7 @@ export function hashTests(
assertEquals(await client.hmget("key", "f1", "f2", "f3"), [
"1",
"2",
undefined,
null,
]);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/commands/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function keyTests(

it("object freq", async () => {
const v = await client.objectFreq("nosuchkey");
assertEquals(v, undefined);
assertEquals(v, null);
});

it("object help", async () => {
Expand Down
18 changes: 9 additions & 9 deletions tests/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ export function listTests(
assertEquals(await client.blpop(2, "list"), ["list", "1"]);
});

it("blpop returns undefined on timeout", async () => {
assertEquals(await client.blpop(1, "list"), undefined);
it("blpop returns null on timeout", async () => {
assertEquals(await client.blpop(1, "list"), null);
});

it("brpop", async () => {
await client.rpush("list", "1", "2");
assertEquals(await client.brpop(2, "list"), ["list", "2"]);
});

it("brpop returns undefined on timeout", async () => {
assertEquals(await client.brpop(1, "list"), undefined);
it("brpop returns null on timeout", async () => {
assertEquals(await client.brpop(1, "list"), null);
});

it("brpoplpush", async () => {
await client.rpush("list", "1", "2");
assertEquals(await client.brpoplpush("list", "list", 2), "2");
});

it("brpoplpush returns undefined on timeout", async () => {
assertEquals(await client.brpoplpush("list", "list", 1), undefined);
it("brpoplpush returns null on timeout", async () => {
assertEquals(await client.brpoplpush("list", "list", 1), null);
});

it("lindex", async () => {
await client.rpush("list", "1", "2");
assertEquals(await client.lindex("list", 0), "1");
assertEquals(await client.lindex("list", 3), undefined);
assertEquals(await client.lindex("list", 3), null);
});

it("linsert", async () => {
Expand All @@ -75,7 +75,7 @@ export function listTests(
it("lpos", async () => {
await client.rpush("list", "a", "b", "c", "1");
assertEquals(await client.lpos("list", "c"), 2);
assertEquals(await client.lpos("list", "d"), undefined);
assertEquals(await client.lpos("list", "d"), null);
});

it("lpos with rank", async () => {
Expand All @@ -90,7 +90,7 @@ export function listTests(

it("lpos with maxlen", async () => {
await client.rpush("list", "a", "b", "c");
assertEquals(await client.lpos("list", "c", { maxlen: 2 }), undefined);
assertEquals(await client.lpos("list", "c", { maxlen: 2 }), null);
assertEquals(await client.lpos("list", "c", { maxlen: 3 }), 2);
});

Expand Down
10 changes: 5 additions & 5 deletions tests/commands/sorted_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ export function zsetTests(
assertEquals(await client.bzpopmin(1, "key"), ["key", "1", "1"]);
});

it("bzpopmin returns undefined on timeout", async () => {
it("bzpopmin returns null on timeout", async () => {
const arr = await client.bzpopmin(1, "key");
assertEquals(arr, undefined);
assertEquals(arr, null);
});

it("bzpopmax", async () => {
await client.zadd("key", { "1": 1, "2": 2 });
assertEquals(await client.bzpopmax(1, "key"), ["key", "2", "2"]);
});

it("bzpopmax returns undefined on timeout", async () => {
it("bzpopmax returns null on timeout", async () => {
const arr = await client.bzpopmax(1, "key");
assertEquals(arr, undefined);
assertEquals(arr, null);
});

it("zadd", async () => {
Expand Down Expand Up @@ -85,7 +85,7 @@ export function zsetTests(
it("zaddIncrWithMode", async () => {
assertEquals(
await client.zaddIncr("key", 1, "one", { mode: "XX" }),
undefined,
null,
"no member should be added",
);
assertEquals(
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function stringTests(

it("getWhenNil", async () => {
const hoge = await client.get("none");
assertEquals(hoge, undefined);
assertEquals(hoge, null);
});

it("getbit", async () => {
Expand Down Expand Up @@ -161,7 +161,7 @@ export function stringTests(
assertEquals(rep2, 0); // No key was set.
assertEquals(await client.get("key1"), "foo");
assertEquals(await client.get("key2"), "bar");
assertEquals(await client.get("key3"), undefined);
assertEquals(await client.get("key3"), null);
});

it("psetex", async () => {
Expand Down

0 comments on commit 21242fe

Please sign in to comment.