2023-09-01 01:37:06 +03:00
|
|
|
package meta
|
|
|
|
|
2023-09-01 17:14:09 +03:00
|
|
|
const classField = "$id"
|
|
|
|
|
2023-09-01 15:23:18 +03:00
|
|
|
func ReadGeneric(r Reader, createFunc StructFactory, field string) (Readable, error) {
|
2023-09-01 01:37:06 +03:00
|
|
|
if err := r.BeginContainer(field); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-23 17:11:43 +03:00
|
|
|
size, err := r.ContainerSize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if size == 0 {
|
|
|
|
if err := r.EndContainer(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-09-01 01:37:06 +03:00
|
|
|
|
|
|
|
var classId uint32
|
2023-09-01 17:14:09 +03:00
|
|
|
if err := r.ReadUint32(&classId, classField); err != nil {
|
2023-09-01 01:37:06 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-01 15:23:18 +03:00
|
|
|
s, err := createFunc(classId)
|
2023-09-01 01:37:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-01 15:23:18 +03:00
|
|
|
if err := s.ReadFields(r); err != nil {
|
2023-09-01 01:37:06 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.EndContainer(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 15:23:18 +03:00
|
|
|
func WriteGeneric(w Writer, s WritableClass, field string) error {
|
2023-09-01 17:21:04 +03:00
|
|
|
if err := w.BeginContainer(s.FieldsCount()+1, field); err != nil {
|
2023-09-01 01:37:06 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-09-01 17:14:09 +03:00
|
|
|
if err := w.WriteUint32(s.ClassId(), classField); err != nil {
|
2023-09-01 01:37:06 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-09-01 15:23:18 +03:00
|
|
|
if err := s.WriteFields(w); err != nil {
|
2023-09-01 01:37:06 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-09-01 17:21:04 +03:00
|
|
|
return w.EndContainer()
|
2023-09-01 01:37:06 +03:00
|
|
|
}
|