Skip to content

Commit

Permalink
fix: Introduce replyTypes namespace (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored Mar 27, 2021
1 parent a9518b1 commit e27e12d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ executor. You can send raw redis commands and receive replies.
```ts
{
const reply = await redis.executor.exec("SET", "redis", "nice");
assert(reply.type === SIMPLE_STRING_TYPE);
assert(reply.type === replyTypes.SimpleString);
assert(reply.value() === "OK");
}

{
const reply = await redis.executor.exec("GET", "redis");
assert(reply.type === BULK_STRING_TYPE);
assert(reply.type === replyTypes.BulkString);
assert(reply.value() === "nice");
}
```
Expand Down
7 changes: 1 addition & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// Generated by tools/make_mod.ts. Don't edit.
export {
ARRAY_TYPE,
BULK_STRING_TYPE,
INTEGER_TYPE,
SIMPLE_STRING_TYPE,
} from "./protocol/mod.ts";
export { replyTypes } from "./protocol/mod.ts";
export { connect, parseURL } from "./redis.ts";
export {
ConnectionClosedError,
Expand Down
5 changes: 1 addition & 4 deletions protocol/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export type {
} from "./types.ts";

export {
ARRAY_TYPE,
BULK_STRING_TYPE,
createSimpleStringReply,
INTEGER_TYPE,
readArrayReply,
readReply,
SIMPLE_STRING_TYPE,
replyTypes,
unwrapReply,
} from "./reply.ts";

Expand Down
18 changes: 10 additions & 8 deletions protocol/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const SimpleStringCode = "+".charCodeAt(0);
const ArrayReplyCode = "*".charCodeAt(0);
const ErrorReplyCode = "-".charCodeAt(0);

export const INTEGER_TYPE = "integer";
export const SIMPLE_STRING_TYPE = "simple string";
export const ARRAY_TYPE = "array";
export const BULK_STRING_TYPE = "bulk string";
export const replyTypes = {
Integer: "integer",
SimpleString: "simple string",
Array: "array",
BulkString: "bulk string",
} as const;

export function unwrapReply(
reply: types.RedisReplyOrError,
Expand Down Expand Up @@ -72,7 +74,7 @@ class IntegerReply implements types.IntegerReply {
}

get type(): "integer" {
return INTEGER_TYPE;
return replyTypes.Integer;
}

value(): types.Integer {
Expand Down Expand Up @@ -108,7 +110,7 @@ class BulkReply implements types.BulkReply {
}

get type(): "bulk string" {
return BULK_STRING_TYPE;
return replyTypes.BulkString;
}

value(): types.Bulk {
Expand Down Expand Up @@ -138,7 +140,7 @@ class SimpleStringReply implements types.SimpleStringReply {
}

get type(): "simple string" {
return SIMPLE_STRING_TYPE;
return replyTypes.SimpleString;
}

value(): types.SimpleString {
Expand Down Expand Up @@ -189,7 +191,7 @@ class ArrayReply implements types.ArrayReply {
}

get type(): "array" {
return ARRAY_TYPE;
return replyTypes.Array;
}

value() {
Expand Down
10 changes: 5 additions & 5 deletions tests/executor_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BulkReply, ErrorReplyError, parseURL } from "../mod.ts";
import { BulkReply, ErrorReplyError, parseURL, replyTypes } from "../mod.ts";
import {
assert,
assertEquals,
Expand All @@ -20,21 +20,21 @@ suite.test("simple, string, and integer replies", async () => {
// simple string
{
const reply = await client.executor.exec("SET", "key", "a");
assertEquals(reply.type, "simple string");
assertEquals(reply.type, replyTypes.SimpleString);
assertEquals(reply.value(), "OK");
}

// bulk string
{
const reply = await client.executor.exec("GET", "key");
assertEquals(reply.type, "bulk string");
assertEquals(reply.type, replyTypes.BulkString);
assertEquals(reply.value(), "a");
}

// integer
{
const reply = await client.executor.exec("EXISTS", "key");
assertEquals(reply.type, "integer");
assertEquals(reply.type, replyTypes.Integer);
assertEquals(reply.value(), 1);
}
});
Expand All @@ -43,7 +43,7 @@ suite.test("get the raw data as Uint8Array", async () => {
const encoder = new TextEncoder();
await client.set("key", encoder.encode("hello"));
const reply = await client.executor.exec("GET", "key");
assertEquals(reply.type, "bulk string");
assertEquals(reply.type, replyTypes.BulkString);
assertEquals((reply as BulkReply).buffer(), encoder.encode("hello"));
});

Expand Down

0 comments on commit e27e12d

Please sign in to comment.