mirror of
https://github.com/safing/portbase
synced 2025-04-17 07:59:09 +00:00
Add support for MsgPack dsd format
This commit is contained in:
parent
8813102b7b
commit
6cde860324
2 changed files with 14 additions and 2 deletions
formats/dsd
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue