README ¶
This code was copied over with many thanks from https://github.com/Joystream/parity-codec-go/tree/develop/withreflect
Chainsafe vs Joystream scale codec
- Code quality wise Chainsafe codec is better because of proper handling(no panic), use of standard interfaces, more tests and good comments.
- However Joystream codec has better usability because of the way it handles structs or unknown types by providing two interfaces.
This helped in debugging issues because structs could be debugged one field at a time when decoding and encoding. It was easy to see the progress of decoding for example by looking at already decoded struct fields and the buffere thats passed in within the decoder struct. This could have been implemented in client code for Chainsafe codec in hindsight, but just dealing with RPC execution issues was of higher priority.// Encodeable is an interface that defines a custom encoding rules for a data type. // Should be defined for structs (not pointers to them). // See OptionBool for an example implementation. type Encodeable interface { // ParityEncode encodes and write this structure into a stream ParityEncode(encoder Encoder) } // Decodeable is an interface that defines a custom encoding rules for a data type. // Should be defined for pointers to structs. // See OptionBool for an example implementation. type Decodeable interface { // ParityDecode populates this structure from a stream (overwriting the current contents), return false on failure ParityDecode(decoder Decoder) }
It's better if the good features of both could be integrated together to create a nicer library.
Documentation ¶
Index ¶
- func Reverse(b []byte)
- func ToKeyedVec(value interface{}, prependKey []byte) ([]byte, error)
- type Decodeable
- type Decoder
- func (pd Decoder) Decode(target interface{}) error
- func (pd Decoder) DecodeIntoReflectValue(target reflect.Value) error
- func (pd Decoder) DecodeOption(hasValue *bool, valuePointer interface{}) error
- func (pd Decoder) DecodeUintCompact() (*big.Int, error)
- func (pd Decoder) Read(bytes []byte) error
- func (pd Decoder) ReadOneByte() (byte, error)
- type Encodeable
- type Encoder
- type OptionBool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Reverse ¶
func Reverse(b []byte)
Reverse reverses bytes in place (manipulates the underlying array)
func ToKeyedVec ¶
ToKeyedVec replicates the behaviour of Rust's to_keyed_vec helper.
Types ¶
type Decodeable ¶
type Decodeable interface { // ParityDecode populates this structure from a stream (overwriting the current contents), return false on failure Decode(decoder Decoder) error }
Decodeable is an interface that defines a custom encoding rules for a data type. Should be defined for pointers to structs. See OptionBool for an example implementation.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is a wraper around a Reader that allows decoding data items from a stream.
func NewDecoder ¶
func (Decoder) Decode ¶
Decode takes a pointer to a decodable value and populates it from the stream.
func (Decoder) DecodeIntoReflectValue ¶
DecodeIntoReflectValue populates a writable reflect.Value from the stream
func (Decoder) DecodeOption ¶
DecodeOption decodes a optionally available value into a boolean presence field and a value.
func (Decoder) DecodeUintCompact ¶
DecodeUintCompact decodes a compact-encoded integer. See EncodeUintCompact method.
func (Decoder) ReadOneByte ¶
ReadOneByte reads a next byte from the stream. Named so to avoid a linter warning about a clash with io.ByteReader.ReadByte
type Encodeable ¶
type Encodeable interface { // ParityEncode encodes and write this structure into a stream Encode(encoder Encoder) error }
Encodeable is an interface that defines a custom encoding rules for a data type. Should be defined for structs (not pointers to them). See OptionBool for an example implementation.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder is a wrapper around a Writer that allows encoding data items to a stream. Allows passing encoding options
func NewEncoder ¶
func (Encoder) EncodeOption ¶
EncodeOption stores optionally present value to the stream.
func (Encoder) EncodeUintCompact ¶
EncodeUintCompact writes an unsigned integer to the stream using the compact encoding. A typical usage is storing the length of a collection. Definition of compact encoding: 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
xx xx xx 00 (0 ... 2**6 - 1) (u8) yL yL yL 01 / yH yH yH yL (2**6 ... 2**14 - 1) (u8, u16) low LH high zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM (2**14 ... 2**30 - 1) (u16, u32) low LMMH high nn nn nn 11 [ / zz zz zz zz ]{4 + n} (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
Rust implementation: see impl<'a> Encode for CompactRef<'a, u64>
type OptionBool ¶
type OptionBool struct {
// contains filtered or unexported fields
}
OptionBool is a structure that can store a boolean or a missing value. Note that encoding rules are slightly different from other "Option" fields.
func NewOptionBool ¶
func NewOptionBool(value bool) OptionBool
NewOptionBool creates an OptionBool with a value.
func NewOptionBoolEmpty ¶
func NewOptionBoolEmpty() OptionBool
NewOptionBoolEmpty creates an OptionBool without a value.
func (*OptionBool) Decode ¶
func (o *OptionBool) Decode(decoder Decoder) error
ParityDecode implements decoding for OptionBool as per Rust implementation.
func (OptionBool) Encode ¶
func (o OptionBool) Encode(encoder Encoder) error
ParityEncode implements encoding for OptionBool as per Rust implementation.