diff --git a/fields_mask_test.go b/fields_mask_test.go index 7209c2c..d1064d3 100644 --- a/fields_mask_test.go +++ b/fields_mask_test.go @@ -3,7 +3,7 @@ package meta_test import ( "testing" - "git.bit5.ru/backend/meta/v3" + "git.bit5.ru/backend/meta/v4" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/go.mod b/go.mod index 5b286be..c3ada7a 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module git.bit5.ru/backend/meta/v3 +module git.bit5.ru/backend/meta/v4 go 1.20 diff --git a/interface.go b/interface.go index 42c4ba9..954b58f 100644 --- a/interface.go +++ b/interface.go @@ -69,6 +69,7 @@ type Readable interface { type Writable interface { Write(Writer) error WriteFields(Writer) error + FieldsCount() int } type Struct interface { @@ -77,7 +78,7 @@ type Struct interface { Writable } -type StructFactory func(classId uint32) (Struct, error) +type StructFactory func(classId uint32) (Readable, error) type Bitmasked interface { FieldChanged(index uint64) bool @@ -85,3 +86,8 @@ type Bitmasked interface { HasChangedFields() bool FieldsMask() FieldsMask } + +type WritableClass interface { + Class + Writable +} diff --git a/meta.go b/meta.go index 8f84c57..573a8d0 100644 --- a/meta.go +++ b/meta.go @@ -1,6 +1,6 @@ package meta -func ReadStructGeneric(r Reader, createStruct StructFactory, field string) (Struct, error) { +func ReadGeneric(r Reader, createFunc StructFactory, field string) (Readable, error) { if err := r.BeginContainer(field); err != nil { return nil, err } @@ -10,12 +10,12 @@ func ReadStructGeneric(r Reader, createStruct StructFactory, field string) (Stru return nil, err } - s, err := createStruct(classId) + s, err := createFunc(classId) if err != nil { return nil, err } - if err := s.Read(r); err != nil { + if err := s.ReadFields(r); err != nil { return nil, err } @@ -26,14 +26,14 @@ func ReadStructGeneric(r Reader, createStruct StructFactory, field string) (Stru return s, nil } -func WriteStructGeneric(w Writer, s Struct, field string) error { - if err := w.BeginCollection(2, field); err != nil { +func WriteGeneric(w Writer, s WritableClass, field string) error { + if err := w.BeginCollection(s.FieldsCount()+1, field); err != nil { return err } if err := w.WriteUint32(s.ClassId(), ""); err != nil { return err } - if err := s.Write(w); err != nil { + if err := s.WriteFields(w); err != nil { return err } return w.EndCollection() diff --git a/msgpack_assoc_writer_test.go b/msgpack_assoc_writer_test.go index 1855ce9..0162fc9 100644 --- a/msgpack_assoc_writer_test.go +++ b/msgpack_assoc_writer_test.go @@ -5,7 +5,7 @@ import ( "encoding/hex" "testing" - "git.bit5.ru/backend/meta/v3" + "git.bit5.ru/backend/meta/v4" "github.com/stretchr/testify/require" ) diff --git a/msgpack_reader_test.go b/msgpack_reader_test.go index 732f194..12931cd 100644 --- a/msgpack_reader_test.go +++ b/msgpack_reader_test.go @@ -5,7 +5,7 @@ import ( "encoding/hex" "testing" - "git.bit5.ru/backend/meta/v3" + "git.bit5.ru/backend/meta/v4" "github.com/stretchr/testify/require" ) diff --git a/msgpack_writer_test.go b/msgpack_writer_test.go index 1aaaccb..5c02cd9 100644 --- a/msgpack_writer_test.go +++ b/msgpack_writer_test.go @@ -5,7 +5,7 @@ import ( "encoding/hex" "testing" - "git.bit5.ru/backend/meta/v3" + "git.bit5.ru/backend/meta/v4" "github.com/stretchr/testify/require" ) diff --git a/structs_test.go b/structs_test.go index 18bcb2b..d6ee4ca 100644 --- a/structs_test.go +++ b/structs_test.go @@ -1,7 +1,7 @@ package meta_test import ( - "git.bit5.ru/backend/meta/v3" + "git.bit5.ru/backend/meta/v4" ) type TestParent struct {