meta/structs_test.go

376 lines
6.4 KiB
Go
Raw Permalink Normal View History

package meta_test
import (
2023-09-11 17:41:16 +03:00
"git.bit5.ru/backend/meta/v5"
"github.com/pkg/errors"
)
func CreateTestParent(id uint32) (meta.Readable, error) {
if id == 111 {
return &TestParent{}, nil
}
return nil, errors.Errorf("wrong id")
}
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"`
Field5 ITestParent `json:"f5" msgpack:"f5"`
}
type ITestParent interface {
meta.Struct
PtrTestParent() *TestParent
}
func (s *TestParent) PtrTestParent() *TestParent {
return s
}
func (s *TestParent) ClassId() uint32 {
return 111
}
func (s *TestParent) ClassName() string {
return "TestParent"
}
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]
}
s.Field5 = nil
}
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 < 5 {
contSize = 5
}
if contSize <= 0 {
return nil
}
contSize--
if err := reader.ReadString(&s.Field1, "f1"); err != nil {
return err
}
if contSize <= 0 {
return nil
}
contSize--
2023-06-16 13:52:56 +03:00
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--
2023-09-11 16:51:34 +03:00
if err := reader.BeginCollection("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)
}
2023-09-11 16:51:34 +03:00
if err := reader.EndCollection(); err != nil {
return err
}
if contSize <= 0 {
return nil
}
contSize--
2023-09-11 16:51:34 +03:00
if err := reader.BeginCollection("f4"); err != nil {
return err
}
field4Size, err := reader.ContainerSize()
if err != nil {
return err
}
for ; field4Size > 0; field4Size-- {
var tmpField4 TestFoo
2023-06-16 13:52:56 +03:00
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)
}
2023-09-11 16:51:34 +03:00
if err := reader.EndCollection(); err != nil {
return err
}
if contSize <= 0 {
return nil
}
contSize--
if v, err := meta.ReadGeneric(reader, CreateTestParent, "f5"); err != nil {
if err != meta.FieldNotFound {
return err
}
} else {
if v != nil {
s.Field5 = v.(ITestParent)
}
}
return nil
}
func (s *TestParent) Write(writer meta.Writer) error {
2023-07-03 22:09:39 +03:00
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
}
2023-07-03 22:09:39 +03:00
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
}
2023-07-03 22:09:39 +03:00
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
}
}
2023-07-03 22:09:39 +03:00
if err := writer.EndCollection(); err != nil {
return err
}
2023-07-03 22:09:39 +03:00
if err := writer.BeginCollection(len(s.Field4), "f4"); err != nil {
return err
}
for _, v := range s.Field4 {
2023-07-03 22:09:39 +03:00
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
}
}
2023-07-03 22:09:39 +03:00
if err := writer.EndCollection(); err != nil {
return err
}
return nil
}
func (s *TestParent) FieldsCount() int {
return 5
}
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 {
2023-07-03 22:09:39 +03:00
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 {
2023-07-03 22:09:39 +03:00
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
}