package meta_test import ( "bytes" "encoding/hex" "testing" "git.bit5.ru/backend/meta" "github.com/stretchr/testify/require" ) type foo struct { field1 uint32 field2 uint32 field3 []uint32 field4 bar } func (f *foo) Read(reader meta.Reader) error { if err := reader.BeginContainer(""); err != nil { return err } if err := f.ReadFields(reader); err != nil { return err } if err := reader.EndContainer(); err != nil { return err } return nil } func (f *foo) ReadFields(reader meta.Reader) error { if err := reader.ReadUint32(&f.field1, "f1"); err != nil { return err } if err := reader.ReadUint32(&f.field2, "f2"); err != nil { return err } if err := reader.BeginContainer("f3"); err != nil { return err } size, err := reader.ContainerSize() if err != nil { return err } for ; size > 0; size-- { var tmp uint32 if err := reader.ReadUint32(&tmp, ""); err != nil { return err } f.field3 = append(f.field3, tmp) } if err := reader.EndContainer(); err != nil { return err } if err := reader.BeginContainer("f4"); err != nil { return err } if err := f.field4.ReadFields(reader); err != nil { return err } if err := reader.EndContainer(); err != nil { return err } return nil } type bar struct { a uint32 b uint32 } func (b *bar) Read(reader meta.Reader) error { if err := reader.BeginContainer(""); err != nil { return err } if err := b.ReadFields(reader); err != nil { return err } if err := reader.EndContainer(); err != nil { return err } return nil } func (b *bar) ReadFields(reader meta.Reader) error { if err := reader.ReadUint32(&b.a, "a"); err != nil { return err } if err := reader.ReadUint32(&b.b, "b"); err != nil { return err } return nil } func TestMsgprdr(t *testing.T) { str := `84A266310AA2663393010203A2663482A16104A16205A266320F` b, err := hex.DecodeString(str) require.NoError(t, err) rdr := meta.NewMsgpackStatefulReader(bytes.NewReader(b)) var actual foo readErr := actual.Read(rdr) require.NoError(t, readErr) expected := foo{ field1: 10, field2: 15, field3: []uint32{1, 2, 3}, field4: bar{ a: 4, b: 5, }, } require.EqualValues(t, expected, actual) }