43 lines
831 B
Go
43 lines
831 B
Go
package meta
|
|
|
|
const classField = "$id"
|
|
|
|
func ReadGeneric(r Reader, createFunc StructFactory, field string) (Readable, error) {
|
|
if err := r.BeginContainer(field); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var classId uint32
|
|
if err := r.ReadUint32(&classId, classField); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s, err := createFunc(classId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := s.ReadFields(r); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := r.EndContainer(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return s, nil
|
|
}
|
|
|
|
func WriteGeneric(w Writer, s WritableClass, field string) error {
|
|
if err := w.BeginContainer(s.FieldsCount()+1, field); err != nil {
|
|
return err
|
|
}
|
|
if err := w.WriteUint32(s.ClassId(), classField); err != nil {
|
|
return err
|
|
}
|
|
if err := s.WriteFields(w); err != nil {
|
|
return err
|
|
}
|
|
return w.EndContainer()
|
|
}
|