msgpack

package module
v0.0.0-...-3780be3 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2018 License: GPL-3.0 Imports: 10 Imported by: 2

README

msgpack

msgpack use a subset of MessagePack protocol, which support types:

uint8
uint16
uint32
uint64
str
bin
array

encode

func Marshal(v interface{}) ([]byte, error)

Sample code:

type TestSubStruct struct{
    V1 string
    V2 uint32
}

type TestStruct struct{
    V1 string
    V2 uint32
    V3 TestSubStruct
}

ts := TestStruct {
    V1: "testuser",
    V2: 99,
    V3: TestSubStruct{V1:"123", V2:3},
}
b, err := Marshal(ts)

// BytesToHex(b)
// dc0003da00087465737475736572ce00000063dc0002da0003313233ce00000003

decode

func Unmarshal(data []byte, dst interface{}) error

Sample code:

type TestSubStruct struct{
    V1 string
    V2 uint32
}

type TestStruct struct{
    V1 string
    V2 uint32
    V3 TestSubStruct
}

b, err := HexToBytes("dc0003da00087465737475736572ce00000063dc0002da0003313233ce00000003")

ts := TestStruct {}
err = Unmarshal(b, &ts)
fmt.Println("ts", ts)
// ts  {testuser 99 {123 3}}

Documentation

Index

Constants

View Source
const (
	//BIN16 is byte array type identifier
	BIN16 = 0xc5
	//UINT8 is uint8
	UINT8 = 0xcc
	//UINT16 is uint16
	UINT16 = 0xcd
	//UINT32 is uint32
	UINT32 = 0xce
	//UINT64 is uint64
	UINT64 = 0xcf
	//STR16 is string type identifier
	STR16 = 0xda
	//ARRAY16 is array size type identifier
	ARRAY16 = 0xdc
	//LEN_INT32 value
	LEN_INT32 = 4
	//LEN_INT64 value
	LEN_INT64 = 8
	//MAX16BIT value
	MAX16BIT = 2 << (16 - 1)
	//REGULAR_UINT7_MAX value
	REGULAR_UINT7_MAX = 2 << (7 - 1)
	//REGULAR_UINT8_MAX value
	REGULAR_UINT8_MAX = 2 << (8 - 1)
	//REGULAR_UINT16_MAX value
	REGULAR_UINT16_MAX = 2 << (16 - 1)
	//REGULAR_UINT32_MAX value
	REGULAR_UINT32_MAX = 2 << (32 - 1)

	//SPECIAL_INT8 value
	SPECIAL_INT8 = 32
	//SPECIAL_INT16 value
	SPECIAL_INT16 = 2 << (8 - 2)
	//SPECIAL_INT32 value
	SPECIAL_INT32 = 2 << (16 - 2)
	//SPECIAL_INT64 value
	SPECIAL_INT64 = 2 << (32 - 2)
)
View Source
const (
	//NEGFIXNUM is negfix maxnum
	NEGFIXNUM = 0xe0
	//FIXMAPMAX is fixmap maxnum
	FIXMAPMAX = 0x8f
	//FIXARRAYMAX is fixarray maxnum
	FIXARRAYMAX = 0x9f
	//FIXRAWMAX is fix raw max
	FIXRAWMAX = 0xbf
	//FIRSTBYTEMASK is first byte mask
	FIRSTBYTEMASK = 0xf
)

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader, dst interface{}) error

Decode is to encode message

func Encode

func Encode(w io.Writer, structs interface{}) error

Encode is to encode message

func EncodeAbi

func EncodeAbi(contractName string, method string, w io.Writer, value interface{}, abi ABI, subStructName string) error

EncodeAbi is to encode message

func EncodeAbiEx

func EncodeAbiEx(contractName string, method string, w io.Writer, value map[string]interface{}, abi ABI, subStructName string) error

EncodeAbiEx is to encode message

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal is to serialize the message

func MarshalAbi

func MarshalAbi(v interface{}, Abi *ABI, contractName string, method string) ([]byte, error)

MarshalAbi is to serialize the message

func MarshalAbiEx

func MarshalAbiEx(v map[string]interface{}, Abi *ABI, contractName string, method string) ([]byte, error)

MarshalAbiEx is to serialize the message

func PackArraySize

func PackArraySize(writer io.Writer, length uint16) (n int, err error)

PackArraySize is to pack a given value and writes it into the specified writer.

func PackBin16

func PackBin16(writer io.Writer, value []byte) (n int, err error)

PackBin16 is to pack a given value and writes it into the specified writer.

func PackStr16

func PackStr16(writer io.Writer, value string) (n int, err error)

PackStr16 is to pack a given value and writes it into the specified writer.

func PackUint16

func PackUint16(writer io.Writer, value uint16) (n int, err error)

PackUint16 is to pack a given value and writes it into the specified writer.

func PackUint32

func PackUint32(writer io.Writer, value uint32) (n int, err error)

PackUint32 is to pack a given value and writes it into the specified writer.

func PackUint64

func PackUint64(writer io.Writer, value uint64) (n int, err error)

PackUint64 is to pack a given value and writes it into the specified writer.

func PackUint8

func PackUint8(writer io.Writer, value uint8) (n int, err error)

PackUint8 is to pack a given value and writes it into the specified writer.

func Setmapval

func Setmapval(structmap map[string]interface{}, key string, val interface{})

func Unmarshal

func Unmarshal(data []byte, dst interface{}) error

Unmarshal is to unserialize the message

func UnpackArraySize

func UnpackArraySize(reader io.Reader) (size uint16, err error)

UnpackArraySize is to unpack message

func UnpackBin16

func UnpackBin16(reader io.Reader) ([]byte, error)

UnpackBin16 is to unpack message

func UnpackStr16

func UnpackStr16(reader io.Reader) (string, error)

UnpackStr16 is to unpack message

func UnpackUint16

func UnpackUint16(reader io.Reader) (v uint16, err error)

UnpackUint16 is to unpack message

func UnpackUint32

func UnpackUint32(reader io.Reader) (v uint32, err error)

UnpackUint32 is to unpack message

func UnpackUint64

func UnpackUint64(reader io.Reader) (v uint64, err error)

UnpackUint64 is to unpack message

func UnpackUint8

func UnpackUint8(reader io.Reader) (v uint8, err error)

UnpackUint8 is to unpack message

Types

type ABI

type ABI struct {
	Types   []interface{} `json:"types"`
	Structs []ABIStruct   `json:"structs"`
	Actions []ABIAction   `json:"actions"`
	Tables  []interface{} `json:"tables"`
}

ABI struct for abi

func GetAbibyContractName

func GetAbibyContractName(contractname string) (ABI, error)

GetAbibyContractName function

func ParseAbi

func ParseAbi(abiRaw []byte) (*ABI, error)

ParseAbi parse abiraw to struct for contracts

type ABIAction

type ABIAction struct {
	ActionName string `json:"action_name"`
	Type       string `json:"type"`
}

ABIAction abi Action(Method)

type ABIStruct

type ABIStruct struct {
	Name   string    `json:"name"`
	Base   string    `json:"base"`
	Fields *FeildMap `json:"fields"`
}

ABIStruct parameter struct for abi Action(Method)

type ABIStructs

type ABIStructs struct {
	Structs []struct {
		Name   string            `json:"name"`
		Base   string            `json:"base"`
		Fields map[string]string `json:"fields"`
	} `json:"structs"`
}

ABIStructs structs for ABI

type ByIndex

type ByIndex []KeyIndex

ByIndex struct

func (ByIndex) Len

func (a ByIndex) Len() int

func (ByIndex) Less

func (a ByIndex) Less(i, j int) bool

func (ByIndex) Swap

func (a ByIndex) Swap(i, j int)

type ByPair

type ByPair struct {
	Pairs    []*Pair
	LessFunc func(a *Pair, j *Pair) bool
}

ByPair function

func (ByPair) Len

func (a ByPair) Len() int

func (ByPair) Less

func (a ByPair) Less(i, j int) bool

func (ByPair) Swap

func (a ByPair) Swap(i, j int)

type Bytes

type Bytes []byte

Bytes is []byte type

type Bytes1

type Bytes1 [1]byte

Bytes1 is first

type Bytes2

type Bytes2 [2]byte

Bytes2 is second

type Bytes4

type Bytes4 [4]byte

Bytes4 is third

type Bytes8

type Bytes8 [8]byte

Bytes8 is forth

type FeildMap

type FeildMap struct {
	// contains filtered or unexported fields
}

FeildMap struct

func New

func New() *FeildMap

New new a FeildMap struct

func (*FeildMap) Delete

func (o *FeildMap) Delete(key string)

Delete delete key

func (*FeildMap) Get

func (o *FeildMap) Get(key string) (interface{}, bool)

Get get interface value by key

func (*FeildMap) GetStringPair

func (o *FeildMap) GetStringPair() []StringPair

GetStringPair get key value pairs

func (*FeildMap) GetStringVal

func (o *FeildMap) GetStringVal(key string) (string, bool)

GetStringVal get string value by key

func (*FeildMap) Keys

func (o *FeildMap) Keys() []string

Keys list all keys

func (FeildMap) MarshalJSON

func (o FeildMap) MarshalJSON() ([]byte, error)

MarshalJSON function

func (*FeildMap) Set

func (o *FeildMap) Set(key string, value interface{})

Set set interface value by key

func (*FeildMap) Sort

func (o *FeildMap) Sort(lessFunc func(a *Pair, b *Pair) bool)

Sort Sort the map using your sort func

func (*FeildMap) SortKeys

func (o *FeildMap) SortKeys(sortFunc func(keys []string))

SortKeys Sort the map keys using your sort func

func (*FeildMap) UnmarshalJSON

func (o *FeildMap) UnmarshalJSON(b []byte) error

UnmarshalJSON function

type KeyIndex

type KeyIndex struct {
	Key   string
	Index int
}

KeyIndex struct

type Pair

type Pair struct {
	// contains filtered or unexported fields
}

Pair struct

func (*Pair) Key

func (kv *Pair) Key() string

Key function

func (*Pair) Value

func (kv *Pair) Value() interface{}

Value function

type StringPair

type StringPair struct {
	Key   string
	Value string
}

StringPair struct

Jump to

Keyboard shortcuts

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