mirror of
https://github.com/safing/portbase
synced 2025-04-17 07:59:09 +00:00
Add dsd CBOR support
This commit is contained in:
parent
52d83f6edd
commit
c2f77b0cb4
4 changed files with 37 additions and 16 deletions
|
@ -8,34 +8,36 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
|
||||
// "github.com/pkg/bson"
|
||||
|
||||
"github.com/fxamacker/cbor/v2"
|
||||
"github.com/safing/portbase/formats/varint"
|
||||
"github.com/safing/portbase/utils"
|
||||
)
|
||||
|
||||
// define types
|
||||
// Types.
|
||||
const (
|
||||
AUTO = 0
|
||||
NONE = 1
|
||||
|
||||
// special
|
||||
// Special types.
|
||||
LIST = 76 // L
|
||||
|
||||
// serialization
|
||||
// Serialization types.
|
||||
BSON = 66 // B
|
||||
CBOR = 67 // C
|
||||
GenCode = 71 // G
|
||||
JSON = 74 // J
|
||||
STRING = 83 // S
|
||||
BYTES = 88 // X
|
||||
JSON = 74 // J
|
||||
BSON = 66 // B
|
||||
GenCode = 71 // G
|
||||
|
||||
// compression
|
||||
// Compression types.
|
||||
GZIP = 90 // Z
|
||||
)
|
||||
|
||||
// define errors
|
||||
var errNoMoreSpace = errors.New("dsd: no more space left after reading dsd type")
|
||||
var errNotImplemented = errors.New("dsd: this type is not yet implemented")
|
||||
// Errors.
|
||||
var (
|
||||
errNoMoreSpace = errors.New("dsd: no more space left after reading dsd type")
|
||||
errNotImplemented = errors.New("dsd: this type is not yet implemented")
|
||||
)
|
||||
|
||||
// Load loads an dsd structured data blob into the given interface.
|
||||
func Load(data []byte, t interface{}) (interface{}, error) {
|
||||
|
@ -75,6 +77,12 @@ func LoadAsFormat(data []byte, format uint8, t interface{}) (interface{}, error)
|
|||
// return nil, err
|
||||
// }
|
||||
// return t, nil
|
||||
case CBOR:
|
||||
err := cbor.Unmarshal(data, t)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("dsd: failed to unpack cbor: %s, data: %s", err, utils.SafeFirst16Bytes(data))
|
||||
}
|
||||
return t, nil
|
||||
case GenCode:
|
||||
genCodeStruct, ok := t.(GenCodeCompatible)
|
||||
if !ok {
|
||||
|
@ -132,6 +140,11 @@ func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) {
|
|||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
case CBOR:
|
||||
data, err = cbor.Marshal(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case GenCode:
|
||||
genCodeStruct, ok := t.(GenCodeCompatible)
|
||||
if !ok {
|
||||
|
|
|
@ -3,12 +3,11 @@ package dsd
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:generate msgp
|
||||
|
||||
// SimpleTestStruct is used for testing.
|
||||
type SimpleTestStruct struct {
|
||||
S string
|
||||
|
@ -26,6 +25,7 @@ type ComplexTestStruct struct {
|
|||
UI16 uint16
|
||||
UI32 uint32
|
||||
UI64 uint64
|
||||
BI *big.Int
|
||||
S string
|
||||
Sp *string
|
||||
Sa []string
|
||||
|
@ -113,6 +113,7 @@ func TestConversion(t *testing.T) {
|
|||
3,
|
||||
4,
|
||||
5,
|
||||
big.NewInt(6),
|
||||
"a",
|
||||
&bString,
|
||||
[]string{"c", "d", "e"},
|
||||
|
@ -153,7 +154,7 @@ func TestConversion(t *testing.T) {
|
|||
}
|
||||
|
||||
// test all formats (complex)
|
||||
formats := []uint8{JSON}
|
||||
formats := []uint8{JSON, CBOR}
|
||||
|
||||
for _, format := range formats {
|
||||
|
||||
|
@ -217,6 +218,9 @@ func TestConversion(t *testing.T) {
|
|||
if complexSubject.UI64 != co.UI64 {
|
||||
t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", complexSubject.UI64, co.UI64)
|
||||
}
|
||||
if complexSubject.BI.Cmp(co.BI) != 0 {
|
||||
t.Errorf("Load (complex struct): struct.BI is not equal (%v != %v)", complexSubject.BI, co.BI)
|
||||
}
|
||||
if complexSubject.S != co.S {
|
||||
t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", complexSubject.S, co.S)
|
||||
}
|
||||
|
@ -251,7 +255,7 @@ func TestConversion(t *testing.T) {
|
|||
}
|
||||
|
||||
// test all formats
|
||||
formats = []uint8{JSON, GenCode}
|
||||
formats = []uint8{JSON, CBOR, GenCode}
|
||||
|
||||
for _, format := range formats {
|
||||
// simple
|
||||
|
|
1
go.mod
1
go.mod
|
@ -12,6 +12,7 @@ require (
|
|||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/dgraph-io/badger v1.6.2
|
||||
github.com/dgraph-io/ristretto v0.1.0 // indirect
|
||||
github.com/fxamacker/cbor/v2 v2.3.0
|
||||
github.com/go-ole/go-ole v1.2.5 // indirect
|
||||
github.com/gofrs/uuid v4.0.0+incompatible
|
||||
github.com/golang/glog v1.0.0 // indirect
|
||||
|
|
3
go.sum
3
go.sum
|
@ -79,6 +79,9 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
|
|||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/fxamacker/cbor v1.5.1 h1:XjQWBgdmQyqimslUh5r4tUGmoqzHmBFQOImkWGi2awg=
|
||||
github.com/fxamacker/cbor/v2 v2.3.0 h1:aM45YGMctNakddNNAezPxDUpv38j44Abh+hifNuqXik=
|
||||
github.com/fxamacker/cbor/v2 v2.3.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
|
|
Loading…
Add table
Reference in a new issue