resp

package
v0.0.0-...-5546168 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package resp provides RESP protocol serialization and deserialization

Index

Constants

View Source
const CRLF = "\r\n"

CRLF is the line ending used in RESP

View Source
const MaxBulkStringSize = 512 * 1024 * 1024 // 512 mb

MaxBulkStringSize is the maximum bulk string size specifed in the RESP Protocol

Variables

View Source
var (
	ErrNull = errors.New("Null")
)

Functions

This section is empty.

Types

type Any

type Any interface {
	Type() Type
	Decode(x interface{}) error
	Appender
	// contains filtered or unexported methods
}

Any is the interface of all RESP values

func ReadAny

func ReadAny(r *bufio.Reader) (Any, error)

ReadAny read a RESP Value from a buffered reader

type Appender

type Appender interface {
	AppendRESP(buf []byte) []byte
}

Appender appends a RESP value to a buffer

type Array

type Array []Any

Array is a RESP array value

func (Array) AppendRESP

func (a Array) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (Array) BulkStringArray

func (a Array) BulkStringArray() (BulkStringArray, error)

BulkStringArray converts a RESP array to a slice of strings

func (Array) BulkStringMap

func (a Array) BulkStringMap() (BulkStringMap, error)

BulkStringMap converts a RESP array of consecutive non null bulk string pairs to a map

func (Array) Decode

func (a Array) Decode(x interface{}) error

func (Array) Len

func (a Array) Len() int

Len returns the array length

func (Array) Null

func (a Array) Null() bool

Null checks if the array is null

func (Array) Type

func (Array) Type() Type

Type implements Value interface

func (*Array) UnmarshalRESP

func (a *Array) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type Buffer

type Buffer struct {
	B []byte
}

func (*Buffer) AppendRESP

func (b *Buffer) AppendRESP(buf []byte) []byte

func (*Buffer) Array

func (b *Buffer) Array(arr ...Any)

func (*Buffer) BulkString

func (b *Buffer) BulkString(s string)

func (*Buffer) BulkStringArray

func (b *Buffer) BulkStringArray(s ...string)

func (*Buffer) BulkStringBytes

func (b *Buffer) BulkStringBytes(data []byte)

func (*Buffer) Err

func (b *Buffer) Err(msg string)

func (*Buffer) Int

func (b *Buffer) Int(n int64)

func (*Buffer) SimpleString

func (b *Buffer) SimpleString(s string)

type BulkString

type BulkString struct {
	String string
	Valid  bool
}

BulkString is a RESP bulk string value

func (*BulkString) AppendRESP

func (s *BulkString) AppendRESP(buf []byte) []byte

func (*BulkString) Decode

func (s *BulkString) Decode(x interface{}) error

Decode implements Value interface

func (*BulkString) Len

func (s *BulkString) Len() int

Len returns the bulk string length

func (*BulkString) Null

func (s *BulkString) Null() bool

Null checks if the bulk string is null

func (*BulkString) Type

func (*BulkString) Type() Type

Type implements Value interface

func (*BulkString) UnmarshalRESP

func (s *BulkString) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type BulkStringArray

type BulkStringArray []string

BulkStringArray is RESP array containing non null bulk strings

func (BulkStringArray) AppendRESP

func (a BulkStringArray) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (BulkStringArray) EachKV

func (a BulkStringArray) EachKV(fn func(k, v string) error) (err error)

EachKV calls fn for each key/value pair in an array

func (BulkStringArray) Get

func (a BulkStringArray) Get(key string) (string, bool)

Get treats the array as a map of consecutive key/value pairs

func (*BulkStringArray) UnmarshalRESP

func (a *BulkStringArray) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type BulkStringBytes

type BulkStringBytes []byte

BulkStringBytes is a buffer of a bulk string

func (BulkStringBytes) AppendRESP

func (raw BulkStringBytes) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (*BulkStringBytes) UnmarshalRESP

func (raw *BulkStringBytes) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type BulkStringMap

type BulkStringMap map[string]string

BulkStringMap is RESP map containing non null bulk strings

func (BulkStringMap) AppendRESP

func (m BulkStringMap) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (*BulkStringMap) UnmarshalRESP

func (m *BulkStringMap) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type DecodeError

type DecodeError struct {
	Reason error
	Source Any
	Dest   interface{}
}

func (*DecodeError) Error

func (d *DecodeError) Error() string

func (*DecodeError) Unwrap

func (d *DecodeError) Unwrap() error

type Error

type Error string

Error is a RESP error value

func (Error) AppendRESP

