resp

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package resp implements the Redis serialization protocol, versions 2 and 3. Spec: https://redis.io/docs/latest/develop/reference/protocol-spec/

Index

Constants

View Source
const (
	MaxBulkLen  = 512 << 20 // 512 MB, same as Redis
	MaxArrayLen = 1 << 20
)

Variables

View Source
var OK = Simple("OK")

Functions

func Append

func Append(b []byte, v Value, proto int) []byte

Append appends the encoding of v to b using the given protocol version.

func AppendCommand

func AppendCommand(b []byte, args []string) []byte

AppendCommand appends args encoded as a RESP array of bulk strings.

func FormatFloat

func FormatFloat(f float64) string

FormatFloat formats a double the way Redis does in RESP2 replies.

func SplitInline

func SplitInline(line string) ([]string, error)

SplitInline splits an inline command line, honouring "double" and 'single' quotes and \n \t \xHH escapes inside double quotes, like redis-cli.

Types

type ProtocolError

type ProtocolError struct{ Msg string }

ProtocolError means the peer sent bytes that are not valid RESP. The connection should be closed after replying.

func (*ProtocolError) Error

func (e *ProtocolError) Error() string

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) Buffered

func (r *Reader) Buffered() int

Buffered reports bytes already received but not parsed. The server uses it to batch replies of pipelined commands into one flush.

func (*Reader) ReadCommand

func (r *Reader) ReadCommand() ([]string, error)

ReadCommand reads one client command: either a RESP array of bulk strings or an inline command ("SET a 1\r\n", as typed in telnet).

func (*Reader) ReadValue

func (r *Reader) ReadValue() (Value, error)

ReadValue reads any RESP2/RESP3 value (used by clients and replication).

type Type

type Type byte
const (
	TypeSimple  Type = '+'
	TypeError   Type = '-'
	TypeInteger Type = ':'
	TypeBulk    Type = '$'
	TypeArray   Type = '*'
	// RESP3 types. When talking to a RESP2 client they are down-converted.
	TypeNull   Type = '_'
	TypeBool   Type = '#'
	TypeDouble Type = ','
	TypeMap    Type = '%'
	TypeSet    Type = '~'
	TypePush   Type = '>'
)

type Value

type Value struct {
	Type  Type
	Str   string  // simple, error, bulk
	Int   int64   // integer
	Float float64 // double
	Bool  bool    // bool
	Elems []Value // array, set, push; map stores key, value, key, value...
	// contains filtered or unexported fields
}

Value is any RESP value. Which fields are used depends on Type.

func Array

func Array(vs ...Value) Value

func Bool

func Bool(b bool) Value

func Bulk

func Bulk(s string) Value

func Bytes

func Bytes(b []byte) Value

func Double

func Double(f float64) Value

func Err

func Err(msg string) Value

Err returns an error reply. msg should start with an error code such as "ERR" or "WRONGTYPE"; if it doesn't, "ERR " is added.

func Errf

func Errf(format string, args ...any) Value

func Int

func Int(n int64) Value

func Invalidate

func Invalidate(keys []string) Value

Invalidate builds the push message for client-side caching. A nil keys slice means "flush everything" (sent after FLUSHALL).

func Map

func Map(kv ...Value) Value

Map builds a map from alternating key/value elements.

func Multi

func Multi(vs ...Value) Value

func None

func None() Value

func Null

func Null() Value

func NullArray

func NullArray() Value

func Push

func Push(vs ...Value) Value

func SetOf

func SetOf(vs ...Value) Value

func Simple

func Simple(s string) Value

func StringSet

func StringSet(ss []string) Value

StringSet builds a RESP3 set (array in RESP2) of bulk strings.

func Strings

func Strings(ss []string) Value

Strings builds an array of bulk strings.

func (Value) IsError

func (v Value) IsError() bool

func (Value) IsMulti

func (v Value) IsMulti() bool

func (Value) IsNone

func (v Value) IsNone() bool

func (Value) IsNull

func (v Value) IsNull() bool

func (Value) String

func (v Value) String() string

String returns the value as a plain string (bulk/simple/integer/double); handy in tests and clients.

type Writer

type Writer struct {
	Proto int
	// contains filtered or unexported fields
}

Writer encodes values for one connection. Proto is 2 or 3.

func NewWriter

func NewWriter(w io.Writer, proto int) *Writer

func (*Writer) Flush

func (w *Writer) Flush() error

func (*Writer) Write

func (w *Writer) Write(v Value) error

func (*Writer) WriteCommand

func (w *Writer) WriteCommand(args ...string) error

WriteCommand encodes a command as an array of bulk strings (client side).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL