mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +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
|
@ -9,6 +9,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/fxamacker/cbor/v2"
|
"github.com/fxamacker/cbor/v2"
|
||||||
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
|
||||||
"github.com/safing/portbase/formats/varint"
|
"github.com/safing/portbase/formats/varint"
|
||||||
"github.com/safing/portbase/utils"
|
"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 fmt.Errorf("dsd: failed to unpack cbor: %w, data: %s", err, utils.SafeFirst16Bytes(data))
|
||||||
}
|
}
|
||||||
return nil
|
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:
|
case GenCode:
|
||||||
genCodeStruct, ok := t.(GenCodeCompatible)
|
genCodeStruct, ok := t.(GenCodeCompatible)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -109,6 +116,11 @@ func DumpIndent(t interface{}, format SerializationFormat, indent string) ([]byt
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
case MsgPack:
|
||||||
|
data, err = msgpack.Marshal(t)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case GenCode:
|
case GenCode:
|
||||||
genCodeStruct, ok := t.(GenCodeCompatible)
|
genCodeStruct, ok := t.(GenCodeCompatible)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -121,7 +121,7 @@ func TestConversion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
compressionFormats := []CompressionFormat{AutoCompress, GZIP}
|
compressionFormats := []CompressionFormat{AutoCompress, GZIP}
|
||||||
formats := []SerializationFormat{JSON, CBOR}
|
formats := []SerializationFormat{JSON, CBOR, MsgPack}
|
||||||
|
|
||||||
for _, compression := range compressionFormats {
|
for _, compression := range compressionFormats {
|
||||||
for _, format := range formats {
|
for _, format := range formats {
|
||||||
|
@ -233,7 +233,7 @@ func TestConversion(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// test all formats
|
// test all formats
|
||||||
simplifiedFormatTesting := []SerializationFormat{JSON, CBOR, GenCode}
|
simplifiedFormatTesting := []SerializationFormat{JSON, CBOR, MsgPack, GenCode}
|
||||||
|
|
||||||
for _, format := range simplifiedFormatTesting {
|
for _, format := range simplifiedFormatTesting {
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue