mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
api/message: for codec implementations, define a function instead of global variables.
This commit is contained in:
parent
6801b536bf
commit
8ffd86daac
4 changed files with 41 additions and 38 deletions
|
|
@ -8,27 +8,28 @@ import (
|
|||
"github.com/eosswedenorg/thalos/api/message"
|
||||
)
|
||||
|
||||
var mh codec.MsgpackHandle
|
||||
|
||||
func encode(a any) ([]byte, error) {
|
||||
var b []byte
|
||||
return b, codec.NewEncoderBytes(&b, &mh).Encode(a)
|
||||
}
|
||||
|
||||
func decode(b []byte, a any) error {
|
||||
return codec.NewDecoderBytes(b, &mh).Decode(a)
|
||||
}
|
||||
|
||||
func init() {
|
||||
mh.MapType = reflect.TypeOf(map[string]any(nil))
|
||||
mh.Canonical = true
|
||||
func createCodec() message.Codec {
|
||||
// Create handler.
|
||||
handle := codec.MsgpackHandle{}
|
||||
handle.MapType = reflect.TypeOf(map[string]any(nil))
|
||||
handle.Canonical = true
|
||||
|
||||
// Wierd name but this is needed for the newest version of msgpack
|
||||
// that has support for time and string datatypes etc.
|
||||
mh.WriteExt = true
|
||||
handle.WriteExt = true
|
||||
|
||||
message.RegisterCodec("msgpack", message.Codec{
|
||||
Encoder: encode,
|
||||
Decoder: decode,
|
||||
})
|
||||
return message.Codec{
|
||||
Encoder: func(a any) ([]byte, error) {
|
||||
var b []byte
|
||||
return b, codec.NewEncoderBytes(&b, &handle).Encode(a)
|
||||
},
|
||||
Decoder: func(b []byte, a any) error {
|
||||
return codec.NewDecoderBytes(b, &handle).Decode(a)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Register codec.
|
||||
message.RegisterCodec("msgpack", createCodec())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ func TestMsgpack_EncodeActionTrace(t *testing.T) {
|
|||
Return: []byte{0xde, 0xad, 0xbe, 0xef},
|
||||
}
|
||||
|
||||
data, err := encode(msg)
|
||||
data, err := createCodec().Encoder(msg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := []byte{
|
||||
|
|
@ -177,7 +177,7 @@ func TestMsgpack_DecodeActionTrace(t *testing.T) {
|
|||
}
|
||||
|
||||
res := message.ActionTrace{}
|
||||
err := decode(data, &res)
|
||||
err := createCodec().Decoder(data, &res)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expected, res)
|
||||
|
|
@ -190,7 +190,7 @@ func TestMsgpack_EncodeHeartbeat(t *testing.T) {
|
|||
LastIrreversibleBlockNum: 1236,
|
||||
}
|
||||
|
||||
data, err := encode(msg)
|
||||
data, err := createCodec().Encoder(msg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, data, []byte{0x83, 0xa8, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x6e, 0x75, 0x6d, 0xcd, 0x4, 0xd2, 0xad, 0x68, 0x65, 0x61, 0x64, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x6e, 0x75, 0x6d, 0xcd, 0x4, 0xd3, 0xba, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x69, 0x72, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x6e, 0x75, 0x6d, 0xcd, 0x4, 0xd4})
|
||||
|
|
@ -206,7 +206,7 @@ func TestMsgpack_DecodeHeartbeat(t *testing.T) {
|
|||
}
|
||||
|
||||
msg := message.HeartBeat{}
|
||||
err := decode(data, &msg)
|
||||
err := createCodec().Decoder(data, &msg)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, msg)
|
||||
}
|
||||
|
|
@ -265,7 +265,7 @@ func TestMsgpack_EncodeTransactionTrace(t *testing.T) {
|
|||
Error: 2,
|
||||
}
|
||||
|
||||
data, err := encode(msg)
|
||||
data, err := createCodec().Encoder(msg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := []byte{
|
||||
|
|
@ -406,7 +406,7 @@ func TestMsgpack_DecodeTransactionTrace(t *testing.T) {
|
|||
}
|
||||
|
||||
res := message.TransactionTrace{}
|
||||
err := decode(data, &res)
|
||||
err := createCodec().Decoder(data, &res)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expected, res)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue