radix: github.com/mediocregopher/radix/resp/resp2 Index | Files

package resp2

import "github.com/mediocregopher/radix/resp/resp2"

Package resp2 implements the original redis RESP protocol, a plaintext protocol which is also binary safe. Redis uses the RESP protocol to communicate with its clients, but there's nothing about the protocol which ties it to redis, it could be used for almost anything.

See https://redis.io/topics/protocol for more details on the protocol.

Index

Package Files

resp.go

Variables

var (
    SimpleStringPrefix = []byte{'+'}
    ErrorPrefix        = []byte{'-'}
    IntPrefix          = []byte{':'}
    BulkStringPrefix   = []byte{'$'}
    ArrayPrefix        = []byte{'*'}
)

Enumeration of each of RESP's message types, each denoted by the prefix which is prepended to messages of that type.

In order to determine the type of a message which is being written to a *bufio.Reader, without actually consuming it, one can use the Peek method and compare it against these values.

type Any Uses

type Any struct {
    I   interface{}

    // If true then the MarshalRESP method will marshal all non-array types as
    // bulk strings. This primarily effects integers and errors.
    MarshalBulkString bool

    // If true then no array headers will be sent when MarshalRESP is called.
    // For I values which are non-arrays this means no behavior change. For
    // arrays and embedded arrays it means only the array elements will be
    // written, and an ArrayHeader must have been manually marshalled
    // beforehand.
    MarshalNoArrayHeaders bool
}

Any represents any primitive go type, such as integers, floats, strings, bools, etc... It also includes encoding.Text(Un)Marshalers and encoding.(Un)BinaryMarshalers. It will _not_ marshal resp.Marshalers.

Most things will be treated as bulk strings, except for those that have their own corresponding type in the RESP protocol (e.g. ints). strings and []bytes will always be encoded as bulk strings, never simple strings.

Arrays and slices will be treated as RESP arrays, and their values will be treated as if also wrapped in an Any struct. Maps will be similarly treated, but they will be flattened into arrays of their alternating keys/values first.

When using UnmarshalRESP the value of I must be a pointer or nil. If it is nil then the RESP value will be read and discarded.

If an error type is read in the UnmarshalRESP method then a resp2.Error will be returned with that error, and the value of I won't be touched.

func (Any) MarshalRESP Uses

func (a Any) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (Any) NumElems Uses

func (a Any) NumElems() int

NumElems returns the number of non-array elements which would be marshalled based on I. For example:

Any{I: "foo"}.NumElems() == 1
Any{I: []string{}}.NumElems() == 0
Any{I: []string{"foo"}}.NumElems() == 1
Any{I: []string{"foo", "bar"}}.NumElems() == 2
Any{I: [][]string{{"foo"}, {"bar", "baz"}, {}}}.NumElems() == 3

func (Any) UnmarshalRESP Uses

func (a Any) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method

type Array Uses

type Array struct {
    A []resp.Marshaler
}

Array represents an array of RESP elements which will be marshaled as a RESP array. If A is Nil then a Nil RESP will be marshaled.

func (Array) MarshalRESP Uses

func (a Array) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

type ArrayHeader Uses

type ArrayHeader struct {
    N int
}

ArrayHeader represents the header sent preceding array elements in the RESP protocol. It does not actually encompass any elements itself, it only declares how many elements will come after it.

An N of -1 may also be used to indicate a nil response, as per the RESP spec

func (ArrayHeader) MarshalRESP Uses

func (ah ArrayHeader) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (*ArrayHeader) UnmarshalRESP Uses

func (ah *ArrayHeader) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method

type BulkReader Uses

type BulkReader struct {
    LR resp.LenReader
}

BulkReader is like BulkString, but it only supports marshalling and will use the given LenReader to do so. If LR is nil then the nil bulk string RESP will be written

func (BulkReader) MarshalRESP Uses

func (b BulkReader) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

type BulkString Uses

type BulkString struct {
    S string
}

BulkString represents the bulk string type in the RESP protocol using a go string.

func (BulkString) MarshalRESP Uses

func (b BulkString) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (*BulkString) UnmarshalRESP Uses

func (b *BulkString) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method. This treats a Nil bulk string message as empty string.

type BulkStringBytes Uses

type BulkStringBytes struct {
    B   []byte

    // If true then this won't marshal the nil RESP value when B is nil, it will
    // marshal as an empty string instead
    MarshalNotNil bool
}

BulkStringBytes represents the bulk string type in the RESP protocol using a go byte slice. A B value of nil indicates the nil bulk string message, versus a B value of []byte{} which indicates a bulk string of length 0.

func (BulkStringBytes) MarshalRESP Uses

func (b BulkStringBytes) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (*BulkStringBytes) UnmarshalRESP Uses

func (b *BulkStringBytes) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method

type Error Uses

type Error struct {
    E error
}

Error represents an error type in the RESP protocol. Note that this only represents an actual error message being read/written on the stream, it is separate from network or parsing errors. An E value of nil is equivalent to an empty error string

func (Error) As Uses

func (e Error) As(target interface{}) bool

As implements the method for the (x)errors.As function.

func (Error) Error Uses

func (e Error) Error() string

func (Error) MarshalRESP Uses

func (e Error) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (*Error) UnmarshalRESP Uses

func (e *Error) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method

type Int Uses

type Int struct {
    I int64
}

Int represents an int type in the RESP protocol

func (Int) MarshalRESP Uses

func (i Int) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (*Int) UnmarshalRESP Uses

func (i *Int) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method

type RawMessage Uses

type RawMessage []byte

RawMessage is a Marshaler/Unmarshaler which will capture the exact raw bytes of a RESP message. When Marshaling the exact bytes of the RawMessage will be written as-is. When Unmarshaling the bytes of a single RESP message will be read into the RawMessage's bytes.

func (RawMessage) IsNil Uses

func (rm RawMessage) IsNil() bool

IsNil returns true if the contents of RawMessage are one of the nil values.

func (RawMessage) MarshalRESP Uses

func (rm RawMessage) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (RawMessage) UnmarshalInto Uses

func (rm RawMessage) UnmarshalInto(u resp.Unmarshaler) error

UnmarshalInto is a shortcut for wrapping this RawMessage in a *bufio.Reader and passing that into the given Unmarshaler's UnmarshalRESP method. Any error from calling UnmarshalRESP is returned, and the RawMessage is unaffected in all cases.

func (*RawMessage) UnmarshalRESP Uses

func (rm *RawMessage) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method

type SimpleString Uses

type SimpleString struct {
    S string
}

SimpleString represents the simple string type in the RESP protocol

func (SimpleString) MarshalRESP Uses

func (ss SimpleString) MarshalRESP(w io.Writer) error

MarshalRESP implements the Marshaler method

func (*SimpleString) UnmarshalRESP Uses

func (ss *SimpleString) UnmarshalRESP(br *bufio.Reader) error

UnmarshalRESP implements the Unmarshaler method

Package resp2 imports 11 packages (graph). Updated 2019-07-25. Refresh now. Tools for package owners.