Update dsd to new splitted format

This commit is contained in:
Daniel 2018-09-27 15:58:05 +02:00
parent d3dcee7075
commit d7e0602548
2 changed files with 22 additions and 21 deletions

View file

@ -12,7 +12,7 @@ import (
// "github.com/pkg/bson" // "github.com/pkg/bson"
"github.com/Safing/safing-core/formats/varint" "github.com/Safing/portbase/formats/varint"
) )
// define types // 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") var errNotImplemented = errors.New("dsd: this type is not yet implemented")
func Load(data []byte, t interface{}) (interface{}, error) { func Load(data []byte, t interface{}) (interface{}, error) {
if len(data) < 2 { if len(data) < 2 {
return nil, errNoMoreSpace return nil, errNoMoreSpace
} }
@ -44,14 +43,17 @@ func Load(data []byte, t interface{}) (interface{}, error) {
return nil, errNoMoreSpace return nil, errNoMoreSpace
} }
return LoadAsFormat(data[read:], format, t)
}
func LoadAsFormat(data []byte, format uint8, t interface{}) (interface{}, error) {
switch format { switch format {
case STRING: case STRING:
return string(data[read:]), nil return string(data), nil
case BYTES: case BYTES:
r := data[read:] return data, nil
return &r, nil
case JSON: case JSON:
err := json.Unmarshal(data[read:], t) err := json.Unmarshal(data, t)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -71,7 +73,6 @@ func Load(data []byte, t interface{}) (interface{}, error) {
default: default:
return nil, errors.New(fmt.Sprintf("dsd: tried to load unknown type %d, data: %v", format, data)) 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) { func Dump(t interface{}, format uint8) ([]byte, error) {

View file

@ -66,9 +66,9 @@ func TestConversion(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Load error (string): %s", err) 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")) t.Errorf("Load (string): subject and loaded object are not equal (%v != %v)", tb, []byte("def"))
} }