RESP (REdis Serialization Protocol) encoding and decoding over ::hot::tcp. Handles RESP2 and the RESP3 additions. Internal to the redis package.

Commands are encoded as arrays of bulk strings. Replies are decoded to Hot values: simple strings to Str, bulk strings to Str when valid UTF-8 (else Bytes), integers to Int, doubles to Dec, booleans to Bool, arrays/sets/pushes to Vec, maps to Map, and the various null forms to null. Error replies decode to Result.Err.

Values

CR

CR: Int 13

LF

LF: Int 10

Functions

encode-command

fn (args: Vec): Bytes

Encode a command (a Vec of arguments) as a RESP array of bulk strings. Bytes arguments pass through unchanged (bulk strings are binary-safe); everything else is coerced to its string form. Lengths are byte lengths, not character lengths.

Example

Str(::redis::resp/encode-command(["PING"]))  // "*1\r\n$4\r\nPING\r\n"

read-reply

fn (socket): Any

Read and decode a single RESP reply from socket. Blocks until a full reply arrives. Error replies (-) decode to a Result.Err carrying the Redis error message.

Example

socket ::hot::tcp/connect("localhost", 6379)
::hot::tcp/write(socket, ::redis::resp/encode-command(["PING"]))
reply ::redis::resp/read-reply(socket)
if-err(reply, (e) { println(`redis said no: ${e}`) })