1
0
Fork 0
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:
Henrik Hautakoski 2024-01-07 14:31:11 +01:00
parent 6801b536bf
commit 8ffd86daac
4 changed files with 41 additions and 38 deletions

View file

@ -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())
}