Add support for MsgPack dsd format

This commit is contained in:
Daniel 2021-11-22 14:49:50 +01:00
parent 8813102b7b
commit 6cde860324
2 changed files with 14 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import (
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/vmihailenco/msgpack/v5"
"github.com/safing/portbase/formats/varint"
"github.com/safing/portbase/utils"
@ -45,6 +46,12 @@ func LoadAsFormat(data []byte, format SerializationFormat, t interface{}) (err e
return fmt.Errorf("dsd: failed to unpack cbor: %w, data: %s", err, utils.SafeFirst16Bytes(data))
}
return nil
case MsgPack:
err = msgpack.Unmarshal(data, t)
if err != nil {
return fmt.Errorf("dsd: failed to unpack msgpack: %w, data: %s", err, utils.SafeFirst16Bytes(data))
}
return nil
case GenCode:
genCodeStruct, ok := t.(GenCodeCompatible)
if !ok {
@ -109,6 +116,11 @@ func DumpIndent(t interface{}, format SerializationFormat, indent string) ([]byt
if err != nil {
return nil, err
}
case MsgPack:
data, err = msgpack.Marshal(t)
if err != nil {
return nil, err
}
case GenCode:
genCodeStruct, ok := t.(GenCodeCompatible)
if !ok {

View file

@ -121,7 +121,7 @@ func TestConversion(t *testing.T) {
t.Parallel()
compressionFormats := []CompressionFormat{AutoCompress, GZIP}
formats := []SerializationFormat{JSON, CBOR}
formats := []SerializationFormat{JSON, CBOR, MsgPack}
for _, compression := range compressionFormats {
for _, format := range formats {
@ -233,7 +233,7 @@ func TestConversion(t *testing.T) {
}
// test all formats
simplifiedFormatTesting := []SerializationFormat{JSON, CBOR, GenCode}
simplifiedFormatTesting := []SerializationFormat{JSON, CBOR, MsgPack, GenCode}
for _, format := range simplifiedFormatTesting {