Add meta data type to record serialization

This commit is contained in:
Daniel 2019-07-19 21:30:51 +02:00
parent 3b68c8ea4c
commit 3bd7b8558f
5 changed files with 67 additions and 26 deletions

View file

@ -2,9 +2,10 @@ package record
import (
"bytes"
"errors"
"testing"
"github.com/safing/portbase/formats/dsd"
"github.com/safing/portbase/container"
)
func TestWrapper(t *testing.T) {
@ -24,14 +25,14 @@ func TestWrapper(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if wrapper.Format != dsd.JSON {
if wrapper.Format != JSON {
t.Error("format mismatch")
}
if !bytes.Equal(testData, wrapper.Data) {
t.Error("data mismatch")
}
encoded, err := wrapper.Marshal(wrapper, dsd.JSON)
encoded, err := wrapper.Marshal(wrapper, JSON)
if err != nil {
t.Fatal(err)
}
@ -40,6 +41,7 @@ func TestWrapper(t *testing.T) {
}
wrapper.SetMeta(&Meta{})
wrapper.meta.Update()
raw, err := wrapper.MarshalRecord(wrapper)
if err != nil {
t.Fatal(err)
@ -53,4 +55,42 @@ func TestWrapper(t *testing.T) {
t.Error("marshal mismatch")
}
// test new format
oldRaw, err := oldWrapperMarshalRecord(wrapper, wrapper)
if err != nil {
t.Fatal(err)
}
wrapper3, err := NewRawWrapper("test", "a", oldRaw)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(testData, wrapper3.Data) {
t.Error("marshal mismatch")
}
}
func oldWrapperMarshalRecord(w *Wrapper, r Record) ([]byte, error) {
if w.Meta() == nil {
return nil, errors.New("missing meta")
}
// version
c := container.New([]byte{1})
// meta
metaSection, err := w.meta.GenCodeMarshal(nil)
if err != nil {
return nil, err
}
c.AppendAsBlock(metaSection)
// data
dataSection, err := w.Marshal(r, JSON)
if err != nil {
return nil, err
}
c.Append(dataSection)
return c.CompileData(), nil
}