/
type.go
46 lines (37 loc) · 930 Bytes
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package variant
import (
"git.golaxy.org/framework/util/binaryutil"
"reflect"
)
// TypeId 类型Id
type TypeId uint32
// Read implements io.Reader
func (t TypeId) Read(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
if err := bs.WriteUvarint(uint64(t)); err != nil {
return bs.BytesWritten(), err
}
return bs.BytesWritten(), nil
}
// Write implements io.Writer
func (t *TypeId) Write(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
v, err := bs.ReadUvarint()
if err != nil {
return bs.BytesRead(), err
}
*t = TypeId(v)
return bs.BytesRead(), nil
}
// Size 大小
func (t TypeId) Size() int {
return binaryutil.SizeofUvarint(uint64(t))
}
// New 创建对象指针
func (t TypeId) New() (Value, error) {
return variantCreator.New(t)
}
// NewReflected 创建反射对象指针
func (t TypeId) NewReflected() (reflect.Value, error) {
return variantCreator.NewReflected(t)
}