Documentation ¶
Overview ¶
go-wire is our custom codec package for serializing and deserializing data and structures as binary and JSON blobs.
In order to get started with go-wire we need to:
1) Choose the receiving structure for deserializing. It MUST be an interface{} and during registration it MUST be wrapped as a struct for example
struct { Receiver }{}
2) Decide the IDs for the respective types that we'll be dealing with. We shall call these the concrete types.
3) Register the receiving structure as well as each of the concrete types. Typically do this in the init function so that it gets run before other functions are invoked
func init() { wire.RegisterInterface( struct { Receiver }{}, wire.ConcreteType{&bcMessage{}, 0x01}, wire.ConcreteType{&bcResponse{}, 0x02}, wire.ConcreteType{&bcStatus{}, 0x03}, ) } type bcMessage struct { Content string Height int } type bcResponse struct { Status int Message string } type bcResponse struct { Status int Message string }
Encoding to binary is performed by invoking wire.WriteBinary. You'll need to provide the data to be encoded/serialized as well as where to store it and storage for the number of bytes written as well as any error encountered
var n int var err error buf := new(bytes.Buffer) bm := &bcMessage{Message: "Tendermint", Height: 100} wire.WriteBinary(bm, buf, &n, &err)
Decoding from binary is performed by invoking wire.ReadBinary. The data being decoded has to be retrieved from the decoding receiver that we previously defined i.e. Receiver for example
recv := wire.ReadBinary(struct{ Receiver }{}, buf, 0, &n, &err).(struct{ Receiver }).Receiver decoded := recv.(*bcMessage) fmt.Printf("Decoded: %#v\n", decoded)
Note that in the decoding example we used
struct { Receiver }{} --> .(struct{ Receiver }).Receiver
to receive the value. That correlates with the type that we registered in wire.RegisterInterface
Index ¶
- Constants
- Variables
- func BinaryBytes(o interface{}) []byte
- func BinaryCompare(a, b interface{}) int
- func BinaryEqual(a, b interface{}) bool
- func BinaryRipemd160(o interface{}) []byte
- func BinarySha256(o interface{}) []byte
- func ByteSliceSize(bz []byte) int
- func GetBool(buf []byte) (bool, error)
- func GetByteSlice(buf []byte) (bz []byte, n int, err error)
- func GetInt16(buf []byte) int16
- func GetInt32(buf []byte) int32
- func GetInt64(buf []byte) int64
- func GetString(buf []byte) (s string, n int, err error)
- func GetTypeFromStructDeclaration(o interface{}) reflect.Type
- func GetUint16(buf []byte) uint16
- func GetUint32(buf []byte) uint32
- func GetUint64(buf []byte) uint64
- func GetUvarint(buf []byte) (i uint, n int, err error)
- func GetVarint(buf []byte) (i int, n int, err error)
- func JSONBytes(o interface{}) []byte
- func JSONBytesPretty(o interface{}) []byte
- func MarshalBinary(o interface{}) ([]byte, error)
- func MarshalJSON(o interface{}) ([]byte, error)
- func PutBool(buf []byte, b bool)
- func PutByteSlice(buf []byte, bz []byte) (n int, err error)
- func PutInt16(buf []byte, i int16)
- func PutInt32(buf []byte, i int32)
- func PutInt64(buf []byte, i int64)
- func PutString(buf []byte, s string) (n int, err error)
- func PutUint16(buf []byte, i uint16)
- func PutUint32(buf []byte, i uint32)
- func PutUint64(buf []byte, i uint64)
- func PutUvarint(buf []byte, i uint) (n int, err error)
- func PutVarint(buf []byte, i int) (n int, err error)
- func ReadBinary(o interface{}, r io.Reader, lmt int, n *int, err *error) (res interface{})
- func ReadBinaryBytes(d []byte, ptr interface{}) error
- func ReadBinaryPtr(o interface{}, r io.Reader, lmt int, n *int, err *error) (res interface{})
- func ReadBinaryPtrLengthPrefixed(o interface{}, r io.Reader, lmt int, n *int, err *error) (res interface{})
- func ReadBool(r io.Reader, n *int, err *error) bool
- func ReadByte(r io.Reader, n *int, err *error) byte
- func ReadByteSlice(r io.Reader, lmt int, n *int, err *error) []byte
- func ReadByteSlices(r io.Reader, lmt int, n *int, err *error) [][]byte
- func ReadFloat32(r io.Reader, n *int, err *error) float32
- func ReadFloat64(r io.Reader, n *int, err *error) float64
- func ReadFull(buf []byte, r io.Reader, n *int, err *error)
- func ReadInt16(r io.Reader, n *int, err *error) int16
- func ReadInt32(r io.Reader, n *int, err *error) int32
- func ReadInt64(r io.Reader, n *int, err *error) int64
- func ReadInt8(r io.Reader, n *int, err *error) int8
- func ReadJSON(o interface{}, bytes []byte, err *error) interface{}
- func ReadJSONBytes(d []byte, ptr interface{}) (err error)
- func ReadJSONObject(o interface{}, object interface{}, err *error) interface{}
- func ReadJSONObjectPtr(o interface{}, object interface{}, err *error) interface{}
- func ReadJSONPtr(o interface{}, bytes []byte, err *error) interface{}
- func ReadString(r io.Reader, lmt int, n *int, err *error) string
- func ReadTime(r io.Reader, n *int, err *error) time.Time
- func ReadUint16(r io.Reader, n *int, err *error) uint16
- func ReadUint16s(r io.Reader, n *int, err *error) []uint16
- func ReadUint32(r io.Reader, n *int, err *error) uint32
- func ReadUint64(r io.Reader, n *int, err *error) uint64
- func ReadUint8(r io.Reader, n *int, err *error) uint8
- func ReadUvarint(r io.Reader, n *int, err *error) uint
- func ReadVarint(r io.Reader, n *int, err *error) int
- func UnmarshalBinary(bz []byte, ptr interface{}) error
- func UnmarshalJSON(bz []byte, ptr interface{}) (err error)
- func UvarintSize(i uint64) int
- func WriteBinary(o interface{}, w io.Writer, n *int, err *error)
- func WriteBinaryLengthPrefixed(o interface{}, w io.Writer, n *int, err *error)
- func WriteBool(b bool, w io.Writer, n *int, err *error)
- func WriteByte(b byte, w io.Writer, n *int, err *error)
- func WriteByteSlice(bz []byte, w io.Writer, n *int, err *error)
- func WriteByteSlices(bzz [][]byte, w io.Writer, n *int, err *error)
- func WriteFloat32(f float32, w io.Writer, n *int, err *error)
- func WriteFloat64(f float64, w io.Writer, n *int, err *error)
- func WriteInt16(i int16, w io.Writer, n *int, err *error)
- func WriteInt32(i int32, w io.Writer, n *int, err *error)
- func WriteInt64(i int64, w io.Writer, n *int, err *error)
- func WriteInt8(i int8, w io.Writer, n *int, err *error)
- func WriteJSON(o interface{}, w io.Writer, n *int, err *error)
- func WriteString(s string, w io.Writer, n *int, err *error)
- func WriteTime(t time.Time, w io.Writer, n *int, err *error)
- func WriteTo(bz []byte, w io.Writer, n *int, err *error)
- func WriteUint16(i uint16, w io.Writer, n *int, err *error)
- func WriteUint16s(iz []uint16, w io.Writer, n *int, err *error)
- func WriteUint32(i uint32, w io.Writer, n *int, err *error)
- func WriteUint64(i uint64, w io.Writer, n *int, err *error)
- func WriteUint8(i uint8, w io.Writer, n *int, err *error)
- func WriteUvarint(i uint, w io.Writer, n *int, err *error)
- func WriteVarint(i int, w io.Writer, n *int, err *error)
- type ConcreteType
- type Options
- type StructFieldInfo
- type TypeInfo
Constants ¶
const (
RFC3339Millis = "2006-01-02T15:04:05.000Z" // forced microseconds
)
const (
ReadSliceChunkSize = 1024
)
const Version = "0.7.3"
Variables ¶
var ( ErrBinaryReadOverflow = errors.New("Error: binary read overflow") ErrBinaryReadInvalidLength = errors.New("Error: binary read invalid length") ErrBinaryReadInvalidTimeNegative = errors.New("Error: binary read invalid time - negative") ErrBinaryReadInvalidTimeSubMillisecond = errors.New("Error: binary read invalid time - sub millisecond") ErrBinaryWriteOverflow = errors.New("Error: binary write overflow") )
Functions ¶
func BinaryBytes ¶
func BinaryBytes(o interface{}) []byte
func BinaryCompare ¶
func BinaryCompare(a, b interface{}) int
NOTE: does not care about the type, only the binary representation.
func BinaryEqual ¶
func BinaryEqual(a, b interface{}) bool
NOTE: does not care about the type, only the binary representation.
func BinaryRipemd160 ¶
func BinaryRipemd160(o interface{}) []byte
NOTE: The default hash function is Ripemd160.
func BinarySha256 ¶
func BinarySha256(o interface{}) []byte
NOTE: only use this if you need 32 bytes.
func ByteSliceSize ¶
Returns the total encoded size of a byteslice
func GetTypeFromStructDeclaration ¶
e.g. If o is struct{Foo}{}, return is the Foo reflection type.
func MarshalBinary ¶
func MarshalJSON ¶
func ReadBinary ¶
func ReadBinaryBytes ¶
ptr: a pointer to the object to be filled
func ReadBinaryPtr ¶
func ReadJSONBytes ¶
ptr: a pointer to the object to be filled
func ReadJSONObject ¶
func ReadJSONObject(o interface{}, object interface{}, err *error) interface{}
o is the ultimate destination, object is the result of json unmarshal
func ReadJSONObjectPtr ¶
func ReadJSONObjectPtr(o interface{}, object interface{}, err *error) interface{}
func ReadJSONPtr ¶
func ReadTime ¶
ReadTime reads an Int64 from the Reader, interprets it as the number of nanoseconds since January 1, 1970 UTC, and returns the corresponding time. If the Int64 read is less than zero, or not a multiple of a million, it sets the error and returns the default time.
func UnmarshalBinary ¶
func UnmarshalJSON ¶
func UvarintSize ¶
func WriteBinary ¶
WriteBinary is the binary encoder. Its arguments are the subject to be encoded, the writer that'll receive the encoded bytes, as well as a receiver to store the bytes written and any error encountered.
func WriteTime ¶
WriteTime writes the number of nanoseconds, with millisecond resolution, since January 1, 1970 UTC, to the Writer as an Int64. Milliseconds are used to ease compatibility with Javascript, which does not support finer resolution. NOTE: panics if the given time is less than January 1, 1970 UTC
Types ¶
type ConcreteType ¶
type ConcreteType struct { O interface{} Byte byte }
For use with the RegisterInterface declaration
type Options ¶
type Options struct { JSONName string // (JSON) Corresponding JSON field name. (override with `json=""`) JSONOmitEmpty bool // (JSON) Omit field if value is empty Varint bool // (Binary) Use length-prefixed encoding for (u)int64 Unsafe bool // (JSON/Binary) Explicitly enable support for floats or maps ZeroValue interface{} // Prototype zero object }
type StructFieldInfo ¶
type TypeInfo ¶
type TypeInfo struct { Type reflect.Type // The type // If Type is kind reflect.Interface, is registered IsRegisteredInterface bool ByteToType map[byte]reflect.Type TypeToByte map[reflect.Type]byte // If Type is kind reflect.Struct Fields []StructFieldInfo Unwrap bool // if struct has only one field and its an anonymous interface }
func GetTypeInfo ¶
func MakeTypeInfo ¶
func RegisterInterface ¶
func RegisterInterface(o interface{}, ctypes ...ConcreteType) *TypeInfo
This function should be used to register the receiving interface that will be used to decode an underlying concrete type. The interface MUST be embedded in a struct, and the interface MUST be the only field and it MUST be exported. For example:
RegisterInterface(struct{ Animal }{}, ConcreteType{&foo, 0x01})
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Data is designed to provide a standard interface and helper functions to easily allow serialization and deserialization of your data structures in both binary and json representations.
|
Data is designed to provide a standard interface and helper functions to easily allow serialization and deserialization of your data structures in both binary and json representations. |
base58
Package base58 provides base58-check encoding.
|
Package base58 provides base58-check encoding. |