mirror of
https://github.com/safing/portbase
synced 2025-04-18 08:29:09 +00:00
Update dsd to new splitted format
This commit is contained in:
parent
d3dcee7075
commit
d7e0602548
2 changed files with 22 additions and 21 deletions
formats/dsd
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
// "github.com/pkg/bson"
|
||||
|
||||
"github.com/Safing/safing-core/formats/varint"
|
||||
"github.com/Safing/portbase/formats/varint"
|
||||
)
|
||||
|
||||
// define types
|
||||
|
@ -31,7 +31,6 @@ var errUnknownType = errors.New("dsd: tried to unpack unknown type")
|
|||
var errNotImplemented = errors.New("dsd: this type is not yet implemented")
|
||||
|
||||
func Load(data []byte, t interface{}) (interface{}, error) {
|
||||
|
||||
if len(data) < 2 {
|
||||
return nil, errNoMoreSpace
|
||||
}
|
||||
|
@ -44,34 +43,36 @@ func Load(data []byte, t interface{}) (interface{}, error) {
|
|||
return nil, errNoMoreSpace
|
||||
}
|
||||
|
||||
return LoadAsFormat(data[read:], format, t)
|
||||
}
|
||||
|
||||
func LoadAsFormat(data []byte, format uint8, t interface{}) (interface{}, error) {
|
||||
switch format {
|
||||
case STRING:
|
||||
return string(data[read:]), nil
|
||||
return string(data), nil
|
||||
case BYTES:
|
||||
r := data[read:]
|
||||
return &r, nil
|
||||
return data, nil
|
||||
case JSON:
|
||||
err := json.Unmarshal(data[read:], t)
|
||||
err := json.Unmarshal(data, t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return t, nil
|
||||
// case BSON:
|
||||
// err := bson.Unmarshal(data[read:], t)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return t, nil
|
||||
// case MSGP:
|
||||
// err := t.UnmarshalMsg(data[read:])
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return t, nil
|
||||
// case BSON:
|
||||
// err := bson.Unmarshal(data[read:], t)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return t, nil
|
||||
// case MSGP:
|
||||
// err := t.UnmarshalMsg(data[read:])
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return t, nil
|
||||
default:
|
||||
return nil, errors.New(fmt.Sprintf("dsd: tried to load unknown type %d, data: %v", format, data))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Dump(t interface{}, format uint8) ([]byte, error) {
|
||||
|
|
|
@ -66,9 +66,9 @@ func TestConversion(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Load error (string): %s", err)
|
||||
}
|
||||
tb := b.(*[]byte)
|
||||
tb := b.([]byte)
|
||||
|
||||
if !bytes.Equal(*tb, []byte("def")) {
|
||||
if !bytes.Equal(tb, []byte("def")) {
|
||||
t.Errorf("Load (string): subject and loaded object are not equal (%v != %v)", tb, []byte("def"))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue