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 ( import (
"testing" "testing"
"git.bit5.ru/backend/meta/v3" "git.bit5.ru/backend/meta/v4"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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 go 1.20

View File

@ -69,6 +69,7 @@ type Readable interface {
type Writable interface { type Writable interface {
Write(Writer) error Write(Writer) error
WriteFields(Writer) error WriteFields(Writer) error
FieldsCount() int
} }
type Struct interface { type Struct interface {
@ -77,7 +78,7 @@ type Struct interface {
Writable Writable
} }
type StructFactory func(classId uint32) (Struct, error) type StructFactory func(classId uint32) (Readable, error)
type Bitmasked interface { type Bitmasked interface {
FieldChanged(index uint64) bool FieldChanged(index uint64) bool
@ -85,3 +86,8 @@ type Bitmasked interface {
HasChangedFields() bool HasChangedFields() bool
FieldsMask() FieldsMask FieldsMask() FieldsMask
} }
type WritableClass interface {
Class
Writable
}

12
meta.go
View File

@ -1,6 +1,6 @@
package meta 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 { if err := r.BeginContainer(field); err != nil {
return nil, err return nil, err
} }
@ -10,12 +10,12 @@ func ReadStructGeneric(r Reader, createStruct StructFactory, field string) (Stru
return nil, err return nil, err
} }
s, err := createStruct(classId) s, err := createFunc(classId)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := s.Read(r); err != nil { if err := s.ReadFields(r); err != nil {
return nil, err return nil, err
} }
@ -26,14 +26,14 @@ func ReadStructGeneric(r Reader, createStruct StructFactory, field string) (Stru
return s, nil return s, nil
} }
func WriteStructGeneric(w Writer, s Struct, field string) error { func WriteGeneric(w Writer, s WritableClass, field string) error {
if err := w.BeginCollection(2, field); err != nil { if err := w.BeginCollection(s.FieldsCount()+1, field); err != nil {
return err return err
} }
if err := w.WriteUint32(s.ClassId(), ""); err != nil { if err := w.WriteUint32(s.ClassId(), ""); err != nil {
return err return err
} }
if err := s.Write(w); err != nil { if err := s.WriteFields(w); err != nil {
return err return err
} }
return w.EndCollection() return w.EndCollection()

View File

@ -5,7 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"git.bit5.ru/backend/meta/v3" "git.bit5.ru/backend/meta/v4"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

@ -5,7 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"git.bit5.ru/backend/meta/v3" "git.bit5.ru/backend/meta/v4"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

@ -5,7 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"git.bit5.ru/backend/meta/v3" "git.bit5.ru/backend/meta/v4"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

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