Documentation
¶
Overview ¶
Package dwire implements the DDRP message encoding scheme as defined in PIP-1.
Fundamental types:
- bool: Encoded as 0x00 or 0x01 if the value is true or false, respectively.
- uint8: Encoded as a single byte in the range 0x00-0xff.
- uint16: Encoded as two big-endian bytes in the range 0x0000-0xffff.
- uint32: Encoded as four big-endian bytes in the range 0x00000000-0xffffffff
- uint64: Encoded as eight big-endian bytes in the range 0x0000000000000000-0xffffffffffffffff.
- string: Encoded as a UTF-8 []byte.
- [N]T: Encoded as the concatenation of the encoding of T.
- []T: Encoded as a binary.Uvarint length prefix followed by the concatenation of the encoding of T.
Well-known types:
- time.Time: Encoded as uint32(time.Unix()).
The easiest way to use this library is to call the EncodeField/DecodeField family of methods. To encode a value into a Writer:
value1 := "this is my value" value2 := 2 err := dwire.EncodeFields(w, value1, value2)
To decode a value from a reader:
var value1 string var value2 string err := dwire.DecodeFields(r, &value1, &value2)
Note that values passed to DecodeField/DecodeFields MUST be pointers.
dwire exposes Encoder and Decoder interfaces, which allow arbitrary types to be encoded and decoded by EncodeField/DecodeField. For example:
type Foo struct { Value string } func (f *Foo) Encode(w io.Writer) error { return dwire.EncodeFields(w, f.Value) } func (f *Foo) Decode(r io.Reader) error { return dwire.DecodeFields(r, &f.Value) }
Index ¶
- Constants
- func DecodeField(r io.Reader, item interface{}) error
- func DecodeFields(r io.Reader, items ...interface{}) error
- func DecodeTime(r io.Reader, val interface{}) error
- func EncodeField(w io.Writer, item interface{}) error
- func EncodeFields(w io.Writer, items ...interface{}) error
- func EncodeTime(w io.Writer, val interface{}) error
- type ConfiguredEncoder
- func (c *ConfiguredEncoder) DecodeField(r io.Reader, item interface{}) error
- func (c *ConfiguredEncoder) DecodeFields(r io.Reader, items ...interface{}) error
- func (c *ConfiguredEncoder) EncodeField(w io.Writer, item interface{}) error
- func (c *ConfiguredEncoder) EncodeFields(w io.Writer, items ...interface{}) error
- type Decoder
- type EncodeDecoder
- type Encoder
Constants ¶
const ( DefaultMaxVariableArrayLen = 1024 DefaultMaxByteFieldLen = 256 * 1024 )
Variables ¶
This section is empty.
Functions ¶
func DecodeField ¶
DecodeFields decodes the field in the item argument from the Reader using the default Encoder. The item provided to DecodeField must be a pointer type.
func DecodeFields ¶
DecodeFields decodes each field in the variadic items argument from the Reader using the default Encoder. Items provided to DecodeFields must be pointer types.
func DecodeTime ¶
Decodes a time.Time value from the Reader. Since time.Time is a well-known type, you likely do not need to call this method directly. Instead, call EncodeField or EncodeFields with a &time.Time item.
func EncodeField ¶
EncodeField encodes a single field into the Writer using the default Encoder.
func EncodeFields ¶
EncodeFields encodes each field in the variadic items argument into the Writer using the default Encoder.
func EncodeTime ¶
Encodes a time.Time value into the Writer. Since time.Time is a well-known type, you likely do not need to call this method directly. Instead, provide the time value to EncodeField or EncodeFields.
Types ¶
type ConfiguredEncoder ¶
type ConfiguredEncoder struct { // MaxVariableArrayLen is the maximum length of a variable-length array dwire // will decode before stopping early. MaxVariableArrayLen int // MaxByteFieldLen is the maximum length of a variable-length byte array field // dwire will decode before stopping early. MaxByteFieldLen uint64 }
func (*ConfiguredEncoder) DecodeField ¶
func (c *ConfiguredEncoder) DecodeField(r io.Reader, item interface{}) error
DecodeField decodes the field in the item argument from the Reader. The item provided to DecodeField must be a pointer type.
func (*ConfiguredEncoder) DecodeFields ¶
func (c *ConfiguredEncoder) DecodeFields(r io.Reader, items ...interface{}) error
DecodeFields decodes each field in the variadic items argument from the Reader. Items provided to DecodeFields must be pointer types.
func (*ConfiguredEncoder) EncodeField ¶
func (c *ConfiguredEncoder) EncodeField(w io.Writer, item interface{}) error
EncodeFields encodes a single field into the Writer.
func (*ConfiguredEncoder) EncodeFields ¶
func (c *ConfiguredEncoder) EncodeFields(w io.Writer, items ...interface{}) error
EncodeFields encodes each field in the variadic items argument into the Writer.
type Decoder ¶
Decoder is an interface that allows arbitrary types to be decoded. Types implementing the Decoder interface can be decoded using DecodeField or DecodeFields.