mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Fix bug in wrapper, remove deprecated code
This commit is contained in:
parent
a1e3817fd0
commit
35ab2be6a0
3 changed files with 14 additions and 70 deletions
|
@ -1,16 +1,10 @@
|
||||||
package record
|
package record
|
||||||
|
|
||||||
import "sync"
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type TestRecord struct {
|
type TestRecord struct {
|
||||||
Base
|
Base
|
||||||
lock sync.Mutex
|
sync.Mutex
|
||||||
}
|
|
||||||
|
|
||||||
func (tm *TestRecord) Lock() {
|
|
||||||
tm.lock.Lock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tm *TestRecord) Unlock() {
|
|
||||||
tm.lock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,27 +37,19 @@ func NewRawWrapper(database, key string, data []byte) (*Wrapper, error) {
|
||||||
offset += n
|
offset += n
|
||||||
|
|
||||||
newMeta := &Meta{}
|
newMeta := &Meta{}
|
||||||
if len(metaSection) == 34 && metaSection[4] == 0 {
|
_, err = dsd.Load(metaSection, newMeta)
|
||||||
// TODO: remove in 2020
|
if err != nil {
|
||||||
// backward compatibility:
|
return nil, fmt.Errorf("could not unmarshal meta section: %s", err)
|
||||||
// format would byte shift and populate metaSection[4] with value > 0 (would naturally populate >0 at 07.02.2106 07:28:15)
|
|
||||||
// this must be gencode without format
|
|
||||||
_, err = newMeta.GenCodeUnmarshal(metaSection)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not unmarshal meta section: %s", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_, err = dsd.Load(metaSection, newMeta)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not unmarshal meta section: %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
format, n, err := varint.Unpack8(data[offset:])
|
var format uint8 = dsd.NONE
|
||||||
if err != nil {
|
if !newMeta.IsDeleted() {
|
||||||
return nil, fmt.Errorf("could not get dsd format: %s", err)
|
format, n, err = varint.Unpack8(data[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not get dsd format: %s", err)
|
||||||
|
}
|
||||||
|
offset += n
|
||||||
}
|
}
|
||||||
offset += n
|
|
||||||
|
|
||||||
return &Wrapper{
|
return &Wrapper{
|
||||||
Base{
|
Base{
|
||||||
|
|
|
@ -2,10 +2,7 @@ package record
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/safing/portbase/container"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWrapper(t *testing.T) {
|
func TestWrapper(t *testing.T) {
|
||||||
|
@ -54,43 +51,4 @@ func TestWrapper(t *testing.T) {
|
||||||
if !bytes.Equal(testData, wrapper2.Data) {
|
if !bytes.Equal(testData, wrapper2.Data) {
|
||||||
t.Error("marshal mismatch")
|
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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue