Documentation ¶
Overview ¶
Package encode provides encoding API definition and with several commonly used Encoder suchas uint32 and uint64 etc.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotSlice indicates it expects a slice type but not ErrNotSlice = errors.New("it is not a slice") // ErrUnknownEltType indicates a type this package does not support. ErrUnknownEltType = errors.New("element type is unknown") // ErrNotFixedSize indicates the size of value of a type can not be // determined by its type. // Such slice of interface. ErrNotFixedSize = errors.New("element type is not fixed size") )
Functions ¶
This section is empty.
Types ¶
type Bytes ¶
type Bytes struct {
Size int
}
Bytes converts a byte slice into fixed length slice. Result slice length is defined by Bytes.Size .
func (Bytes) Decode ¶
Decode copies fixed length slice out of source byte slice. The returned bytes are NOT copied.
func (Bytes) GetEncodedSize ¶
GetEncodedSize returns c.Size
type Dummy ¶ added in v0.5.10
type Dummy struct {
Size int
}
Dummy converts anything to nothing.
func (Dummy) GetEncodedSize ¶ added in v0.5.10
GetEncodedSize returns 0.
type Encoder ¶
type Encoder interface { // Convert into serialized byte stream. Encode(interface{}) []byte // Read byte stream and convert it back to typed data. Decode([]byte) (int, interface{}) // GetSize returns the size in byte after encoding v. // If v is of type this encoder can not encode, it panics. GetSize(v interface{}) int // GetEncodedSize returns size of the encoded value. // Encoded element may be var-length. // This function is used to determine element size without the need of // encoding it. GetEncodedSize([]byte) int }
A Encoder converts one element between serialized byte stream and in-memory data structure.
func GetSliceEltEncoder ¶
GetSliceEltEncoder creates a `Encoder` for type of element in slice `s`
type I16 ¶
type I16 struct{}
I16 converts int16 to slice of 2 bytes and back.
func (I16) Decode ¶
Decode converts slice of 2 bytes to int16. It returns number bytes consumed and an int16.
type I32 ¶
type I32 struct{}
I32 converts int32 to slice of 4 bytes and back.
func (I32) Decode ¶
Decode converts slice of 4 bytes to int32. It returns number bytes consumed and an int32.
type I64 ¶
type I64 struct{}
I64 converts int64 to slice of 8 bytes and back.
func (I64) Decode ¶
Decode converts slice of 8 bytes to int64. It returns number bytes consumed and an int64.
type I8 ¶ added in v0.5.10
type I8 struct{}
I8 converts int8 to slice of 1 byte and back.
func (I8) Decode ¶ added in v0.5.10
Decode converts slice of 1 byte to int8. It returns number bytes consumed and an int8.
func (I8) GetEncodedSize ¶ added in v0.5.10
GetEncodedSize returns 2.
type Int ¶ added in v0.4.1
type Int struct{}
Int converts int to slice of bytes and back.
func (Int) Decode ¶ added in v0.4.1
Decode converts slice of bytes to int. It returns number bytes consumed and an int.
func (Int) GetEncodedSize ¶ added in v0.4.1
GetEncodedSize returns native int size.
type String16 ¶
type String16 struct{}
String16 converts uint16 to slice of 2 bytes and back.
func (String16) Decode ¶
Decode converts slice of 2 bytes to uint16. It returns number bytes consumed and an uint16.
func (String16) GetEncodedSize ¶
GetEncodedSize returned size of encoded data.
type TypeEncoder ¶
type TypeEncoder struct { // Endian defines the byte order to encode a value. // By default it is binary.LittleEndian Endian binary.ByteOrder // Type is the data type to encode. Type reflect.Type // Size is the encoded size of this type. Size int }
TypeEncoder provides encoding for fixed size types. Such as int32 or struct { X int32; Y int64; }
"int" is not a fixed size type: int on different platform has different size, 4 or 8 bytes.
"[]int32" is not a fixed size type: the data size is also defined by the number of elements.
func NewTypeEncoder ¶
func NewTypeEncoder(zero interface{}) (*TypeEncoder, error)
NewTypeEncoder creates a *TypeEncoder by a value. The value "zero" defines what type this Encoder can deal with and must be a fixed size type.
func NewTypeEncoderEndian ¶
func NewTypeEncoderEndian(zero interface{}, endian binary.ByteOrder) (*TypeEncoder, error)
NewTypeEncoderEndian creates a *TypeEncoder with a specified byte order.
"endian" could be binary.LittleEndian or binary.BigEndian.
func NewTypeEncoderEndianByType ¶
NewTypeEncoderEndianByType creates a *TypeEncoder for specified type and with a specified byte order.
"endian" could be binary.LittleEndian or binary.BigEndian.
func (*TypeEncoder) Decode ¶
func (m *TypeEncoder) Decode(b []byte) (int, interface{})
Decode converts byte slice to a pointer to Type value. It returns number bytes consumed and an Type value in interface{}.
func (*TypeEncoder) Encode ¶
func (m *TypeEncoder) Encode(d interface{}) []byte
Encode converts a m.Type value to byte slice. If a different type value from the one used with NewTypeEncoder passed in, it panics.
func (*TypeEncoder) GetEncodedSize ¶
func (m *TypeEncoder) GetEncodedSize(b []byte) int
GetEncodedSize returns m.Size.
func (*TypeEncoder) GetSize ¶
func (m *TypeEncoder) GetSize(d interface{}) int
GetSize returns m.Size.
type U16 ¶
type U16 struct{}
U16 converts uint16 to slice of 2 bytes and back.
func (U16) Decode ¶
Decode converts slice of 2 bytes to uint16. It returns number bytes consumed and an uint16.
type U32 ¶
type U32 struct{}
U32 converts uint32 to slice of 4 bytes and back.
func (U32) Decode ¶
Decode converts slice of 4 bytes to uint32. It returns number bytes consumed and an uint32.