func (e Error) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (Error) Decode

func (e Error) Decode(x interface{}) error

Decode implements Value interface

func (Error) Error

func (e Error) Error() string

func (Error) Type

func (Error) Type() Type

Type implements Value interface

func (*Error) UnmarshalRESP

func (e *Error) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type Integer

type Integer int64

Integer is a RESP integer value

func (Integer) AppendRESP

func (i Integer) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (Integer) Decode

func (i Integer) Decode(x interface{}) error

Decode implements Value interface

func (Integer) Type

func (i Integer) Type() Type

Type implements Value interface

func (*Integer) UnmarshalRESP

func (i *Integer) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type Iter

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

Iter iterates over an array of RESP values

func (*Iter) Close

func (iter *Iter) Close()

Close ends the iterator and releases the reply buffer to avoid memory leaks

func (*Iter) More

func (iter *Iter) More() bool

More checks if the iterator has more elements

func (*Iter) Next

func (iter *Iter) Next()

Next advances the iteration

func (*Iter) Value

func (iter *Iter) Value() Value

Value returns the current iterator value

type Message

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

Message is a reply for a redis command.

func (*Message) Any

func (msg *Message) Any() Any

Any returns the reply RESP value

func (*Message) AppendRESP

func (msg *Message) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (*Message) Parse

func (msg *Message) Parse(buf []byte) (Value, error)

Parse parses a RESP value from a buffer

func (*Message) ParseString

func (msg *Message) ParseString(str string) (Value, error)

ParseString parses a RESP value from a string

func (*Message) ReadFrom

func (msg *Message) ReadFrom(r *bufio.Reader) (Value, error)

ReadFrom reads a reply from a redis stream.

func (*Message) Reset

func (msg *Message) Reset()

Reset resets the reply buffer

func (*Message) Value

func (msg *Message) Value() Value

Value returns the root Value node

type NopUnmarshaler

type NopUnmarshaler struct{}

func (NopUnmarshaler) UnmarshalRESP

func (NopUnmarshaler) UnmarshalRESP(_ Value) error

type ProtocolError

type ProtocolError struct {
	Message string
}

ProtocolError is a RESP protocol error

func (*ProtocolError) Error

func (e *ProtocolError) Error() string

func (*ProtocolError) String

func (e *ProtocolError) String() string

type SimpleString

type SimpleString string

SimpleString is a RESP simple string value

func (SimpleString) AppendRESP

func (s SimpleString) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (SimpleString) Check

func (s SimpleString) Check() error

func (SimpleString) Decode

func (s SimpleString) Decode(x interface{}) error

Decode implements Value interface

func (SimpleString) Type

func (SimpleString) Type() Type

Type implements Value interface

func (*SimpleString) UnmarshalRESP

func (s *SimpleString) UnmarshalRESP(v Value) error

UnmarshalRESP implements Unmarshaler interface

type SimpleStringRecord

type SimpleStringRecord map[string]Any

func (*SimpleStringRecord) UnmarshalRESP

func (m *SimpleStringRecord) UnmarshalRESP(v Value) error

type Stream

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

func NewStream

func NewStream(r io.Reader) *Stream

func NewStreamSize

func NewStreamSize(r io.Reader, size int) *Stream

func (*Stream) CopyBulkString

func (s *Stream) CopyBulkString(w io.Writer) (n int64, err error)

func (*Stream) Decode

func (s *Stream) Decode(x interface{}) error

func (*Stream) Err

func (s *Stream) Err() error

func (*Stream) Reset

func (s *Stream) Reset(r *bufio.Reader)

type Tee

type Tee []Unmarshaler

func (Tee) Concat

func (tee Tee) Concat(u ...Unmarshaler) Tee

func (Tee) UnmarshalRESP

func (tee Tee) UnmarshalRESP(v Value) error

type Type

type Type byte

Type is the type of a RESP value

const (
	TypeSimpleString Type = '+'
	TypeError        Type = '-'
	TypeInteger      Type = ':'
	TypeBulkString   Type = '$'
	TypeArray        Type = '*'
)

Value types

func (Type) String

func (t Type) String() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalRESP(Value) error
}

Unmarshaler can unmarshal from a RESP value

func Once

func Once(dest interface{}) Unmarshaler

type Value

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

Value is handle to a value in a RESP reply.

func (Value) Any

func (v Value) Any() Any

Any returns a Any for a RESP value

func (Value) AppendRESP

func (v Value) AppendRESP(buf []byte) []byte

AppendRESP implements Appender interface

