mirror of
https://github.com/safing/portbase
synced 2025-04-18 00:19:09 +00:00
Add compression and DumpIndent
This commit is contained in:
parent
cac3c2b2e5
commit
146b6724cc
3 changed files with 383 additions and 256 deletions
formats/dsd
104
formats/dsd/compression.go
Normal file
104
formats/dsd/compression.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
package dsd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/safing/portbase/formats/varint"
|
||||
)
|
||||
|
||||
// DumpAndCompress stores the interface as a dsd formatted data structure and compresses the resulting data.
|
||||
func DumpAndCompress(t interface{}, format uint8, compression uint8) ([]byte, error) {
|
||||
data, err := Dump(t, format)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// handle special cases
|
||||
switch compression {
|
||||
case NONE:
|
||||
return data, nil
|
||||
case AUTO:
|
||||
compression = GZIP
|
||||
}
|
||||
|
||||
// prepare writer
|
||||
packetFormat := varint.Pack8(compression)
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Write(packetFormat)
|
||||
|
||||
// compress
|
||||
switch compression {
|
||||
case GZIP:
|
||||
// create gzip writer
|
||||
gzipWriter, err := gzip.NewWriterLevel(buf, gzip.BestCompression)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// write data
|
||||
n, err := gzipWriter.Write(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n != len(data) {
|
||||
return nil, errors.New("failed to fully write to gzip compressor")
|
||||
}
|
||||
|
||||
// flush and write gzip footer
|
||||
err = gzipWriter.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("dsd: tried to compress with unknown format %d", format)
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// DecompressAndLoad decompresses the data using the specified compression format and then loads the resulting data blob into the interface.
|
||||
func DecompressAndLoad(data []byte, format uint8, t interface{}) (interface{}, error) {
|
||||
// prepare reader
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
// decompress
|
||||
switch format {
|
||||
case GZIP:
|
||||
// create gzip reader
|
||||
gzipReader, err := gzip.NewReader(bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// read uncompressed data
|
||||
_, err = buf.ReadFrom(gzipReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// flush and verify gzip footer
|
||||
err = gzipReader.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("dsd: tried to dump with unknown format %d", format)
|
||||
}
|
||||
|
||||
// assign decompressed data
|
||||
data = buf.Bytes()
|
||||
|
||||
// get format
|
||||
format, read, err := varint.Unpack8(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(data) <= read {
|
||||
return nil, errNoMoreSpace
|
||||
}
|
||||
|
||||
return LoadAsFormat(data[read:], format, t)
|
||||
}
|
|
@ -15,12 +15,21 @@ import (
|
|||
|
||||
// define types
|
||||
const (
|
||||
AUTO = 0
|
||||
AUTO = 0
|
||||
NONE = 1
|
||||
|
||||
// special
|
||||
LIST = 76 // L
|
||||
|
||||
// serialization
|
||||
STRING = 83 // S
|
||||
BYTES = 88 // X
|
||||
JSON = 74 // J
|
||||
BSON = 66 // B
|
||||
GenCode = 71 // G
|
||||
|
||||
// compression
|
||||
GZIP = 90 // Z
|
||||
)
|
||||
|
||||
// define errors
|
||||
|
@ -29,10 +38,6 @@ var 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) {
|
||||
if len(data) < 2 {
|
||||
return nil, errNoMoreSpace
|
||||
}
|
||||
|
||||
format, read, err := varint.Unpack8(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -41,7 +46,12 @@ func Load(data []byte, t interface{}) (interface{}, error) {
|
|||
return nil, errNoMoreSpace
|
||||
}
|
||||
|
||||
return LoadAsFormat(data[read:], format, t)
|
||||
switch format {
|
||||
case GZIP:
|
||||
return DecompressAndLoad(data[read:], format, t)
|
||||
default:
|
||||
return LoadAsFormat(data[read:], format, t)
|
||||
}
|
||||
}
|
||||
|
||||
// LoadAsFormat loads a data blob into the interface using the specified format.
|
||||
|
@ -81,6 +91,11 @@ func LoadAsFormat(data []byte, format uint8, t interface{}) (interface{}, error)
|
|||
|
||||
// Dump stores the interface as a dsd formatted data structure.
|
||||
func Dump(t interface{}, format uint8) ([]byte, error) {
|
||||
return DumpIndent(t, format, "")
|
||||
}
|
||||
|
||||
// DumpIndent stores the interface as a dsd formatted data structure with indentation, if available.
|
||||
func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) {
|
||||
if format == AUTO {
|
||||
switch t.(type) {
|
||||
case string:
|
||||
|
@ -102,7 +117,11 @@ func Dump(t interface{}, format uint8) ([]byte, error) {
|
|||
data = t.([]byte)
|
||||
case JSON:
|
||||
// TODO: use SetEscapeHTML(false)
|
||||
data, err = json.Marshal(t)
|
||||
if indent != "" {
|
||||
data, err = json.MarshalIndent(t, "", indent)
|
||||
} else {
|
||||
data, err = json.Marshal(t)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -122,7 +141,7 @@ func Dump(t interface{}, format uint8) ([]byte, error) {
|
|||
return nil, fmt.Errorf("dsd: failed to pack gencode struct: %s", err)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("dsd: tried to dump unknown type %d", format)
|
||||
return nil, fmt.Errorf("dsd: tried to dump with unknown format %d", format)
|
||||
}
|
||||
|
||||
r := append(f, data...)
|
||||
|
|
|
@ -58,277 +58,281 @@ type GenCodeTestStruct struct {
|
|||
}
|
||||
|
||||
func TestConversion(t *testing.T) {
|
||||
compressionFormats := []uint8{NONE, GZIP}
|
||||
for _, compression := range compressionFormats {
|
||||
|
||||
// STRING
|
||||
d, err := Dump("abc", STRING)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (string): %s", err)
|
||||
}
|
||||
|
||||
s, err := Load(d, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (string): %s", err)
|
||||
}
|
||||
ts := s.(string)
|
||||
|
||||
if ts != "abc" {
|
||||
t.Errorf("Load (string): subject and loaded object are not equal (%v != %v)", ts, "abc")
|
||||
}
|
||||
|
||||
// BYTES
|
||||
d, err = Dump([]byte("def"), BYTES)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (string): %s", err)
|
||||
}
|
||||
|
||||
b, err := Load(d, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (string): %s", err)
|
||||
}
|
||||
tb := b.([]byte)
|
||||
|
||||
if !bytes.Equal(tb, []byte("def")) {
|
||||
t.Errorf("Load (string): subject and loaded object are not equal (%v != %v)", tb, []byte("def"))
|
||||
}
|
||||
|
||||
// STRUCTS
|
||||
simpleSubject := SimpleTestStruct{
|
||||
"a",
|
||||
0x01,
|
||||
}
|
||||
|
||||
bString := "b"
|
||||
var bBytes byte = 0x02
|
||||
|
||||
complexSubject := ComplexTestStruct{
|
||||
-1,
|
||||
-2,
|
||||
-3,
|
||||
-4,
|
||||
-5,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
"a",
|
||||
&bString,
|
||||
[]string{"c", "d", "e"},
|
||||
&[]string{"f", "g", "h"},
|
||||
0x01,
|
||||
&bBytes,
|
||||
[]byte{0x03, 0x04, 0x05},
|
||||
&[]byte{0x05, 0x06, 0x07},
|
||||
map[string]string{
|
||||
"a": "b",
|
||||
"c": "d",
|
||||
"e": "f",
|
||||
},
|
||||
&map[string]string{
|
||||
"g": "h",
|
||||
"i": "j",
|
||||
"k": "l",
|
||||
},
|
||||
}
|
||||
|
||||
genCodeSubject := GenCodeTestStruct{
|
||||
-2,
|
||||
-3,
|
||||
-4,
|
||||
-5,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
"a",
|
||||
&bString,
|
||||
[]string{"c", "d", "e"},
|
||||
&[]string{"f", "g", "h"},
|
||||
0x01,
|
||||
&bBytes,
|
||||
[]byte{0x03, 0x04, 0x05},
|
||||
&[]byte{0x05, 0x06, 0x07},
|
||||
}
|
||||
|
||||
// test all formats (complex)
|
||||
formats := []uint8{JSON}
|
||||
|
||||
for _, format := range formats {
|
||||
|
||||
// simple
|
||||
b, err := Dump(&simpleSubject, format)
|
||||
// STRING
|
||||
d, err := DumpAndCompress("abc", STRING, compression)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (simple struct): %s", err)
|
||||
t.Fatalf("Dump error (string): %s", err)
|
||||
}
|
||||
|
||||
o, err := Load(b, &SimpleTestStruct{})
|
||||
s, err := Load(d, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (simple struct): %s", err)
|
||||
t.Fatalf("Load error (string): %s", err)
|
||||
}
|
||||
ts := s.(string)
|
||||
|
||||
if ts != "abc" {
|
||||
t.Errorf("Load (string): subject and loaded object are not equal (%v != %v)", ts, "abc")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(&simpleSubject, o) {
|
||||
t.Errorf("Load (simple struct): subject does not match loaded object")
|
||||
t.Errorf("Encoded: %v", string(b))
|
||||
t.Errorf("Compared: %v == %v", &simpleSubject, o)
|
||||
}
|
||||
|
||||
// complex
|
||||
b, err = Dump(&complexSubject, format)
|
||||
// BYTES
|
||||
d, err = DumpAndCompress([]byte("def"), BYTES, compression)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (complex struct): %s", err)
|
||||
t.Fatalf("Dump error (string): %s", err)
|
||||
}
|
||||
|
||||
o, err = Load(b, &ComplexTestStruct{})
|
||||
b, err := Load(d, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (complex struct): %s", err)
|
||||
t.Fatalf("Load error (string): %s", err)
|
||||
}
|
||||
tb := b.([]byte)
|
||||
|
||||
if !bytes.Equal(tb, []byte("def")) {
|
||||
t.Errorf("Load (string): subject and loaded object are not equal (%v != %v)", tb, []byte("def"))
|
||||
}
|
||||
|
||||
co := o.(*ComplexTestStruct)
|
||||
|
||||
if complexSubject.I != co.I {
|
||||
t.Errorf("Load (complex struct): struct.I is not equal (%v != %v)", complexSubject.I, co.I)
|
||||
}
|
||||
if complexSubject.I8 != co.I8 {
|
||||
t.Errorf("Load (complex struct): struct.I8 is not equal (%v != %v)", complexSubject.I8, co.I8)
|
||||
}
|
||||
if complexSubject.I16 != co.I16 {
|
||||
t.Errorf("Load (complex struct): struct.I16 is not equal (%v != %v)", complexSubject.I16, co.I16)
|
||||
}
|
||||
if complexSubject.I32 != co.I32 {
|
||||
t.Errorf("Load (complex struct): struct.I32 is not equal (%v != %v)", complexSubject.I32, co.I32)
|
||||
}
|
||||
if complexSubject.I64 != co.I64 {
|
||||
t.Errorf("Load (complex struct): struct.I64 is not equal (%v != %v)", complexSubject.I64, co.I64)
|
||||
}
|
||||
if complexSubject.UI != co.UI {
|
||||
t.Errorf("Load (complex struct): struct.UI is not equal (%v != %v)", complexSubject.UI, co.UI)
|
||||
}
|
||||
if complexSubject.UI8 != co.UI8 {
|
||||
t.Errorf("Load (complex struct): struct.UI8 is not equal (%v != %v)", complexSubject.UI8, co.UI8)
|
||||
}
|
||||
if complexSubject.UI16 != co.UI16 {
|
||||
t.Errorf("Load (complex struct): struct.UI16 is not equal (%v != %v)", complexSubject.UI16, co.UI16)
|
||||
}
|
||||
if complexSubject.UI32 != co.UI32 {
|
||||
t.Errorf("Load (complex struct): struct.UI32 is not equal (%v != %v)", complexSubject.UI32, co.UI32)
|
||||
}
|
||||
if complexSubject.UI64 != co.UI64 {
|
||||
t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", complexSubject.UI64, co.UI64)
|
||||
}
|
||||
if complexSubject.S != co.S {
|
||||
t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", complexSubject.S, co.S)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Sp, co.Sp) {
|
||||
t.Errorf("Load (complex struct): struct.Sp is not equal (%v != %v)", complexSubject.Sp, co.Sp)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Sa, co.Sa) {
|
||||
t.Errorf("Load (complex struct): struct.Sa is not equal (%v != %v)", complexSubject.Sa, co.Sa)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Sap, co.Sap) {
|
||||
t.Errorf("Load (complex struct): struct.Sap is not equal (%v != %v)", complexSubject.Sap, co.Sap)
|
||||
}
|
||||
if complexSubject.B != co.B {
|
||||
t.Errorf("Load (complex struct): struct.B is not equal (%v != %v)", complexSubject.B, co.B)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Bp, co.Bp) {
|
||||
t.Errorf("Load (complex struct): struct.Bp is not equal (%v != %v)", complexSubject.Bp, co.Bp)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Ba, co.Ba) {
|
||||
t.Errorf("Load (complex struct): struct.Ba is not equal (%v != %v)", complexSubject.Ba, co.Ba)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Bap, co.Bap) {
|
||||
t.Errorf("Load (complex struct): struct.Bap is not equal (%v != %v)", complexSubject.Bap, co.Bap)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.M, co.M) {
|
||||
t.Errorf("Load (complex struct): struct.M is not equal (%v != %v)", complexSubject.M, co.M)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Mp, co.Mp) {
|
||||
t.Errorf("Load (complex struct): struct.Mp is not equal (%v != %v)", complexSubject.Mp, co.Mp)
|
||||
// STRUCTS
|
||||
simpleSubject := SimpleTestStruct{
|
||||
"a",
|
||||
0x01,
|
||||
}
|
||||
|
||||
}
|
||||
bString := "b"
|
||||
var bBytes byte = 0x02
|
||||
|
||||
// test all formats
|
||||
formats = []uint8{JSON, GenCode}
|
||||
|
||||
for _, format := range formats {
|
||||
// simple
|
||||
b, err := Dump(&simpleSubject, format)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (simple struct): %s", err)
|
||||
complexSubject := ComplexTestStruct{
|
||||
-1,
|
||||
-2,
|
||||
-3,
|
||||
-4,
|
||||
-5,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
"a",
|
||||
&bString,
|
||||
[]string{"c", "d", "e"},
|
||||
&[]string{"f", "g", "h"},
|
||||
0x01,
|
||||
&bBytes,
|
||||
[]byte{0x03, 0x04, 0x05},
|
||||
&[]byte{0x05, 0x06, 0x07},
|
||||
map[string]string{
|
||||
"a": "b",
|
||||
"c": "d",
|
||||
"e": "f",
|
||||
},
|
||||
&map[string]string{
|
||||
"g": "h",
|
||||
"i": "j",
|
||||
"k": "l",
|
||||
},
|
||||
}
|
||||
|
||||
o, err := Load(b, &SimpleTestStruct{})
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (simple struct): %s", err)
|
||||
genCodeSubject := GenCodeTestStruct{
|
||||
-2,
|
||||
-3,
|
||||
-4,
|
||||
-5,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
"a",
|
||||
&bString,
|
||||
[]string{"c", "d", "e"},
|
||||
&[]string{"f", "g", "h"},
|
||||
0x01,
|
||||
&bBytes,
|
||||
[]byte{0x03, 0x04, 0x05},
|
||||
&[]byte{0x05, 0x06, 0x07},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(&simpleSubject, o) {
|
||||
t.Errorf("Load (simple struct): subject does not match loaded object")
|
||||
t.Errorf("Encoded: %v", string(b))
|
||||
t.Errorf("Compared: %v == %v", &simpleSubject, o)
|
||||
// test all formats (complex)
|
||||
formats := []uint8{JSON}
|
||||
|
||||
for _, format := range formats {
|
||||
|
||||
// simple
|
||||
b, err := DumpAndCompress(&simpleSubject, format, compression)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (simple struct): %s", err)
|
||||
}
|
||||
|
||||
o, err := Load(b, &SimpleTestStruct{})
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (simple struct): %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(&simpleSubject, o) {
|
||||
t.Errorf("Load (simple struct): subject does not match loaded object")
|
||||
t.Errorf("Encoded: %v", string(b))
|
||||
t.Errorf("Compared: %v == %v", &simpleSubject, o)
|
||||
}
|
||||
|
||||
// complex
|
||||
b, err = DumpAndCompress(&complexSubject, format, compression)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (complex struct): %s", err)
|
||||
}
|
||||
|
||||
o, err = Load(b, &ComplexTestStruct{})
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (complex struct): %s", err)
|
||||
}
|
||||
|
||||
co := o.(*ComplexTestStruct)
|
||||
|
||||
if complexSubject.I != co.I {
|
||||
t.Errorf("Load (complex struct): struct.I is not equal (%v != %v)", complexSubject.I, co.I)
|
||||
}
|
||||
if complexSubject.I8 != co.I8 {
|
||||
t.Errorf("Load (complex struct): struct.I8 is not equal (%v != %v)", complexSubject.I8, co.I8)
|
||||
}
|
||||
if complexSubject.I16 != co.I16 {
|
||||
t.Errorf("Load (complex struct): struct.I16 is not equal (%v != %v)", complexSubject.I16, co.I16)
|
||||
}
|
||||
if complexSubject.I32 != co.I32 {
|
||||
t.Errorf("Load (complex struct): struct.I32 is not equal (%v != %v)", complexSubject.I32, co.I32)
|
||||
}
|
||||
if complexSubject.I64 != co.I64 {
|
||||
t.Errorf("Load (complex struct): struct.I64 is not equal (%v != %v)", complexSubject.I64, co.I64)
|
||||
}
|
||||
if complexSubject.UI != co.UI {
|
||||
t.Errorf("Load (complex struct): struct.UI is not equal (%v != %v)", complexSubject.UI, co.UI)
|
||||
}
|
||||
if complexSubject.UI8 != co.UI8 {
|
||||
t.Errorf("Load (complex struct): struct.UI8 is not equal (%v != %v)", complexSubject.UI8, co.UI8)
|
||||
}
|
||||
if complexSubject.UI16 != co.UI16 {
|
||||
t.Errorf("Load (complex struct): struct.UI16 is not equal (%v != %v)", complexSubject.UI16, co.UI16)
|
||||
}
|
||||
if complexSubject.UI32 != co.UI32 {
|
||||
t.Errorf("Load (complex struct): struct.UI32 is not equal (%v != %v)", complexSubject.UI32, co.UI32)
|
||||
}
|
||||
if complexSubject.UI64 != co.UI64 {
|
||||
t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", complexSubject.UI64, co.UI64)
|
||||
}
|
||||
if complexSubject.S != co.S {
|
||||
t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", complexSubject.S, co.S)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Sp, co.Sp) {
|
||||
t.Errorf("Load (complex struct): struct.Sp is not equal (%v != %v)", complexSubject.Sp, co.Sp)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Sa, co.Sa) {
|
||||
t.Errorf("Load (complex struct): struct.Sa is not equal (%v != %v)", complexSubject.Sa, co.Sa)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Sap, co.Sap) {
|
||||
t.Errorf("Load (complex struct): struct.Sap is not equal (%v != %v)", complexSubject.Sap, co.Sap)
|
||||
}
|
||||
if complexSubject.B != co.B {
|
||||
t.Errorf("Load (complex struct): struct.B is not equal (%v != %v)", complexSubject.B, co.B)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Bp, co.Bp) {
|
||||
t.Errorf("Load (complex struct): struct.Bp is not equal (%v != %v)", complexSubject.Bp, co.Bp)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Ba, co.Ba) {
|
||||
t.Errorf("Load (complex struct): struct.Ba is not equal (%v != %v)", complexSubject.Ba, co.Ba)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Bap, co.Bap) {
|
||||
t.Errorf("Load (complex struct): struct.Bap is not equal (%v != %v)", complexSubject.Bap, co.Bap)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.M, co.M) {
|
||||
t.Errorf("Load (complex struct): struct.M is not equal (%v != %v)", complexSubject.M, co.M)
|
||||
}
|
||||
if !reflect.DeepEqual(complexSubject.Mp, co.Mp) {
|
||||
t.Errorf("Load (complex struct): struct.Mp is not equal (%v != %v)", complexSubject.Mp, co.Mp)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// complex
|
||||
b, err = Dump(&genCodeSubject, format)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (complex struct): %s", err)
|
||||
// test all formats
|
||||
formats = []uint8{JSON, GenCode}
|
||||
|
||||
for _, format := range formats {
|
||||
// simple
|
||||
b, err := DumpAndCompress(&simpleSubject, format, compression)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (simple struct): %s", err)
|
||||
}
|
||||
|
||||
o, err := Load(b, &SimpleTestStruct{})
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (simple struct): %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(&simpleSubject, o) {
|
||||
t.Errorf("Load (simple struct): subject does not match loaded object")
|
||||
t.Errorf("Encoded: %v", string(b))
|
||||
t.Errorf("Compared: %v == %v", &simpleSubject, o)
|
||||
}
|
||||
|
||||
// complex
|
||||
b, err = DumpAndCompress(&genCodeSubject, format, compression)
|
||||
if err != nil {
|
||||
t.Fatalf("Dump error (complex struct): %s", err)
|
||||
}
|
||||
|
||||
o, err = Load(b, &GenCodeTestStruct{})
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (complex struct): %s", err)
|
||||
}
|
||||
|
||||
co := o.(*GenCodeTestStruct)
|
||||
|
||||
if genCodeSubject.I8 != co.I8 {
|
||||
t.Errorf("Load (complex struct): struct.I8 is not equal (%v != %v)", genCodeSubject.I8, co.I8)
|
||||
}
|
||||
if genCodeSubject.I16 != co.I16 {
|
||||
t.Errorf("Load (complex struct): struct.I16 is not equal (%v != %v)", genCodeSubject.I16, co.I16)
|
||||
}
|
||||
if genCodeSubject.I32 != co.I32 {
|
||||
t.Errorf("Load (complex struct): struct.I32 is not equal (%v != %v)", genCodeSubject.I32, co.I32)
|
||||
}
|
||||
if genCodeSubject.I64 != co.I64 {
|
||||
t.Errorf("Load (complex struct): struct.I64 is not equal (%v != %v)", genCodeSubject.I64, co.I64)
|
||||
}
|
||||
if genCodeSubject.UI8 != co.UI8 {
|
||||
t.Errorf("Load (complex struct): struct.UI8 is not equal (%v != %v)", genCodeSubject.UI8, co.UI8)
|
||||
}
|
||||
if genCodeSubject.UI16 != co.UI16 {
|
||||
t.Errorf("Load (complex struct): struct.UI16 is not equal (%v != %v)", genCodeSubject.UI16, co.UI16)
|
||||
}
|
||||
if genCodeSubject.UI32 != co.UI32 {
|
||||
t.Errorf("Load (complex struct): struct.UI32 is not equal (%v != %v)", genCodeSubject.UI32, co.UI32)
|
||||
}
|
||||
if genCodeSubject.UI64 != co.UI64 {
|
||||
t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", genCodeSubject.UI64, co.UI64)
|
||||
}
|
||||
if genCodeSubject.S != co.S {
|
||||
t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", genCodeSubject.S, co.S)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Sp, co.Sp) {
|
||||
t.Errorf("Load (complex struct): struct.Sp is not equal (%v != %v)", genCodeSubject.Sp, co.Sp)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Sa, co.Sa) {
|
||||
t.Errorf("Load (complex struct): struct.Sa is not equal (%v != %v)", genCodeSubject.Sa, co.Sa)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Sap, co.Sap) {
|
||||
t.Errorf("Load (complex struct): struct.Sap is not equal (%v != %v)", genCodeSubject.Sap, co.Sap)
|
||||
}
|
||||
if genCodeSubject.B != co.B {
|
||||
t.Errorf("Load (complex struct): struct.B is not equal (%v != %v)", genCodeSubject.B, co.B)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Bp, co.Bp) {
|
||||
t.Errorf("Load (complex struct): struct.Bp is not equal (%v != %v)", genCodeSubject.Bp, co.Bp)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Ba, co.Ba) {
|
||||
t.Errorf("Load (complex struct): struct.Ba is not equal (%v != %v)", genCodeSubject.Ba, co.Ba)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Bap, co.Bap) {
|
||||
t.Errorf("Load (complex struct): struct.Bap is not equal (%v != %v)", genCodeSubject.Bap, co.Bap)
|
||||
}
|
||||
}
|
||||
|
||||
o, err = Load(b, &GenCodeTestStruct{})
|
||||
if err != nil {
|
||||
t.Fatalf("Load error (complex struct): %s", err)
|
||||
}
|
||||
|
||||
co := o.(*GenCodeTestStruct)
|
||||
|
||||
if genCodeSubject.I8 != co.I8 {
|
||||
t.Errorf("Load (complex struct): struct.I8 is not equal (%v != %v)", genCodeSubject.I8, co.I8)
|
||||
}
|
||||
if genCodeSubject.I16 != co.I16 {
|
||||
t.Errorf("Load (complex struct): struct.I16 is not equal (%v != %v)", genCodeSubject.I16, co.I16)
|
||||
}
|
||||
if genCodeSubject.I32 != co.I32 {
|
||||
t.Errorf("Load (complex struct): struct.I32 is not equal (%v != %v)", genCodeSubject.I32, co.I32)
|
||||
}
|
||||
if genCodeSubject.I64 != co.I64 {
|
||||
t.Errorf("Load (complex struct): struct.I64 is not equal (%v != %v)", genCodeSubject.I64, co.I64)
|
||||
}
|
||||
if genCodeSubject.UI8 != co.UI8 {
|
||||
t.Errorf("Load (complex struct): struct.UI8 is not equal (%v != %v)", genCodeSubject.UI8, co.UI8)
|
||||
}
|
||||
if genCodeSubject.UI16 != co.UI16 {
|
||||
t.Errorf("Load (complex struct): struct.UI16 is not equal (%v != %v)", genCodeSubject.UI16, co.UI16)
|
||||
}
|
||||
if genCodeSubject.UI32 != co.UI32 {
|
||||
t.Errorf("Load (complex struct): struct.UI32 is not equal (%v != %v)", genCodeSubject.UI32, co.UI32)
|
||||
}
|
||||
if genCodeSubject.UI64 != co.UI64 {
|
||||
t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", genCodeSubject.UI64, co.UI64)
|
||||
}
|
||||
if genCodeSubject.S != co.S {
|
||||
t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", genCodeSubject.S, co.S)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Sp, co.Sp) {
|
||||
t.Errorf("Load (complex struct): struct.Sp is not equal (%v != %v)", genCodeSubject.Sp, co.Sp)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Sa, co.Sa) {
|
||||
t.Errorf("Load (complex struct): struct.Sa is not equal (%v != %v)", genCodeSubject.Sa, co.Sa)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Sap, co.Sap) {
|
||||
t.Errorf("Load (complex struct): struct.Sap is not equal (%v != %v)", genCodeSubject.Sap, co.Sap)
|
||||
}
|
||||
if genCodeSubject.B != co.B {
|
||||
t.Errorf("Load (complex struct): struct.B is not equal (%v != %v)", genCodeSubject.B, co.B)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Bp, co.Bp) {
|
||||
t.Errorf("Load (complex struct): struct.Bp is not equal (%v != %v)", genCodeSubject.Bp, co.Bp)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Ba, co.Ba) {
|
||||
t.Errorf("Load (complex struct): struct.Ba is not equal (%v != %v)", genCodeSubject.Ba, co.Ba)
|
||||
}
|
||||
if !reflect.DeepEqual(genCodeSubject.Bap, co.Bap) {
|
||||
t.Errorf("Load (complex struct): struct.Bap is not equal (%v != %v)", genCodeSubject.Bap, co.Bap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue