init
This commit is contained in:
@@ -0,0 +1,420 @@
|
||||
package taf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
)
|
||||
|
||||
// Reader 负责从 TAF/JCE 字节流中按 tag 顺序读取字段。
|
||||
type Reader struct {
|
||||
buf *bytes.Reader
|
||||
}
|
||||
|
||||
func NewReader(data []byte) *Reader {
|
||||
return &Reader{buf: bytes.NewReader(data)}
|
||||
}
|
||||
|
||||
func (r *Reader) Position() int64 {
|
||||
pos, _ := r.buf.Seek(0, io.SeekCurrent)
|
||||
return pos
|
||||
}
|
||||
|
||||
func (r *Reader) Seek(pos int64) error {
|
||||
_, err := r.buf.Seek(pos, io.SeekStart)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Reader) ReadHead() (int, Type, error) {
|
||||
b, err := r.buf.ReadByte()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
tag := int((b >> 4) & 0x0f)
|
||||
typ := Type(b & 0x0f)
|
||||
if tag == 15 {
|
||||
ext, err := r.buf.ReadByte()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
tag = int(ext)
|
||||
}
|
||||
return tag, typ, nil
|
||||
}
|
||||
|
||||
func (r *Reader) PeekHead() (int, Type, error) {
|
||||
pos := r.Position()
|
||||
tag, typ, err := r.ReadHead()
|
||||
_ = r.Seek(pos)
|
||||
return tag, typ, err
|
||||
}
|
||||
|
||||
func (r *Reader) ReadIntValue(typ Type) (int64, error) {
|
||||
switch typ {
|
||||
case TypeZero:
|
||||
return 0, nil
|
||||
case TypeInt8:
|
||||
b, err := r.buf.ReadByte()
|
||||
return int64(int8(b)), err
|
||||
case TypeInt16:
|
||||
var b [2]byte
|
||||
if _, err := io.ReadFull(r.buf, b[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int64(int16(binary.BigEndian.Uint16(b[:]))), nil
|
||||
case TypeInt32:
|
||||
var b [4]byte
|
||||
if _, err := io.ReadFull(r.buf, b[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int64(int32(binary.BigEndian.Uint32(b[:]))), nil
|
||||
case TypeInt64:
|
||||
var b [8]byte
|
||||
if _, err := io.ReadFull(r.buf, b[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int64(binary.BigEndian.Uint64(b[:])), nil
|
||||
default:
|
||||
return 0, fmt.Errorf("taf: 期望整数类型,实际 0x%02x", byte(typ))
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) ReadStringValue(typ Type) (string, error) {
|
||||
var n uint32
|
||||
switch typ {
|
||||
case TypeString1:
|
||||
b, err := r.buf.ReadByte()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
n = uint32(b)
|
||||
case TypeString4:
|
||||
var b [4]byte
|
||||
if _, err := io.ReadFull(r.buf, b[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
n = binary.BigEndian.Uint32(b[:])
|
||||
default:
|
||||
return "", fmt.Errorf("taf: 期望字符串类型,实际 0x%02x", byte(typ))
|
||||
}
|
||||
raw := make([]byte, n)
|
||||
if _, err := io.ReadFull(r.buf, raw); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(raw), nil
|
||||
}
|
||||
|
||||
func (r *Reader) ReadBytesValue(typ Type) ([]byte, error) {
|
||||
if typ != TypeSimpleList {
|
||||
return nil, fmt.Errorf("taf: 期望 bytes,实际 0x%02x", byte(typ))
|
||||
}
|
||||
_, itemType, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if itemType != TypeInt8 {
|
||||
return nil, fmt.Errorf("taf: bytes 元素类型不是 int8: 0x%02x", byte(itemType))
|
||||
}
|
||||
n, err := r.readLength()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n < 0 || n > math.MaxInt32 {
|
||||
return nil, fmt.Errorf("taf: bytes 长度异常: %d", n)
|
||||
}
|
||||
raw := make([]byte, int(n))
|
||||
_, err = io.ReadFull(r.buf, raw)
|
||||
return raw, err
|
||||
}
|
||||
|
||||
func (r *Reader) Skip(typ Type) error {
|
||||
switch typ {
|
||||
case TypeZero:
|
||||
return nil
|
||||
case TypeInt8:
|
||||
_, err := r.buf.ReadByte()
|
||||
return err
|
||||
case TypeInt16:
|
||||
_, err := r.buf.Seek(2, io.SeekCurrent)
|
||||
return err
|
||||
case TypeInt32, TypeFloat:
|
||||
_, err := r.buf.Seek(4, io.SeekCurrent)
|
||||
return err
|
||||
case TypeInt64, TypeDouble:
|
||||
_, err := r.buf.Seek(8, io.SeekCurrent)
|
||||
return err
|
||||
case TypeString1:
|
||||
b, err := r.buf.ReadByte()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.buf.Seek(int64(b), io.SeekCurrent)
|
||||
return err
|
||||
case TypeString4:
|
||||
var b [4]byte
|
||||
if _, err := io.ReadFull(r.buf, b[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := r.buf.Seek(int64(binary.BigEndian.Uint32(b[:])), io.SeekCurrent)
|
||||
return err
|
||||
case TypeMap:
|
||||
n, err := r.readLength()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := int64(0); i < n; i++ {
|
||||
if _, kt, err := r.ReadHead(); err != nil {
|
||||
return err
|
||||
} else if err := r.Skip(kt); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, vt, err := r.ReadHead(); err != nil {
|
||||
return err
|
||||
} else if err := r.Skip(vt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
case TypeList:
|
||||
n, err := r.readLength()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := int64(0); i < n; i++ {
|
||||
if _, it, err := r.ReadHead(); err != nil {
|
||||
return err
|
||||
} else if err := r.Skip(it); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
case TypeStructBegin:
|
||||
return r.SkipToStructEnd()
|
||||
case TypeStructEnd:
|
||||
return nil
|
||||
case TypeSimpleList:
|
||||
_, itemType, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if itemType != TypeInt8 {
|
||||
return fmt.Errorf("taf: simple list 元素类型异常: 0x%02x", byte(itemType))
|
||||
}
|
||||
n, err := r.readLength()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.buf.Seek(n, io.SeekCurrent)
|
||||
return err
|
||||
default:
|
||||
return fmt.Errorf("taf: 未知字段类型 0x%02x", byte(typ))
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) readLength() (int64, error) {
|
||||
_, typ, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.ReadIntValue(typ)
|
||||
}
|
||||
|
||||
func (r *Reader) findTag(target int, required bool) (Type, bool, error) {
|
||||
for {
|
||||
pos := r.Position()
|
||||
tag, typ, err := r.ReadHead()
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) && !required {
|
||||
return 0, false, nil
|
||||
}
|
||||
return 0, false, err
|
||||
}
|
||||
if typ == TypeStructEnd {
|
||||
_ = r.Seek(pos)
|
||||
if required {
|
||||
return 0, false, fmt.Errorf("taf: 未找到 tag=%d,已到结构尾", target)
|
||||
}
|
||||
return 0, false, nil
|
||||
}
|
||||
if tag == target {
|
||||
return typ, true, nil
|
||||
}
|
||||
if tag > target {
|
||||
_ = r.Seek(pos)
|
||||
if required {
|
||||
return 0, false, fmt.Errorf("taf: 未找到 tag=%d,遇到 tag=%d", target, tag)
|
||||
}
|
||||
return 0, false, nil
|
||||
}
|
||||
if err := r.Skip(typ); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) ReadInt64(tag int, required bool, def int64) (int64, error) {
|
||||
typ, ok, err := r.findTag(tag, required)
|
||||
if err != nil || !ok {
|
||||
return def, err
|
||||
}
|
||||
return r.ReadIntValue(typ)
|
||||
}
|
||||
|
||||
func (r *Reader) ReadInt32(tag int, required bool, def int32) (int32, error) {
|
||||
value, err := r.ReadInt64(tag, required, int64(def))
|
||||
return int32(value), err
|
||||
}
|
||||
|
||||
func (r *Reader) ReadString(tag int, required bool, def string) (string, error) {
|
||||
typ, ok, err := r.findTag(tag, required)
|
||||
if err != nil || !ok {
|
||||
return def, err
|
||||
}
|
||||
return r.ReadStringValue(typ)
|
||||
}
|
||||
|
||||
func (r *Reader) ReadBytes(tag int, required bool, def []byte) ([]byte, error) {
|
||||
typ, ok, err := r.findTag(tag, required)
|
||||
if err != nil || !ok {
|
||||
return def, err
|
||||
}
|
||||
return r.ReadBytesValue(typ)
|
||||
}
|
||||
|
||||
func (r *Reader) ReadMapStringString(tag int, required bool) (map[string]string, error) {
|
||||
typ, ok, err := r.findTag(tag, required)
|
||||
if err != nil || !ok {
|
||||
return map[string]string{}, err
|
||||
}
|
||||
if typ != TypeMap {
|
||||
return nil, fmt.Errorf("taf: tag=%d 不是 map: 0x%02x", tag, byte(typ))
|
||||
}
|
||||
n, err := r.readLength()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make(map[string]string, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
_, kt, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k, err := r.ReadStringValue(kt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, vt, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v, err := r.ReadStringValue(vt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[k] = v
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *Reader) ReadMapStringBytes(tag int, required bool) (map[string][]byte, error) {
|
||||
typ, ok, err := r.findTag(tag, required)
|
||||
if err != nil || !ok {
|
||||
return map[string][]byte{}, err
|
||||
}
|
||||
if typ != TypeMap {
|
||||
return nil, fmt.Errorf("taf: tag=%d 不是 map: 0x%02x", tag, byte(typ))
|
||||
}
|
||||
n, err := r.readLength()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make(map[string][]byte, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
_, kt, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k, err := r.ReadStringValue(kt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, vt, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v, err := r.ReadBytesValue(vt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[k] = v
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *Reader) ReadStruct(tag int, required bool, obj Unmarshaler) (bool, error) {
|
||||
typ, ok, err := r.findTag(tag, required)
|
||||
if err != nil || !ok {
|
||||
return false, err
|
||||
}
|
||||
if typ != TypeStructBegin {
|
||||
return false, fmt.Errorf("taf: tag=%d 不是结构体: 0x%02x", tag, byte(typ))
|
||||
}
|
||||
if err := obj.ReadFrom(r); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, r.SkipToStructEnd()
|
||||
}
|
||||
|
||||
func (r *Reader) ReadListStruct(tag int, required bool, factory func() Unmarshaler) ([]Unmarshaler, error) {
|
||||
typ, ok, err := r.findTag(tag, required)
|
||||
if err != nil || !ok {
|
||||
return nil, err
|
||||
}
|
||||
if typ != TypeList {
|
||||
return nil, fmt.Errorf("taf: tag=%d 不是 list: 0x%02x", tag, byte(typ))
|
||||
}
|
||||
n, err := r.readLength()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Unmarshaler, 0, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
_, it, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if it != TypeStructBegin {
|
||||
if err := r.Skip(it); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
obj := factory()
|
||||
if err := obj.ReadFrom(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.SkipToStructEnd(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, obj)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *Reader) SkipToStructEnd() error {
|
||||
for {
|
||||
_, typ, err := r.ReadHead()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if typ == TypeStructEnd {
|
||||
return nil
|
||||
}
|
||||
if err := r.Skip(typ); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package taf_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"huya-go/internal/huya"
|
||||
"huya-go/internal/taf"
|
||||
)
|
||||
|
||||
func TestStructRoundTrip(t *testing.T) {
|
||||
user := &huya.ActivityUserID{
|
||||
UID: 123456,
|
||||
GUID: "guid-1",
|
||||
HuyaUA: huya.ActivityWebUA,
|
||||
Cookie: "yyuid=123456;",
|
||||
TokenType: 0,
|
||||
}
|
||||
w := taf.NewWriter()
|
||||
if err := w.WriteStruct(0, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got huya.ActivityUserID
|
||||
r := taf.NewReader(w.Bytes())
|
||||
ok, err := r.ReadStruct(0, true, &got)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !ok || got.UID != user.UID || got.GUID != user.GUID || got.HuyaUA != user.HuyaUA {
|
||||
t.Fatalf("round trip mismatch: %#v", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package taf
|
||||
|
||||
// Type 是虎牙前端使用的 TAF/JCE 字段类型。
|
||||
type Type byte
|
||||
|
||||
const (
|
||||
TypeInt8 Type = 0x00
|
||||
TypeInt16 Type = 0x01
|
||||
TypeInt32 Type = 0x02
|
||||
TypeInt64 Type = 0x03
|
||||
TypeFloat Type = 0x04
|
||||
TypeDouble Type = 0x05
|
||||
TypeString1 Type = 0x06
|
||||
TypeString4 Type = 0x07
|
||||
TypeMap Type = 0x08
|
||||
TypeList Type = 0x09
|
||||
TypeStructBegin Type = 0x0a
|
||||
TypeStructEnd Type = 0x0b
|
||||
TypeZero Type = 0x0c
|
||||
TypeSimpleList Type = 0x0d
|
||||
)
|
||||
|
||||
// Marshaler 表示可编码成 TAF 结构体字段的对象。
|
||||
type Marshaler interface {
|
||||
WriteTo(*Writer) error
|
||||
}
|
||||
|
||||
// Unmarshaler 表示可从 TAF 结构体字段读取自身的对象。
|
||||
type Unmarshaler interface {
|
||||
ReadFrom(*Reader) error
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package taf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// Writer 负责按 TAF/JCE 格式编码字段。
|
||||
type Writer struct {
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
func NewWriter() *Writer {
|
||||
return &Writer{}
|
||||
}
|
||||
|
||||
func (w *Writer) Bytes() []byte {
|
||||
return w.buf.Bytes()
|
||||
}
|
||||
|
||||
func (w *Writer) WriteHead(tag int, typ Type) error {
|
||||
if tag < 0 || tag > 255 {
|
||||
return fmt.Errorf("taf: tag 超出范围: %d", tag)
|
||||
}
|
||||
if tag < 15 {
|
||||
return w.buf.WriteByte(byte(tag<<4) | byte(typ))
|
||||
}
|
||||
if err := w.buf.WriteByte(0xf0 | byte(typ)); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.buf.WriteByte(byte(tag))
|
||||
}
|
||||
|
||||
func (w *Writer) WriteInt8(tag int, value int64) error {
|
||||
if value == 0 {
|
||||
return w.WriteHead(tag, TypeZero)
|
||||
}
|
||||
if value < math.MinInt8 || value > math.MaxInt8 {
|
||||
return fmt.Errorf("taf: int8 溢出: %d", value)
|
||||
}
|
||||
if err := w.WriteHead(tag, TypeInt8); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.buf.WriteByte(byte(int8(value)))
|
||||
}
|
||||
|
||||
func (w *Writer) WriteInt16(tag int, value int64) error {
|
||||
if value >= math.MinInt8 && value <= math.MaxInt8 {
|
||||
return w.WriteInt8(tag, value)
|
||||
}
|
||||
if value < math.MinInt16 || value > math.MaxInt16 {
|
||||
return fmt.Errorf("taf: int16 溢出: %d", value)
|
||||
}
|
||||
if err := w.WriteHead(tag, TypeInt16); err != nil {
|
||||
return err
|
||||
}
|
||||
var b [2]byte
|
||||
binary.BigEndian.PutUint16(b[:], uint16(int16(value)))
|
||||
_, err := w.buf.Write(b[:])
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *Writer) WriteInt32(tag int, value int64) error {
|
||||
if value >= math.MinInt16 && value <= math.MaxInt16 {
|
||||
return w.WriteInt16(tag, value)
|
||||
}
|
||||
if value < math.MinInt32 || value > math.MaxInt32 {
|
||||
return fmt.Errorf("taf: int32 溢出: %d", value)
|
||||
}
|
||||
if err := w.WriteHead(tag, TypeInt32); err != nil {
|
||||
return err
|
||||
}
|
||||
var b [4]byte
|
||||
binary.BigEndian.PutUint32(b[:], uint32(int32(value)))
|
||||
_, err := w.buf.Write(b[:])
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *Writer) WriteInt64(tag int, value int64) error {
|
||||
if value >= math.MinInt32 && value <= math.MaxInt32 {
|
||||
return w.WriteInt32(tag, value)
|
||||
}
|
||||
if err := w.WriteHead(tag, TypeInt64); err != nil {
|
||||
return err
|
||||
}
|
||||
var b [8]byte
|
||||
binary.BigEndian.PutUint64(b[:], uint64(value))
|
||||
_, err := w.buf.Write(b[:])
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *Writer) WriteBool(tag int, value bool) error {
|
||||
if value {
|
||||
return w.WriteInt8(tag, 1)
|
||||
}
|
||||
return w.WriteInt8(tag, 0)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteFloat(tag int, value float32) error {
|
||||
if err := w.WriteHead(tag, TypeFloat); err != nil {
|
||||
return err
|
||||
}
|
||||
var b [4]byte
|
||||
binary.BigEndian.PutUint32(b[:], math.Float32bits(value))
|
||||
_, err := w.buf.Write(b[:])
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *Writer) WriteDouble(tag int, value float64) error {
|
||||
if err := w.WriteHead(tag, TypeDouble); err != nil {
|
||||
return err
|
||||
}
|
||||
var b [8]byte
|
||||
binary.BigEndian.PutUint64(b[:], math.Float64bits(value))
|
||||
_, err := w.buf.Write(b[:])
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *Writer) WriteString(tag int, value string) error {
|
||||
raw := []byte(value)
|
||||
if len(raw) > math.MaxUint8 {
|
||||
if err := w.WriteHead(tag, TypeString4); err != nil {
|
||||
return err
|
||||
}
|
||||
var b [4]byte
|
||||
binary.BigEndian.PutUint32(b[:], uint32(len(raw)))
|
||||
if _, err := w.buf.Write(b[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := w.buf.Write(raw)
|
||||
return err
|
||||
}
|
||||
if err := w.WriteHead(tag, TypeString1); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.buf.WriteByte(byte(len(raw))); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := w.buf.Write(raw)
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *Writer) WriteBytes(tag int, value []byte) error {
|
||||
if err := w.WriteHead(tag, TypeSimpleList); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteHead(0, TypeInt8); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(0, int64(len(value))); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := w.buf.Write(value)
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *Writer) WriteStructBegin(tag int) error {
|
||||
return w.WriteHead(tag, TypeStructBegin)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteStructEnd() error {
|
||||
return w.WriteHead(0, TypeStructEnd)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteStruct(tag int, value Marshaler) error {
|
||||
if err := w.WriteStructBegin(tag); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := value.WriteTo(w); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.WriteStructEnd()
|
||||
}
|
||||
|
||||
func (w *Writer) WriteMapStringString(tag int, value map[string]string) error {
|
||||
if value == nil {
|
||||
value = map[string]string{}
|
||||
}
|
||||
if err := w.WriteHead(tag, TypeMap); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(0, int64(len(value))); err != nil {
|
||||
return err
|
||||
}
|
||||
for k, v := range value {
|
||||
if err := w.WriteString(0, k); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Writer) WriteMapStringBytes(tag int, value map[string][]byte) error {
|
||||
if value == nil {
|
||||
value = map[string][]byte{}
|
||||
}
|
||||
if err := w.WriteHead(tag, TypeMap); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(0, int64(len(value))); err != nil {
|
||||
return err
|
||||
}
|
||||
for k, v := range value {
|
||||
if err := w.WriteString(0, k); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteBytes(1, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Writer) WriteListStruct(tag int, values []Marshaler) error {
|
||||
if err := w.WriteHead(tag, TypeList); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(0, int64(len(values))); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, value := range values {
|
||||
if err := w.WriteStruct(0, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user