329 lines
5.6 KiB
Go
329 lines
5.6 KiB
Go
package meta_test
|
|
|
|
import (
|
|
"git.bit5.ru/backend/meta/v4"
|
|
)
|
|
|
|
type TestParent struct {
|
|
Field1 string `json:"f1" msgpack:"f1"`
|
|
Field2 TestFoo `json:"f2" msgpack:"f2"`
|
|
Field3 []int8 `json:"f3" msgpack:"f3"`
|
|
Field4 []TestFoo `json:"f4" msgpack:"f4"`
|
|
}
|
|
|
|
func (s *TestParent) Reset() {
|
|
|
|
s.Field1 = ""
|
|
s.Field2.Reset()
|
|
if s.Field3 == nil {
|
|
s.Field3 = make([]int8, 0)
|
|
} else {
|
|
s.Field3 = s.Field3[:0]
|
|
}
|
|
if s.Field4 == nil {
|
|
s.Field4 = make([]TestFoo, 0)
|
|
} else {
|
|
s.Field4 = s.Field4[:0]
|
|
}
|
|
|
|
}
|
|
|
|
func (s *TestParent) Read(reader meta.Reader) error {
|
|
if err := reader.BeginContainer(""); err != nil {
|
|
return err
|
|
}
|
|
if err := s.ReadFields(reader); err != nil {
|
|
return err
|
|
}
|
|
return reader.EndContainer()
|
|
}
|
|
|
|
func (s *TestParent) ReadFields(reader meta.Reader) error {
|
|
s.Reset()
|
|
|
|
contSize, err := reader.ContainerSize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if contSize < 4 {
|
|
contSize = 4
|
|
}
|
|
|
|
if contSize <= 0 {
|
|
return nil
|
|
}
|
|
|
|
contSize--
|
|
if err := reader.ReadString(&s.Field1, "f1"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if contSize <= 0 {
|
|
return nil
|
|
}
|
|
|
|
contSize--
|
|
if err := reader.BeginContainer("f2"); err != nil {
|
|
return err
|
|
}
|
|
if err := s.Field2.ReadFields(reader); err != nil {
|
|
return err
|
|
}
|
|
if err := reader.EndContainer(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if contSize <= 0 {
|
|
return nil
|
|
}
|
|
|
|
contSize--
|
|
if err := reader.BeginContainer("f3"); err != nil {
|
|
return err
|
|
}
|
|
field3Size, err := reader.ContainerSize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for ; field3Size > 0; field3Size-- {
|
|
var tmpField3 int8
|
|
if err := reader.ReadInt8(&tmpField3, ""); err != nil {
|
|
return err
|
|
}
|
|
|
|
s.Field3 = append(s.Field3, tmpField3)
|
|
}
|
|
if err := reader.EndContainer(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if contSize <= 0 {
|
|
return nil
|
|
}
|
|
|
|
contSize--
|
|
if err := reader.BeginContainer("f4"); err != nil {
|
|
return err
|
|
}
|
|
field4Size, err := reader.ContainerSize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for ; field4Size > 0; field4Size-- {
|
|
var tmpField4 TestFoo
|
|
if err := reader.BeginContainer(""); err != nil {
|
|
return err
|
|
}
|
|
if err := tmpField4.ReadFields(reader); err != nil {
|
|
return err
|
|
}
|
|
if err := reader.EndContainer(); err != nil {
|
|
return err
|
|
}
|
|
|
|
s.Field4 = append(s.Field4, tmpField4)
|
|
}
|
|
if err := reader.EndContainer(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *TestParent) Write(writer meta.Writer) error {
|
|
if err := writer.BeginContainer(4, ""); err != nil {
|
|
return err
|
|
}
|
|
if err := s.WriteFields(writer); err != nil {
|
|
return err
|
|
}
|
|
return writer.EndContainer()
|
|
}
|
|
|
|
func (s *TestParent) WriteFields(writer meta.Writer) error {
|
|
|
|
if err := writer.WriteString(s.Field1, "f1"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := writer.BeginContainer(1, "f2"); err != nil {
|
|
return err
|
|
}
|
|
if err := s.Field2.WriteFields(writer); err != nil {
|
|
return err
|
|
}
|
|
if err := writer.EndContainer(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := writer.BeginCollection(len(s.Field3), "f3"); err != nil {
|
|
return err
|
|
}
|
|
for _, v := range s.Field3 {
|
|
if err := writer.WriteInt8(v, ""); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := writer.EndCollection(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := writer.BeginCollection(len(s.Field4), "f4"); err != nil {
|
|
return err
|
|
}
|
|
for _, v := range s.Field4 {
|
|
if err := writer.BeginContainer(1, ""); err != nil {
|
|
return err
|
|
}
|
|
if err := v.WriteFields(writer); err != nil {
|
|
return err
|
|
}
|
|
if err := writer.EndContainer(); err != nil {
|
|
return err
|
|
}
|
|
|
|
}
|
|
if err := writer.EndCollection(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type TestChild struct {
|
|
TestParent
|
|
|
|
Field string `json:"f" msgpack:"f"`
|
|
}
|
|
|
|
func (s *TestChild) Reset() {
|
|
s.TestParent.Reset()
|
|
|
|
s.Field = ""
|
|
|
|
}
|
|
|
|
func (s *TestChild) Read(reader meta.Reader) error {
|
|
if err := reader.BeginContainer(""); err != nil {
|
|
return err
|
|
}
|
|
if err := s.ReadFields(reader); err != nil {
|
|
return err
|
|
}
|
|
return reader.EndContainer()
|
|
}
|
|
|
|
func (s *TestChild) ReadFields(reader meta.Reader) error {
|
|
s.Reset()
|
|
|
|
contSize, err := reader.ContainerSize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if contSize < 1 {
|
|
contSize = 1
|
|
}
|
|
|
|
if err := s.TestParent.ReadFields(reader); err != nil {
|
|
return err
|
|
}
|
|
|
|
if contSize <= 0 {
|
|
return nil
|
|
}
|
|
|
|
contSize--
|
|
if err := reader.ReadString(&s.Field, "f"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *TestChild) Write(writer meta.Writer) error {
|
|
if err := writer.BeginContainer(5, ""); err != nil {
|
|
return err
|
|
}
|
|
if err := s.WriteFields(writer); err != nil {
|
|
return err
|
|
}
|
|
return writer.EndContainer()
|
|
}
|
|
|
|
func (s *TestChild) WriteFields(writer meta.Writer) error {
|
|
if err := s.TestParent.WriteFields(writer); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := writer.WriteString(s.Field, "f"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type TestFoo struct {
|
|
Field int64 `json:"field" msgpack:"field"`
|
|
}
|
|
|
|
func (s *TestFoo) Reset() {
|
|
|
|
s.Field = 0
|
|
|
|
}
|
|
|
|
func (s *TestFoo) Read(reader meta.Reader) error {
|
|
if err := reader.BeginContainer(""); err != nil {
|
|
return err
|
|
}
|
|
if err := s.ReadFields(reader); err != nil {
|
|
return err
|
|
}
|
|
return reader.EndContainer()
|
|
}
|
|
|
|
func (s *TestFoo) ReadFields(reader meta.Reader) error {
|
|
s.Reset()
|
|
|
|
contSize, err := reader.ContainerSize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if contSize < 1 {
|
|
contSize = 1
|
|
}
|
|
|
|
if contSize <= 0 {
|
|
return nil
|
|
}
|
|
|
|
contSize--
|
|
if err := reader.ReadInt64(&s.Field, "field"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *TestFoo) Write(writer meta.Writer) error {
|
|
if err := writer.BeginContainer(1, ""); err != nil {
|
|
return err
|
|
}
|
|
if err := s.WriteFields(writer); err != nil {
|
|
return err
|
|
}
|
|
return writer.EndContainer()
|
|
}
|
|
|
|
func (s *TestFoo) WriteFields(writer meta.Writer) error {
|
|
|
|
if err := writer.WriteInt64(s.Field, "field"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|