resp

package
v0.0.0-...-c9fc7fd Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package resp implements the protocol of RESP(REdis Serialization Protocol)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTimeout

func IsTimeout(r *Resp) bool

IsTimeout is a helper function for determining if an IOErr Resp was caused by a network timeout

Types

type Resp

type Resp struct {
	Typ RespType
	Val interface{}

	// Err indicates that this Resp signals some kind of error, either on the
	// connection level or the application level. Use IsType if you need to
	// determine which, otherwise you can simply check if this is nil
	Err error
}

Resp represents a single response or message being sent to/from a redis server. Each Resp has a type (see RespType and IsType) and a value. Values can be retrieved using any of the casting methods on this type (e.g. Str)

func NewResp

func NewResp(v interface{}) *Resp

NewResp takes the given value and interprets it into a resp encoded byte stream

func NewRespErr

func NewRespErr(s string) *Resp

NewRespErr is a convenience method for making Resps to wrap errors

func NewRespFlattenedStrings

func NewRespFlattenedStrings(v interface{}) *Resp

NewRespFlattenedStrings is like NewResp except it looks through the given value and converts any types (except slices/maps) into strings, and flatten any embedded slices/maps into a single slice. This is useful because commands to a redis server must be given as an array of bulk strings. If the argument isn't already in a slice/map it will be wrapped so that it is written as a Array of size one

func NewRespIOErr

func NewRespIOErr(err error) *Resp

NewRespIOErr is a convenience method for making Resps to wrap io errors

func NewRespSimple

func NewRespSimple(s string) *Resp

NewRespSimple is like NewResp except it encodes its string as a resp SimpleStr type, whereas NewResp will encode all strings as BulkStr

func (*Resp) Array

func (r *Resp) Array() ([]*Resp, error)

Array returns the Resp slice encompassed by this Resp. Only valid for a Resp of type Array. If r.Err != nil that will be returned

func (*Resp) Bytes

func (r *Resp) Bytes() ([]byte, error)

Bytes returns a byte slice representing the value of the Resp. Only valid for a Resp of type Str. If r.Err != nil that will be returned.

func (*Resp) Float64

func (r *Resp) Float64() (float64, error)

Float64 returns a float64 representing the value of the Resp. Only valud for a Resp of type Str which represents an actual float. If r.Err != nil that will be returned

func (*Resp) Int

func (r *Resp) Int() (int, error)

Int returns an int representing the value of the Resp. For a Resp of type Int the integer value will be returned directly. For a Resp of type Str the string will attempt to be parsed as a base-10 integer, returning the parsing error if any. If r.Err != nil that will be returned

func (*Resp) Int64

func (r *Resp) Int64() (int64, error)

Int64 is like Int, but returns int64 instead of Int

func (*Resp) IsType

func (r *Resp) IsType(t RespType) bool

IsType returns whether or or not the reply is of a given type

isStr := r.IsType(redis.Str)

Multiple types can be checked at the same time by or'ing the desired types

isStrOrInt := r.IsType(redis.Str | redis.Int)

func (*Resp) List

func (r *Resp) List() ([]string, error)

List is a wrapper around Array which returns the result as a list of strings, calling Str() on each Resp which Array returns. Any errors encountered are immediately returned. Any Nil replies are interpreted as empty strings

func (*Resp) ListBytes

func (r *Resp) ListBytes() ([][]byte, error)

ListBytes is a wrapper around Array which returns the result as a list of byte slices, calling Bytes() on each Resp which Array returns. Any errors encountered are immediately returned. Any Nil replies are interpreted as nil

func (*Resp) Map

func (r *Resp) Map() (map[string]string, error)

Map is a wrapper around Array which returns the result as a map of strings, calling Str() on alternating key/values for the map. All value fields of type Nil will be treated as empty strings, keys must all be of type Str

func (*Resp) Str

func (r *Resp) Str() (string, error)

Str is a wrapper around Bytes which returns the result as a string instead of a byte slice

func (*Resp) String

func (r *Resp) String() string

String returns a string representation of the Resp. This method is for debugging, use Str() for reading a Str reply

func (*Resp) WriteTo

func (r *Resp) WriteTo(w io.Writer) (int64, error)

WriteTo writes the resp encoded form of the Resp to the given writer, implementing the WriterTo interface

type RespReader

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

RespReader is a wrapper around an io.Reader which will read Resp messages off of the io.Reader

func NewRespReader

func NewRespReader(r io.Reader) *RespReader

NewRespReader creates and returns a new RespReader which will read from the given io.Reader. Once passed in the io.Reader shouldn't be read from by any other processes

func (*RespReader) Read

func (rr *RespReader) Read() *Resp

ReadResp attempts to read a message object from the given io.Reader, parse it, and return a Resp representing it

type RespType

type RespType int

RespType is a field on every Resp which indicates the type of the data it contains

const (
	// RESP Simple Strings
	SimpleStr RespType = 1 << iota
	// RESP Bulk Strings
	BulkStr
	// An error which prevented reading/writing, e.g. connection close
	IOErr
	// RESP Errors, an error returned by redis, e.g. WRONGTYPE
	AppErr
	// RESP Integers
	Int
	// RESP Arrays
	Array
	// Null Bulk String
	Nil

	// Str combines both SimpleStr and BulkStr, which are considered strings to
	// the Str() method.  This is what you want to give to IsType when
	// determining if a response is a string
	Str = SimpleStr | BulkStr

	// Err combines both IOErr and AppErr, which both indicate that the Err
	// field on their Resp is filled. To determine if a Resp is an error you'll
	// most often want to simply check if the Err field on it is nil
	Err = IOErr | AppErr
)

Different RespTypes. You can check if a message is of one or more types using the IsType method on Resp @ref http://redis.io/topics/protocol

Jump to

Keyboard shortcuts

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