extend Writable interface. WritableClass added

This commit is contained in:
Pavel Merzlyakov 2023-09-01 15:23:18 +03:00
parent 62a85e0392
commit 521d19d172
8 changed files with 19 additions and 13 deletions

View File

@ -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"

2
go.mod
View File

@ -1,4 +1,4 @@
module git.bit5.ru/backend/meta/v3
module git.bit5.ru/backend/meta/v4
go 1.20

View File

@ -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
}

12
meta.go
View File

@ -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()

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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"
)

View File

@ -1,7 +1,7 @@
package meta_test
import (
"git.bit5.ru/backend/meta/v3"
"git.bit5.ru/backend/meta/v4"
)
type TestParent struct {