func (Value) BulkString

func (v Value) BulkString() (BulkString, bool)

BulkString returns a RESP bulk string value

func (Value) Decode

func (v Value) Decode(x interface{}) error

Decode decodes a RESP value to x

func (Value) Each

func (v Value) Each(fn func(v string) error) error

func (Value) EachKV

func (v Value) EachKV(fn func(k, v string) error) error

EachKV calls fn for each key/value pair in an array

func (Value) Err

func (v Value) Err() error

Err returns an error if the value is a RESP error value.

func (Value) Integer

func (v Value) Integer() (int64, bool)

Integer retuns the reply as int.

func (Value) IsZero

func (v Value) IsZero() bool

IsZero checks if a RESP value is the zero value

func (Value) Iter

func (v Value) Iter() Iter

Iter returns an iterator over RESP values

func (Value) Len

func (v Value) Len() int64

Len returns the number of an array value's elements.

func (Value) Null

func (v Value) Null() bool

Null checks if a value is a null value.

func (Value) NullArray

func (v Value) NullArray() bool

NullArray checks if a value is a null array

func (Value) NullBulkString

func (v Value) NullBulkString() bool

NullBulkString checks if a value is a null bulk string

func (Value) SimpleString

func (v Value) SimpleString() (string, bool)

SimpleString returns a RESP simple string value

func (Value) Type

func (v Value) Type() Type

Type returns the type of the value.

type Writer

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

Writer is a buffered writer for the RESP protocol.

It provides helper methods to write RESP messages with minimum copying and allocations. All writes are buffered up to a specified size. An explicit call to `Flush()` is required to write all data to the underlying `io.Writer`. The writer acts like `bufio.Writer` but avoids duplicate buffering during RESP protocol serialization.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new writer using the default buffer size (4096 bytes)

func NewWriterSize

func NewWriterSize(w io.Writer, size int) *Writer

NewWriterSize creates a new writer using the specified buffer size. A minimum buffer size of 512 bytes is enforced to allow enough space for RESP serialization without exceeding the buffer's capacity.

func (*Writer) Available

func (w *Writer) Available() int

Available returns the size available before data is flushed to the underlying `io.Writer`

func (*Writer) Buffered

func (w *Writer) Buffered() int

Buffered returns the size of buffered data in bytes

func (*Writer) Err

func (w *Writer) Err() error

Err returns an error of the writer

func (*Writer) Flush

func (w *Writer) Flush() error

Flush writes all buffered data to the underlying `io.Writer` and empties the buffer.

func (*Writer) Reset

func (w *Writer) Reset(dest io.Writer)

Reset resets the underlying `io.Writer`. This discards any buffered data and retains the buffer's capacity

func (*Writer) Size

func (w *Writer) Size() int

Size returns the size of the buffer in bytes

func (*Writer) WriteArray

func (w *Writer) WriteArray(i int64) error

WriteArray writes a RESP array with size `i`

func (*Writer) WriteBulkString

func (w *Writer) WriteBulkString(s string) error

WriteBulkString writes `s` as a RESP bulk string

func (*Writer) WriteBulkStringBytes

func (w *Writer) WriteBulkStringBytes(s []byte) error

WriteBulkStringBytes writes `s` as a RESP bulk string

func (*Writer) WriteBulkStringFloat

func (w *Writer) WriteBulkStringFloat(f float64) error

WriteBulkStringFloat writes `f` as a RESP bulk string

func (*Writer) WriteBulkStringInt

func (w *Writer) WriteBulkStringInt(n int64) error

WriteBulkStringInt writes `n` as a RESP bulk string

func (*Writer) WriteBulkStringNull

func (w *Writer) WriteBulkStringNull() error

WriteBulkStringNull writes a null RESP bulk string

func (*Writer) WriteBulkStringPrefix

func (w *Writer) WriteBulkStringPrefix(prefix, s string) error

WriteBulkStringPrefix writes `s` prefixed by `prefix` as a RESP bulk string

func (*Writer) WriteBulkStringUint

func (w *Writer) WriteBulkStringUint(n uint64) error

WriteBulkStringUint writes `n` as a RESP bulk string

func (*Writer) WriteError

func (w *Writer) WriteError(s string) error

WriteError writes `s` as a RESP error string

func (*Writer) WriteInteger

func (w *Writer) WriteInteger(i int64) error

WriteInteger writes `i` as a RESP integer

func (*Writer) WriteSimpleString

func (w *Writer) WriteSimpleString(s string) error

WriteSimpleString writes `s` as a RESP simple string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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