resp2

package
v0.0.0-...-ae4e79a Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2018 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any

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

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

MarshalRESP implements the Marshaler method

func (Any) NumElems

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() == 2 Any{I: []string{"foo", "bar"}}.NumElems() == 2 Any{I: [][]string{{"foo"}, {"bar", "baz"}, {}}}.NumElems() == 3

func (Any) UnmarshalRESP

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

UnmarshalRESP implements the Unmarshaler method

type Array

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

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

MarshalRESP implements the Marshaler method

type ArrayHeader

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

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

MarshalRESP implements the Marshaler method

func (*ArrayHeader) UnmarshalRESP

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

UnmarshalRESP implements the Unmarshaler method

type BulkReader

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

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

MarshalRESP implements the Marshaler method

type BulkString

type BulkString struct {
	S string
}

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

func (BulkString) MarshalRESP

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

MarshalRESP implements the Marshaler method

func (*BulkString) UnmarshalRESP

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

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

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

MarshalRESP implements the Marshaler method

func (*BulkStringBytes) UnmarshalRESP

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

UnmarshalRESP implements the Unmarshaler method

type Error

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) Error

func (e Error) Error() string

func (Error) MarshalRESP

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

MarshalRESP implements the Marshaler method

func (*Error) UnmarshalRESP

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

UnmarshalRESP implements the Unmarshaler method

type Int

type Int struct {
	I int64
}

Int represents an int type in the RESP protocol

func (Int) MarshalRESP

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

MarshalRESP implements the Marshaler method

func (*Int) UnmarshalRESP

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

UnmarshalRESP implements the Unmarshaler method

type RawMessage

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

func (rm RawMessage) IsNil() bool

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

func (RawMessage) MarshalRESP

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

MarshalRESP implements the Marshaler method

func (RawMessage) UnmarshalInto

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

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

UnmarshalRESP implements the Unmarshaler method

type SimpleString

type SimpleString struct {
	S string
}

SimpleString represents the simple string type in the RESP protocol

func (SimpleString) MarshalRESP

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

MarshalRESP implements the Marshaler method

func (*SimpleString) UnmarshalRESP

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

UnmarshalRESP implements the Unmarshaler method

Jump to

Keyboard shortcuts

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