encode

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: MIT Imports: 6 Imported by: 1

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

View Source
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

func (c Bytes) Decode(b []byte) (int, interface{})

Decode copies fixed length slice out of source byte slice. The returned bytes are NOT copied.

func (Bytes) Encode

func (c Bytes) Encode(d interface{}) []byte

Encode converts byte slice to byte slice.

func (Bytes) GetEncodedSize

func (c Bytes) GetEncodedSize(b []byte) int

GetEncodedSize returns c.Size

func (Bytes) GetSize

func (c Bytes) GetSize(d interface{}) int

GetSize returns the length: c.Size.

type Dummy added in v0.5.10

type Dummy struct {
	Size int
}

Dummy converts anything to nothing.

func (Dummy) Decode added in v0.5.10

func (c Dummy) Decode(b []byte) (int, interface{})

Decode always returns nil.

func (Dummy) Encode added in v0.5.10

func (c Dummy) Encode(d interface{}) []byte

Encode converts something to empty byte slice.

func (Dummy) GetEncodedSize added in v0.5.10

func (c Dummy) GetEncodedSize(b []byte) int

GetEncodedSize returns 0.

func (Dummy) GetSize added in v0.5.10

func (c Dummy) GetSize(d interface{}) int

GetSize 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 EncoderByKind

func EncoderByKind(k reflect.Kind) (Encoder, error)

func EncoderOf

func EncoderOf(e interface{}) (Encoder, error)

EncoderOf returns a `Encoder` implementation for type of `e`

func GetSliceEltEncoder

func GetSliceEltEncoder(s interface{}) (Encoder, error)

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

func (c I16) Decode(b []byte) (int, interface{})

Decode converts slice of 2 bytes to int16. It returns number bytes consumed and an int16.

func (I16) Encode

func (c I16) Encode(d interface{}) []byte

Encode converts int16 to slice of 2 bytes.

func (I16) GetEncodedSize

func (c I16) GetEncodedSize(b []byte) int

GetEncodedSize returns 2.

func (I16) GetSize

func (c I16) GetSize(d interface{}) int

GetSize returns the size in byte after encoding v.

type I32

type I32 struct{}

I32 converts int32 to slice of 4 bytes and back.

func (I32) Decode

func (c I32) Decode(b []byte) (int, interface{})

Decode converts slice of 4 bytes to int32. It returns number bytes consumed and an int32.

func (I32) Encode

func (c I32) Encode(d interface{}) []byte

Encode converts int32 to slice of 4 bytes.

func (I32) GetEncodedSize

func (c I32) GetEncodedSize(b []byte) int

GetEncodedSize returns 4.

func (I32) GetSize

func (c I32) GetSize(d interface{}) int

GetSize returns the size in byte after encoding v.

type I64

type I64 struct{}

I64 converts int64 to slice of 8 bytes and back.

func (I64) Decode

func (c I64) Decode(b []byte) (int, interface{})

Decode converts slice of 8 bytes to int64. It returns number bytes consumed and an int64.

func (I64) Encode

func (c I64) Encode(d interface{}) []byte

Encode converts int64 to slice of 8 bytes.

func (I64) GetEncodedSize

func (c I64) GetEncodedSize(b []byte) int

GetEncodedSize returns 8.

func (I64) GetSize

func (c I64) GetSize(d interface{}) int

GetSize returns the size in byte after encoding v.

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

func (c I8) Decode(b []byte) (int, interface{})

Decode converts slice of 1 byte to int8. It returns number bytes consumed and an int8.

func (I8) Encode added in v0.5.10

func (c I8) Encode(d interface{}) []byte

Encode converts int8 to slice of 1 byte.

func (I8) GetEncodedSize added in v0.5.10

func (c I8) GetEncodedSize(b []byte) int

GetEncodedSize returns 2.

func (I8) GetSize added in v0.5.10

func (c I8) GetSize(d interface{}) int

GetSize returns the size in byte after encoding v.

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

func (c Int) Decode(b []byte) (int, interface{})

Decode converts slice of bytes to int. It returns number bytes consumed and an int.

func (Int) Encode added in v0.4.1

func (c Int) Encode(d interface{}) []byte

Encode converts int to slice of bytes.

func (Int) GetEncodedSize added in v0.4.1

func (c Int) GetEncodedSize(b []byte) int

GetEncodedSize returns native int size.

func (Int) GetSize added in v0.4.1

func (c Int) GetSize(d interface{}) int

GetSize returns native int size in byte after encoding v.

type String16

type String16 struct{}

String16 converts uint16 to slice of 2 bytes and back.

func (String16) Decode

func (s String16) Decode(b []byte) (int, interface{})

Decode converts slice of 2 bytes to uint16. It returns number bytes consumed and an uint16.

func (String16) Encode

func (s String16) Encode(d interface{}) []byte

Encode converts uint16 to slice of 2 bytes.

func (String16) GetEncodedSize

func (s String16) GetEncodedSize(b []byte) int

GetEncodedSize returned size of encoded data.

func (String16) GetSize

func (s String16) GetSize(d interface{}) int

GetSize returns number of byte required to encode a string. It is len(str) + 2;

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

func NewTypeEncoderEndianByType(t reflect.Type, endian binary.ByteOrder) (*TypeEncoder, error)

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

func (c U16) Decode(b []byte) (int, interface{})

Decode converts slice of 2 bytes to uint16. It returns number bytes consumed and an uint16.

func (U16) Encode

func (c U16) Encode(d interface{}) []byte

Encode converts uint16 to slice of 2 bytes.

func (U16) GetEncodedSize

func (c U16) GetEncodedSize(b []byte) int

GetEncodedSize returns 2.

func (U16) GetSize

func (c U16) GetSize(d interface{}) int

GetSize returns the size in byte after encoding v.

type U32

type U32 struct{}

U32 converts uint32 to slice of 4 bytes and back.

func (U32) Decode

func (c U32) Decode(b []byte) (int, interface{})

Decode converts slice of 4 bytes to uint32. It returns number bytes consumed and an uint32.

func (U32) Encode

func (c U32) Encode(d interface{}) []byte

Encode converts uint32 to slice of 4 bytes.

func (U32) GetEncodedSize

func (c U32) GetEncodedSize(b []byte) int

GetEncodedSize returns 4.

func (U32) GetSize

func (c U32) GetSize(d interface{}) int

GetSize returns the size in byte after encoding v.

type U64

type U64 struct{}

U64 converts uint64 to slice of 8 bytes and back.

func (U64) Decode

func (c U64) Decode(b []byte) (int, interface{})

Decode converts slice of 8 bytes to uint64. It returns number bytes consumed and an uint64.

func (U64) Encode

func (c U64) Encode(d interface{}) []byte

Encode converts uint64 to slice of 8 bytes.

func (U64) GetEncodedSize

func (c U64) GetEncodedSize(b []byte) int

GetEncodedSize returns 8.

func (U64) GetSize

func (c U64) GetSize(d interface{}) int

GetSize returns the size in byte after encoding v.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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