github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/binary.go (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package thrift
    18  
    19  import (
    20  	"encoding/base64"
    21  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"math"
    26  	"reflect"
    27  	"strconv"
    28  	"strings"
    29  	"sync"
    30  	"unsafe"
    31  
    32  	"github.com/cloudwego/dynamicgo/internal/json"
    33  	"github.com/cloudwego/dynamicgo/internal/primitive"
    34  	"github.com/cloudwego/dynamicgo/internal/rt"
    35  	"github.com/cloudwego/dynamicgo/internal/util"
    36  	"github.com/cloudwego/dynamicgo/meta"
    37  )
    38  
    39  // memory resize factor
    40  const (
    41  	// new = old + old >> growSliceFactor
    42  	growBufferFactor = 1
    43  
    44  	defaultBufferSize = 4096
    45  
    46  	msgHeaderFixedLen = 4 + 4 + 4 + 3
    47  	msgFooterFixedLen = 1
    48  )
    49  
    50  // TMessageType is the type of message
    51  type TMessageType int32
    52  
    53  const (
    54  	INVALID_TMESSAGE_TYPE TMessageType = 0
    55  	CALL                  TMessageType = 1
    56  	REPLY                 TMessageType = 2
    57  	EXCEPTION             TMessageType = 3
    58  	ONEWAY                TMessageType = 4
    59  )
    60  
    61  var (
    62  	errDismatchPrimitive = meta.NewError(meta.ErrDismatchType, "dismatch primitive types", nil)
    63  	errInvalidDataSize   = meta.NewError(meta.ErrInvalidParam, "invalid data size", nil)
    64  	errInvalidVersion    = meta.NewError(meta.ErrInvalidParam, "invalid version in ReadMessageBegin", nil)
    65  	errExceedDepthLimit  = meta.NewError(meta.ErrStackOverflow, "exceed depth limit", nil)
    66  	errInvalidDataType   = meta.NewError(meta.ErrRead, "invalid data type", nil)
    67  	errUnknonwField      = meta.NewError(meta.ErrUnknownField, "unknown field", nil)
    68  	errUnsupportedType   = meta.NewError(meta.ErrUnsupportedType, "unsupported type", nil)
    69  	errNotImplemented    = meta.NewError(meta.ErrNotImplemented, "not implemted type", nil)
    70  )
    71  
    72  // must be strict read & strict write
    73  var (
    74  	bpPool = sync.Pool{
    75  		New: func() interface{} {
    76  			return &BinaryProtocol{
    77  				Buf: make([]byte, 0, defaultBufferSize),
    78  			}
    79  		},
    80  	}
    81  )
    82  
    83  // NewBinaryProtocol get a new binary protocol from sync.Pool.
    84  func NewBinaryProtocol(buf []byte) *BinaryProtocol {
    85  	bp := bpPool.Get().(*BinaryProtocol)
    86  	bp.Buf = buf
    87  	return bp
    88  }
    89  
    90  // NewBinaryProtocolBuffer gets a new binary protocol from sync.Pool
    91  // and reuse the buffer in pool
    92  func NewBinaryProtocolBuffer() *BinaryProtocol {
    93  	bp := bpPool.Get().(*BinaryProtocol)
    94  	return bp
    95  }
    96  
    97  // FreeBinaryProtocol resets the buffer and puts the binary protocol back to sync.Pool
    98  func FreeBinaryProtocolBuffer(bp *BinaryProtocol) {
    99  	bp.Reset()
   100  	bpPool.Put(bp)
   101  }
   102  
   103  // Recycle put the protocol back to sync.Pool
   104  func (p *BinaryProtocol) Recycle() {
   105  	p.Reset()
   106  	bpPool.Put(p)
   107  }
   108  
   109  // BinaryProtocol implements the BinaryProtocol
   110  // see https://github.com/apache/thrift/blob/master/doc/specs/thrift-binary-protocol.md
   111  type BinaryProtocol struct {
   112  	Buf  []byte
   113  	Read int
   114  }
   115  
   116  // Reset resets the buffer and read position
   117  func (p *BinaryProtocol) Reset() {
   118  	p.Read = 0
   119  	p.Buf = p.Buf[:0]
   120  }
   121  
   122  // RawBuf returns the raw buffer of the protocol
   123  func (p BinaryProtocol) RawBuf() []byte {
   124  	return p.Buf
   125  }
   126  
   127  // Left returns the left bytes to read
   128  func (p BinaryProtocol) Left() int {
   129  	return len(p.Buf) - p.Read
   130  }
   131  
   132  /**
   133   * Message related methods
   134   */
   135  
   136  // GetBinaryMessageHeaderAndFooter writes the message parameters into header and footer
   137  func GetBinaryMessageHeaderAndFooter(methodName string, msgTyp TMessageType, structID FieldID, seqID int32) (header []byte, footer []byte, err error) {
   138  	var writer = BinaryProtocol{}
   139  
   140  	// write header
   141  	header = make([]byte, 0, msgHeaderFixedLen+len(methodName))
   142  	writer.Buf = header
   143  	err = writer.WriteMessageBegin(methodName, msgTyp, seqID)
   144  	if err != nil {
   145  		return
   146  	}
   147  	err = writer.WriteStructBegin("")
   148  	if err != nil {
   149  		return
   150  	}
   151  	err = writer.WriteFieldBegin("", STRUCT, structID)
   152  	if err != nil {
   153  		return
   154  	}
   155  	header = writer.Buf
   156  
   157  	// write footer
   158  	footer = make([]byte, 0, msgFooterFixedLen)
   159  	writer.Buf = footer
   160  	err = writer.WriteFieldEnd()
   161  	if err != nil {
   162  		return
   163  	}
   164  	err = writer.WriteStructEnd()
   165  	if err != nil {
   166  		return
   167  	}
   168  	err = writer.WriteMessageEnd()
   169  	if err != nil {
   170  		return
   171  	}
   172  	footer = writer.Buf
   173  
   174  	return
   175  }
   176  
   177  // WrapBinaryMessage wraps the message with header and footer and body
   178  func WrapBinaryBody(body []byte, methodName string, msgTyp TMessageType, structID FieldID, seqID int32) ([]byte, error) {
   179  	// write header
   180  	buf := make([]byte, 0, msgHeaderFixedLen+len(methodName)+len(body)+msgFooterFixedLen)
   181  	writer := BinaryProtocol{Buf: buf}
   182  	writer.WriteMessageBegin(methodName, msgTyp, seqID)
   183  	writer.WriteStructBegin("")
   184  	writer.WriteFieldBegin("", STRUCT, structID)
   185  	writer.Buf = append(writer.Buf, body...)
   186  	writer.WriteFieldEnd()
   187  	writer.WriteStructEnd()
   188  	writer.WriteMessageEnd()
   189  	return writer.Buf, nil
   190  }
   191  
   192  // UnwrapBinaryMessage unwraps the message parameters from the buf
   193  func UnwrapBinaryMessage(buf []byte) (name string, callType TMessageType, seqID int32, structID FieldID, body []byte, err error) {
   194  	var reader = BinaryProtocol{
   195  		Buf: buf,
   196  	}
   197  	return reader.UnwrapBody()
   198  }
   199  
   200  // UnwrapBody unwraps the  message parameters from its buf
   201  func (p BinaryProtocol) UnwrapBody() (string, TMessageType, int32, FieldID, []byte, error) {
   202  	name, rTyp, seqID, err := p.ReadMessageBegin(false)
   203  	if err != nil {
   204  		return name, rTyp, seqID, 0, nil, err
   205  	}
   206  	// read the success struct
   207  	_, _, structID, err := p.ReadFieldBegin()
   208  	if err != nil {
   209  		return name, rTyp, seqID, structID, nil, err
   210  	}
   211  	// there's alway a struct stop by success struct
   212  	if p.Read > len(p.Buf)-1 {
   213  		return name, rTyp, seqID, structID, nil, io.EOF
   214  	}
   215  	return name, rTyp, seqID, structID, p.Buf[p.Read : len(p.Buf)-1], err
   216  }
   217  
   218  /**
   219   * Writing Methods
   220   */
   221  
   222  // WriteMessageBegin ...
   223  func (p *BinaryProtocol) WriteMessageBegin(name string, typeID TMessageType, seqID int32) error {
   224  	version := uint32(VERSION_1) | uint32(typeID)
   225  	e := p.WriteI32(int32(version))
   226  	if e != nil {
   227  		return e
   228  	}
   229  	e = p.WriteString(name)
   230  	if e != nil {
   231  		return e
   232  	}
   233  	e = p.WriteI32(seqID)
   234  	return e
   235  }
   236  
   237  // WriteMessageEnd ...
   238  func (p *BinaryProtocol) WriteMessageEnd() error {
   239  	return nil
   240  }
   241  
   242  // WriteStructBegin ...
   243  func (p *BinaryProtocol) WriteStructBegin(name string) error {
   244  	return nil
   245  }
   246  
   247  // WriteStructEnd ...
   248  func (p *BinaryProtocol) WriteStructEnd() error {
   249  	return p.WriteFieldStop()
   250  }
   251  
   252  // WriteFieldBegin ...
   253  func (p *BinaryProtocol) WriteFieldBegin(name string, typeID Type, id FieldID) error {
   254  	e := p.WriteByte(byte(typeID))
   255  	if e != nil {
   256  		return e
   257  	}
   258  	e = p.WriteI16(int16(id))
   259  	return e
   260  }
   261  
   262  // WriteFieldEnd ...
   263  func (p *BinaryProtocol) WriteFieldEnd() error {
   264  	return nil
   265  }
   266  
   267  // WriteFieldStop ...
   268  func (p *BinaryProtocol) WriteFieldStop() error {
   269  	e := p.WriteByte(byte(STOP))
   270  	return e
   271  }
   272  
   273  // WriteMapBegin ...
   274  func (p *BinaryProtocol) WriteMapBegin(keyType, valueType Type, size int) error {
   275  	e := p.WriteByte(byte(keyType))
   276  	if e != nil {
   277  		return e
   278  	}
   279  	e = p.WriteByte(byte(valueType))
   280  	if e != nil {
   281  		return e
   282  	}
   283  	e = p.WriteI32(int32(size))
   284  	return e
   285  }
   286  
   287  // WriteMapBeginWithSizePos writes the map begin, and return the buffer position of the size data
   288  func (p *BinaryProtocol) WriteMapBeginWithSizePos(keyType, valueType Type, size int) (int, error) {
   289  	e := p.WriteByte(byte(keyType))
   290  	if e != nil {
   291  		return 0, e
   292  	}
   293  	e = p.WriteByte(byte(valueType))
   294  	if e != nil {
   295  		return 0, e
   296  	}
   297  	re := len(p.Buf)
   298  	e = p.WriteI32(int32(size))
   299  	return re, e
   300  }
   301  
   302  // WriteMapEnd ...
   303  func (p *BinaryProtocol) WriteMapEnd() error {
   304  	return nil
   305  }
   306  
   307  // WriteListBegin ...
   308  func (p *BinaryProtocol) WriteListBegin(elemType Type, size int) error {
   309  	e := p.WriteByte(byte(elemType))
   310  	if e != nil {
   311  		return e
   312  	}
   313  	e = p.WriteI32(int32(size))
   314  	return e
   315  }
   316  
   317  // WriteListBeginWithSizePos writes the list begin, and return the buffer position of the size data
   318  func (p *BinaryProtocol) WriteListBeginWithSizePos(elemType Type, size int) (int, error) {
   319  	e := p.WriteByte(byte(elemType))
   320  	if e != nil {
   321  		return 0, e
   322  	}
   323  	re := len(p.Buf)
   324  	e = p.WriteI32(int32(size))
   325  	return re, e
   326  }
   327  
   328  // WriteListEnd ...
   329  func (p *BinaryProtocol) WriteListEnd() error {
   330  	return nil
   331  }
   332  
   333  // WriteSetBegin ...
   334  func (p *BinaryProtocol) WriteSetBegin(elemType Type, size int) error {
   335  	e := p.WriteByte(byte(elemType))
   336  	if e != nil {
   337  		return e
   338  	}
   339  	e = p.WriteI32(int32(size))
   340  	return e
   341  }
   342  
   343  // WriteSetEnd ...
   344  func (p *BinaryProtocol) WriteSetEnd() error {
   345  	return nil
   346  }
   347  
   348  // WriteBool ...
   349  func (p *BinaryProtocol) WriteBool(value bool) error {
   350  	if value {
   351  		return p.WriteByte(1)
   352  	}
   353  	return p.WriteByte(0)
   354  }
   355  
   356  // WriteByte ...
   357  func (p *BinaryProtocol) WriteByte(value byte) error {
   358  	p.Buf = append(p.Buf, byte(value))
   359  	return nil
   360  }
   361  
   362  // WriteI16 ...
   363  func (p *BinaryProtocol) WriteI16(value int16) error {
   364  	v, err := p.malloc(2)
   365  	if err != nil {
   366  		return err
   367  	}
   368  	binary.BigEndian.PutUint16(v, uint16(value))
   369  	return err
   370  }
   371  
   372  // ModifyI16 write int32 into the buffer at the given position
   373  func (p *BinaryProtocol) ModifyI32(pos int, value int32) error {
   374  	old := len(p.Buf)
   375  	if old < pos+4 {
   376  		return fmt.Errorf("not enough space to modify i32")
   377  	}
   378  	p.Buf = p.Buf[:pos]
   379  	p.WriteI32(value)
   380  	p.Buf = p.Buf[:old]
   381  	return nil
   382  }
   383  
   384  // WriteI32 ...
   385  func (p *BinaryProtocol) WriteI32(value int32) error {
   386  	v, err := p.malloc(4)
   387  	if err != nil {
   388  		return err
   389  	}
   390  	binary.BigEndian.PutUint32(v, uint32(value))
   391  	return err
   392  }
   393  
   394  // WriteI64 ...
   395  func (p *BinaryProtocol) WriteI64(value int64) error {
   396  	v, err := p.malloc(8)
   397  	if err != nil {
   398  		return err
   399  	}
   400  	binary.BigEndian.PutUint64(v, uint64(value))
   401  	return err
   402  }
   403  
   404  // WriteInt ...
   405  func (p *BinaryProtocol) WriteInt(t Type, value int) error {
   406  	switch t {
   407  	case I08:
   408  		return p.WriteByte(byte(value))
   409  	case I16:
   410  		return p.WriteI16(int16(value))
   411  	case I32:
   412  		return p.WriteI32(int32(value))
   413  	case I64:
   414  		return p.WriteI64(int64(value))
   415  	default:
   416  		return errInvalidDataType
   417  	}
   418  }
   419  
   420  // WriteDouble ...
   421  func (p *BinaryProtocol) WriteDouble(value float64) error {
   422  	return p.WriteI64(int64(math.Float64bits(value)))
   423  }
   424  
   425  // WriteString ...
   426  func (p *BinaryProtocol) WriteString(value string) error {
   427  	len := len(value)
   428  	e := p.WriteI32(int32(len))
   429  	if e != nil {
   430  		return e
   431  	}
   432  	p.Buf = append(p.Buf, value...)
   433  	return nil
   434  }
   435  
   436  // WriteBinary ...
   437  func (p *BinaryProtocol) WriteBinary(value []byte) error {
   438  	e := p.WriteI32(int32(len(value)))
   439  	if e != nil {
   440  		return e
   441  	}
   442  	p.Buf = append(p.Buf, value...)
   443  	return nil
   444  }
   445  
   446  // malloc ...
   447  func (p *BinaryProtocol) malloc(size int) ([]byte, error) {
   448  	if size <= 0 {
   449  		panic(errors.New("invalid size"))
   450  	}
   451  
   452  	l := len(p.Buf)
   453  	c := cap(p.Buf)
   454  	d := l + size
   455  
   456  	if d > c {
   457  		c += c >> growBufferFactor
   458  		if d > c {
   459  			c = d * 2
   460  		}
   461  		buf := rt.Growslice(byteType, *(*rt.GoSlice)(unsafe.Pointer(&p.Buf)), c)
   462  		p.Buf = *(*[]byte)(unsafe.Pointer(&buf))
   463  	}
   464  	p.Buf = (p.Buf)[:d]
   465  
   466  	return (p.Buf)[l:d], nil
   467  }
   468  
   469  // WriteDefaultOrEmpty write default value if any, otherwise write zero value
   470  func (p *BinaryProtocol) WriteDefaultOrEmpty(field *FieldDescriptor) error {
   471  	if dv := field.DefaultValue(); dv != nil {
   472  		p.Buf = append(p.Buf, dv.ThriftBinary()...)
   473  		return nil
   474  	}
   475  	return p.WriteEmpty(field.Type())
   476  }
   477  
   478  // WriteEmpty write zero value
   479  func (p *BinaryProtocol) WriteEmpty(desc *TypeDescriptor) error {
   480  	switch desc.Type() {
   481  	case BOOL:
   482  		return p.WriteBool(false)
   483  	case BYTE:
   484  		return p.WriteByte(0)
   485  	case I16:
   486  		return p.WriteI16(0)
   487  	case I32:
   488  		return p.WriteI32(0)
   489  	case I64:
   490  		return p.WriteI64(0)
   491  	case DOUBLE:
   492  		return p.WriteDouble(0)
   493  	case STRING:
   494  		return p.WriteString("")
   495  	case LIST, SET:
   496  		if err := p.WriteListBegin(desc.Elem().Type(), 0); err != nil {
   497  			return err
   498  		}
   499  		return p.WriteListEnd()
   500  	case MAP:
   501  		if err := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), 0); err != nil {
   502  			return err
   503  		}
   504  		return p.WriteMapEnd()
   505  	case STRUCT:
   506  		// NOTICE: to avoid self-cycled type dead loop here, just write empty struct
   507  		return p.WriteStructEnd()
   508  	default:
   509  		return errors.New("invalid type")
   510  	}
   511  }
   512  
   513  /**
   514   * Reading methods
   515   */
   516  
   517  // ReadMessageBegin ...
   518  func (p *BinaryProtocol) ReadMessageBegin(copyString bool) (name string, typeID TMessageType, seqID int32, err error) {
   519  	size, e := p.ReadI32()
   520  	if e != nil {
   521  		return "", typeID, 0, errInvalidVersion
   522  	}
   523  	if size > 0 {
   524  		return name, typeID, seqID, errInvalidVersion
   525  	}
   526  	typeID = TMessageType(size & 0x0ff)
   527  	version := int64(int64(size) & VERSION_MASK)
   528  	if version != VERSION_1 {
   529  		return name, typeID, seqID, errInvalidVersion
   530  	}
   531  	name, e = p.ReadString(copyString)
   532  	if e != nil {
   533  		return name, typeID, seqID, errInvalidVersion
   534  	}
   535  	seqID, e = p.ReadI32()
   536  	if e != nil {
   537  		return name, typeID, seqID, errInvalidVersion
   538  	}
   539  	return name, typeID, seqID, nil
   540  }
   541  
   542  // ReadMessageEnd ...
   543  func (p *BinaryProtocol) ReadMessageEnd() error {
   544  	return nil
   545  }
   546  
   547  // ReadStructBegin ...
   548  func (p *BinaryProtocol) ReadStructBegin() (name string, err error) {
   549  	return
   550  }
   551  
   552  // ReadStructEnd ...
   553  func (p *BinaryProtocol) ReadStructEnd() error {
   554  	return nil
   555  }
   556  
   557  // ReadFieldBegin ...
   558  func (p *BinaryProtocol) ReadFieldBegin() (name string, typeID Type, id FieldID, err error) {
   559  	t, err := p.ReadByte()
   560  	typeID = Type(t)
   561  	if err != nil {
   562  		return name, typeID, id, err
   563  	}
   564  	if !typeID.Valid() {
   565  		return "", 0, 0, errInvalidDataType
   566  	}
   567  	if t != byte(STOP) {
   568  		var x int16
   569  		x, err = p.ReadI16()
   570  		id = FieldID(x)
   571  	}
   572  	return name, typeID, id, err
   573  }
   574  
   575  // ReadFieldEnd ...
   576  func (p *BinaryProtocol) ReadFieldEnd() error {
   577  	return nil
   578  }
   579  
   580  // ReadMapBegin ...
   581  func (p *BinaryProtocol) ReadMapBegin() (kType, vType Type, size int, err error) {
   582  	k, e := p.ReadByte()
   583  	if e != nil {
   584  		err = e
   585  		return
   586  	}
   587  	kType = Type(k)
   588  	if !kType.Valid() {
   589  		return 0, 0, 0, errInvalidDataType
   590  	}
   591  
   592  	v, e := p.ReadByte()
   593  	if e != nil {
   594  		err = e
   595  		return
   596  	}
   597  	vType = Type(v)
   598  	if !vType.Valid() {
   599  		return 0, 0, 0, errInvalidDataType
   600  	}
   601  
   602  	size32, e := p.ReadI32()
   603  	if e != nil {
   604  		err = e
   605  		return
   606  	}
   607  	if size32 < 0 {
   608  		err = errInvalidDataSize
   609  		return
   610  	}
   611  	size = int(size32)
   612  	return kType, vType, size, nil
   613  }
   614  
   615  // ReadMapEnd ...
   616  func (p *BinaryProtocol) ReadMapEnd() error {
   617  	return nil
   618  }
   619  
   620  // ReadListBegin ...
   621  func (p *BinaryProtocol) ReadListBegin() (elemType Type, size int, err error) {
   622  	b, e := p.ReadByte()
   623  	if e != nil {
   624  		err = e
   625  		return
   626  	}
   627  
   628  	elemType = Type(b)
   629  	if !elemType.Valid() {
   630  		return 0, 0, errInvalidDataType
   631  	}
   632  
   633  	size32, e := p.ReadI32()
   634  	if e != nil {
   635  		err = e
   636  		return
   637  	}
   638  	if size32 < 0 {
   639  		err = errInvalidDataSize
   640  		return
   641  	}
   642  	size = int(size32)
   643  
   644  	return
   645  }
   646  
   647  // ReadListEnd ...
   648  func (p *BinaryProtocol) ReadListEnd() error {
   649  	return nil
   650  }
   651  
   652  // ReadSetBegin ...
   653  func (p *BinaryProtocol) ReadSetBegin() (elemType Type, size int, err error) {
   654  	b, e := p.ReadByte()
   655  	if e != nil {
   656  		err = e
   657  		return
   658  	}
   659  
   660  	elemType = Type(b)
   661  	if !elemType.Valid() {
   662  		return 0, 0, errInvalidDataType
   663  	}
   664  
   665  	size32, e := p.ReadI32()
   666  	if e != nil {
   667  		err = e
   668  		return
   669  	}
   670  	if size32 < 0 {
   671  		err = errInvalidDataSize
   672  		return
   673  	}
   674  	size = int(size32)
   675  	return elemType, size, nil
   676  }
   677  
   678  // ReadSetEnd ...
   679  func (p *BinaryProtocol) ReadSetEnd() error {
   680  	return nil
   681  }
   682  
   683  // ReadBool ...
   684  func (p *BinaryProtocol) ReadBool() (bool, error) {
   685  	b, e := p.ReadByte()
   686  	v := true
   687  	if b != 1 {
   688  		v = false
   689  	}
   690  	return v, e
   691  }
   692  
   693  // ReadByte ...
   694  func (p *BinaryProtocol) ReadByte() (value byte, err error) {
   695  	buf, err := p.next(1)
   696  	if err != nil {
   697  		return value, err
   698  	}
   699  	return byte(buf[0]), err
   700  }
   701  
   702  // ReadI16 ...
   703  func (p *BinaryProtocol) ReadI16() (value int16, err error) {
   704  	buf, err := p.next(2)
   705  	if err != nil {
   706  		return value, err
   707  	}
   708  	value = int16(binary.BigEndian.Uint16(buf))
   709  	return value, err
   710  }
   711  
   712  // ReadI32 ...
   713  func (p *BinaryProtocol) ReadI32() (value int32, err error) {
   714  	buf, err := p.next(4)
   715  	if err != nil {
   716  		return value, err
   717  	}
   718  	value = int32(binary.BigEndian.Uint32(buf))
   719  	return value, err
   720  }
   721  
   722  // ReadI64 ...
   723  func (p *BinaryProtocol) ReadI64() (value int64, err error) {
   724  	buf, err := p.next(8)
   725  	if err != nil {
   726  		return value, err
   727  	}
   728  	value = int64(binary.BigEndian.Uint64(buf))
   729  	return value, err
   730  }
   731  
   732  // ReadInt ...
   733  func (p *BinaryProtocol) ReadInt(t Type) (value int, err error) {
   734  	switch t {
   735  	case I08:
   736  		n, err := p.ReadByte()
   737  		return int(n), err
   738  	case I16:
   739  		n, err := p.ReadI16()
   740  		return int(n), err
   741  	case I32:
   742  		n, err := p.ReadI32()
   743  		return int(n), err
   744  	case I64:
   745  		n, err := p.ReadI64()
   746  		return int(n), err
   747  	default:
   748  		return 0, errInvalidDataType
   749  	}
   750  }
   751  
   752  // ReadDouble ...
   753  func (p *BinaryProtocol) ReadDouble() (value float64, err error) {
   754  	buf, err := p.next(8)
   755  	if err != nil {
   756  		return value, err
   757  	}
   758  	value = math.Float64frombits(binary.BigEndian.Uint64(buf))
   759  	return value, err
   760  }
   761  
   762  // ReadString ...
   763  func (p *BinaryProtocol) ReadString(copy bool) (value string, err error) {
   764  	size, e := p.ReadI32()
   765  	if e != nil {
   766  		return "", e
   767  	}
   768  	if size < 0 || int(size) > len(p.Buf)-p.Read {
   769  		err = errInvalidDataSize
   770  		return
   771  	}
   772  
   773  	if copy {
   774  		value = string(*(*[]byte)(unsafe.Pointer(&rt.GoSlice{
   775  			Ptr: rt.IndexPtr(*(*unsafe.Pointer)(unsafe.Pointer(&p.Buf)), byteTypeSize, p.Read),
   776  			Len: int(size),
   777  			Cap: int(size),
   778  		})))
   779  	} else {
   780  		v := (*rt.GoString)(unsafe.Pointer(&value))
   781  		v.Ptr = rt.IndexPtr(*(*unsafe.Pointer)(unsafe.Pointer(&p.Buf)), byteTypeSize, p.Read)
   782  		v.Len = int(size)
   783  	}
   784  
   785  	p.Read += int(size)
   786  	return
   787  }
   788  
   789  // ReadBinary ...
   790  func (p *BinaryProtocol) ReadBinary(copyBytes bool) (value []byte, err error) {
   791  	size, e := p.ReadI32()
   792  	if e != nil {
   793  		return nil, e
   794  	}
   795  	if size < 0 || int(size) > len(p.Buf)-p.Read {
   796  		return nil, errInvalidDataSize
   797  	}
   798  
   799  	if copyBytes {
   800  		value = make([]byte, int(size))
   801  		copy(value, *(*[]byte)(unsafe.Pointer(&rt.GoSlice{
   802  			Ptr: rt.IndexPtr(*(*unsafe.Pointer)(unsafe.Pointer(&p.Buf)), byteTypeSize, p.Read),
   803  			Len: int(size),
   804  			Cap: int(size),
   805  		})))
   806  	} else {
   807  		v := (*rt.GoString)(unsafe.Pointer(&value))
   808  		v.Ptr = rt.IndexPtr(*(*unsafe.Pointer)(unsafe.Pointer(&p.Buf)), byteTypeSize, p.Read)
   809  		v.Len = int(size)
   810  	}
   811  
   812  	p.Read += int(size)
   813  	return
   814  }
   815  
   816  // ReadStringWithDesc explains thrift data with desc and converts to simple string
   817  func (p *BinaryProtocol) ReadStringWithDesc(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool) error {
   818  	return p.EncodeText(desc, buf, byteAsUint8, disallowUnknown, base64Binary, true, false)
   819  }
   820  
   821  // EncodeText reads thrift data with descriptor, and converts it to a specail text-protocol string:
   822  // This protocol is similar to JSON, excepts its key (or field id) IS NOT QUOTED unless it is a string type:
   823  //   - LIST/SET's all elements will be joined with ',',
   824  //     and if asJson is true the entiry value will be wrapped by '[' (start) and ']' (end).
   825  //   - MAP's each pair of key and value will be binded with ':', all elements will be joined with ',',
   826  //     and if asJson is true the entiry value will be wrapped by '{' (start) and '}' (end).
   827  //   - STRUCT's each pair of field (name or id) and value will be binded with ':', all elements will be joined with ',',
   828  //     and if asJson is true the entiry value will be wrapped by '{' (start) and '}' (end).
   829  //   - STRING (including key) will be wrapped by '"' if asJson is true.
   830  func (p *BinaryProtocol) EncodeText(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool) error {
   831  	switch desc.Type() {
   832  	case BOOL:
   833  		b, err := p.ReadBool()
   834  		if err != nil {
   835  			return err
   836  		}
   837  		*buf = strconv.AppendBool(*buf, b)
   838  		return nil
   839  	case BYTE:
   840  		b, err := p.ReadByte()
   841  		if err != nil {
   842  			return err
   843  		}
   844  		if byteAsUint8 {
   845  			*buf = strconv.AppendInt(*buf, int64(uint8(b)), 10)
   846  			return nil
   847  		} else {
   848  			*buf = strconv.AppendInt(*buf, int64(b), 10)
   849  			return nil
   850  		}
   851  	case I16:
   852  		i, err := p.ReadI16()
   853  		if err != nil {
   854  			return err
   855  		}
   856  		*buf = json.EncodeInt64(*buf, int64(i))
   857  		return nil
   858  	case I32:
   859  		i, err := p.ReadI32()
   860  		if err != nil {
   861  			return err
   862  		}
   863  		*buf = json.EncodeInt64(*buf, int64(i))
   864  		return nil
   865  	case I64:
   866  		i, err := p.ReadI64()
   867  		if err != nil {
   868  			return err
   869  		}
   870  		*buf = json.EncodeInt64(*buf, i)
   871  		return nil
   872  	case DOUBLE:
   873  		f, err := p.ReadDouble()
   874  		if err != nil {
   875  			return err
   876  		}
   877  		*buf = json.EncodeFloat64(*buf, f)
   878  		return nil
   879  	case STRING:
   880  		if base64Binary && desc.IsBinary() {
   881  			vs, err := p.ReadBinary(false)
   882  			if err != nil {
   883  				return err
   884  			}
   885  			if !asJson {
   886  				*buf = json.EncodeBase64(*buf, vs)
   887  				return nil
   888  			}
   889  			*buf = json.EncodeBaniry(*buf, vs)
   890  		} else {
   891  			vs, err := p.ReadString(false)
   892  			if err != nil {
   893  				return err
   894  			}
   895  			if !asJson {
   896  				*buf = append(*buf, vs...)
   897  				return nil
   898  			}
   899  			*buf = json.EncodeString(*buf, vs)
   900  		}
   901  		return nil
   902  	case SET, LIST:
   903  		elemType, size, e := p.ReadSetBegin()
   904  		if e != nil {
   905  			return e
   906  		}
   907  		et := desc.Elem()
   908  		if et.Type() != elemType {
   909  			return errDismatchPrimitive
   910  		}
   911  		if asJson {
   912  			*buf = append(*buf, '[')
   913  		}
   914  		for i := 0; i < size; i++ {
   915  			if e := p.EncodeText(et, buf, byteAsUint8, disallowUnknown, base64Binary, useFieldName, asJson); e != nil {
   916  				return e
   917  			}
   918  			if i != size-1 {
   919  				*buf = append(*buf, ',')
   920  			}
   921  		}
   922  		if asJson {
   923  			*buf = append(*buf, ']')
   924  		}
   925  		return nil
   926  	case MAP:
   927  		keyType, valueType, size, e := p.ReadMapBegin()
   928  		if e != nil {
   929  			return e
   930  		}
   931  		et := desc.Elem()
   932  		if et.Type() != valueType || keyType != desc.Key().Type() {
   933  			return errDismatchPrimitive
   934  		}
   935  		if asJson {
   936  			*buf = append(*buf, '{')
   937  		}
   938  		for i := 0; i < size; i++ {
   939  			if e := p.EncodeText(desc.Key(), buf, byteAsUint8, disallowUnknown, base64Binary, useFieldName, asJson); e != nil {
   940  				return e
   941  			}
   942  			*buf = append(*buf, ':')
   943  			if e := p.EncodeText(desc.Elem(), buf, byteAsUint8, disallowUnknown, base64Binary, useFieldName, asJson); e != nil {
   944  				return e
   945  			}
   946  			if i != size-1 {
   947  				*buf = append(*buf, ',')
   948  			}
   949  		}
   950  		if asJson {
   951  			*buf = append(*buf, '}')
   952  		}
   953  		return nil
   954  	case STRUCT:
   955  		st := desc.Struct()
   956  		if asJson {
   957  			*buf = append(*buf, '{')
   958  		}
   959  		hasVal := false
   960  		for {
   961  			_, typ, id, err := p.ReadFieldBegin()
   962  			if err != nil {
   963  				return err
   964  			}
   965  			if typ == STOP {
   966  				break
   967  			}
   968  			if !hasVal {
   969  				hasVal = true
   970  			} else {
   971  				*buf = append(*buf, ',')
   972  			}
   973  			field := st.FieldById(id)
   974  			if field == nil {
   975  				if !disallowUnknown {
   976  					return errUnknonwField
   977  				}
   978  				continue
   979  			}
   980  			if !useFieldName {
   981  				*buf = json.EncodeInt64(*buf, int64(id))
   982  			} else {
   983  				*buf = append(*buf, field.Alias()...)
   984  			}
   985  			*buf = append(*buf, ':')
   986  			if err := p.EncodeText(field.Type(), buf, byteAsUint8, disallowUnknown, base64Binary, useFieldName, asJson); err != nil {
   987  				return err
   988  			}
   989  		}
   990  		if asJson {
   991  			*buf = append(*buf, '}')
   992  		}
   993  		return nil
   994  	default:
   995  		return errUnsupportedType
   996  	}
   997  }
   998  
   999  // ReadAnyWithDesc explains thrift data with descriptor and converts it to go interface{}
  1000  //   - LIST/SET will be converted to []interface{}
  1001  //   - MAP will be converted to map[string]interface{} or map[int]interface{}
  1002  //     or map[interface{}]interface (depends on its key type)
  1003  //   - STRUCT will be converted to map[FieldID]interface{}
  1004  func (p *BinaryProtocol) ReadAnyWithDesc(desc *TypeDescriptor, byteAsUint8 bool, copyString bool, disallowUnknonw bool, useFieldName bool) (interface{}, error) {
  1005  	switch desc.Type() {
  1006  	case STOP:
  1007  		return nil, nil
  1008  	case BOOL:
  1009  		return p.ReadBool()
  1010  	case BYTE:
  1011  		v, e := p.ReadByte()
  1012  		if e != nil {
  1013  			return nil, e
  1014  		}
  1015  		if !byteAsUint8 {
  1016  			return int8(v), nil
  1017  		}
  1018  		return v, nil
  1019  	case I16:
  1020  		return p.ReadI16()
  1021  	case I32:
  1022  		return p.ReadI32()
  1023  	case I64:
  1024  		return p.ReadI64()
  1025  	case DOUBLE:
  1026  		return p.ReadDouble()
  1027  	case STRING:
  1028  		if desc.IsBinary() {
  1029  			return p.ReadBinary(copyString)
  1030  		} else {
  1031  			return p.ReadString(copyString)
  1032  		}
  1033  	case SET, LIST:
  1034  		elemType, size, e := p.ReadSetBegin()
  1035  		if e != nil {
  1036  			return nil, e
  1037  		}
  1038  		et := desc.Elem()
  1039  		if et.Type() != elemType {
  1040  			return nil, errDismatchPrimitive
  1041  		}
  1042  		ret := make([]interface{}, 0, size)
  1043  		for i := 0; i < size; i++ {
  1044  			v, e := p.ReadAnyWithDesc(et, byteAsUint8, copyString, disallowUnknonw, useFieldName)
  1045  			if e != nil {
  1046  				return nil, e
  1047  			}
  1048  			ret = append(ret, v)
  1049  		}
  1050  		return ret, p.ReadSetEnd()
  1051  	case MAP:
  1052  		var ret interface{}
  1053  		keyType, valueType, size, e := p.ReadMapBegin()
  1054  		if e != nil {
  1055  			return nil, e
  1056  		}
  1057  		et := desc.Elem()
  1058  		if et.Type() != valueType || keyType != desc.Key().Type() {
  1059  			return nil, errDismatchPrimitive
  1060  		}
  1061  		if keyType == STRING {
  1062  			m := make(map[string]interface{}, size)
  1063  			for i := 0; i < size; i++ {
  1064  				kv, e := p.ReadString(false)
  1065  				if e != nil {
  1066  					return nil, e
  1067  				}
  1068  				vv, e := p.ReadAnyWithDesc(et, byteAsUint8, copyString, disallowUnknonw, useFieldName)
  1069  				if e != nil {
  1070  					return nil, e
  1071  				}
  1072  				m[kv] = vv
  1073  			}
  1074  			ret = m
  1075  		} else if keyType.IsInt() {
  1076  			m := make(map[int]interface{}, size)
  1077  			for i := 0; i < size; i++ {
  1078  				kv, e := p.ReadInt(keyType)
  1079  				if e != nil {
  1080  					return nil, e
  1081  				}
  1082  				vv, e := p.ReadAnyWithDesc(et, byteAsUint8, copyString, disallowUnknonw, useFieldName)
  1083  				if e != nil {
  1084  					return nil, e
  1085  				}
  1086  				m[kv] = vv
  1087  			}
  1088  			ret = m
  1089  		} else {
  1090  			m := make(map[interface{}]interface{})
  1091  			for i := 0; i < size; i++ {
  1092  				kv, e := p.ReadAnyWithDesc(desc.Key(), byteAsUint8, copyString, disallowUnknonw, useFieldName)
  1093  				if e != nil {
  1094  					return nil, e
  1095  				}
  1096  				vv, e := p.ReadAnyWithDesc(et, byteAsUint8, copyString, disallowUnknonw, useFieldName)
  1097  				if e != nil {
  1098  					return nil, e
  1099  				}
  1100  				switch x := kv.(type) {
  1101  				case map[string]interface{}:
  1102  					m[&x] = vv
  1103  				case map[int]interface{}:
  1104  					m[&x] = vv
  1105  				case map[interface{}]interface{}:
  1106  					m[&x] = vv
  1107  				case []interface{}:
  1108  					m[&x] = vv
  1109  				case map[FieldID]interface{}:
  1110  					m[&x] = vv
  1111  				default:
  1112  					m[kv] = vv
  1113  				}
  1114  			}
  1115  			ret = m
  1116  		}
  1117  		return ret, p.ReadMapEnd()
  1118  	case STRUCT:
  1119  		st := desc.Struct()
  1120  		var ret map[FieldID]interface{}
  1121  		var ret2 map[string]interface{}
  1122  		if useFieldName {
  1123  			ret2 = make(map[string]interface{}, len(st.Fields()))
  1124  		} else {
  1125  			ret = make(map[FieldID]interface{}, len(st.Fields()))
  1126  		}
  1127  		for {
  1128  			_, typ, id, err := p.ReadFieldBegin()
  1129  			if err != nil {
  1130  				return nil, err
  1131  			}
  1132  			if typ == STOP {
  1133  				if useFieldName {
  1134  					return ret2, nil
  1135  				} else {
  1136  					return ret, nil
  1137  				}
  1138  			}
  1139  			next := st.FieldById(id)
  1140  			if next == nil {
  1141  				if disallowUnknonw {
  1142  					return nil, errUnknonwField
  1143  				}
  1144  				if err := p.Skip(typ, false); err != nil {
  1145  					return nil, err
  1146  				}
  1147  				continue
  1148  			}
  1149  			vv, err := p.ReadAnyWithDesc(next.Type(), byteAsUint8, copyString, disallowUnknonw, useFieldName)
  1150  			if err != nil {
  1151  				return nil, err
  1152  			}
  1153  			if useFieldName {
  1154  				ret2[next.Alias()] = vv
  1155  			} else {
  1156  				ret[id] = vv
  1157  			}
  1158  		}
  1159  	default:
  1160  		return nil, errUnsupportedType
  1161  	}
  1162  }
  1163  
  1164  // WriteStringWithDesc explain simple string val with desc and convert to thrift data
  1165  func (p *BinaryProtocol) WriteStringWithDesc(val string, desc *TypeDescriptor, disallowUnknown bool, base64Binary bool) error {
  1166  	return p.DecodeText(val, desc, disallowUnknown, base64Binary, true, false)
  1167  }
  1168  
  1169  // DecodeText decode special text-encoded val with desc and write it into buffer
  1170  // The encoding of val should be compatible with `EncodeText()`
  1171  // WARNING: this function is not fully implemented, only support json-encoded string for LIST/MAP/SET/STRUCT
  1172  func (p *BinaryProtocol) DecodeText(val string, desc *TypeDescriptor, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool) error {
  1173  	switch desc.Type() {
  1174  	case STRING:
  1175  		if asJson {
  1176  			v, err := strconv.Unquote(val)
  1177  			if err != nil {
  1178  				return err
  1179  			}
  1180  			val = v
  1181  		}
  1182  		if base64Binary && desc.IsBinary() {
  1183  			v, err := base64.StdEncoding.DecodeString(val)
  1184  			if err != nil {
  1185  				return err
  1186  			}
  1187  			val = rt.Mem2Str(v)
  1188  		}
  1189  		return p.WriteString(val)
  1190  	case BOOL:
  1191  		v, err := strconv.ParseBool(val)
  1192  		if err != nil {
  1193  			return err
  1194  		}
  1195  		return p.WriteBool(v)
  1196  	case BYTE:
  1197  		i, err := strconv.ParseInt(val, 10, 64)
  1198  		if err != nil {
  1199  			return err
  1200  		}
  1201  		return p.WriteByte(byte(i))
  1202  	case I16:
  1203  		i, err := strconv.ParseInt(val, 10, 64)
  1204  		if err != nil {
  1205  			return err
  1206  		}
  1207  		return p.WriteI16(int16(i))
  1208  	case I32:
  1209  		i, err := strconv.ParseInt(val, 10, 64)
  1210  		if err != nil {
  1211  			return err
  1212  		}
  1213  		return p.WriteI32(int32(i))
  1214  	case I64:
  1215  		i, err := strconv.ParseInt(val, 10, 64)
  1216  		if err != nil {
  1217  			return err
  1218  		}
  1219  		return p.WriteI64(i)
  1220  	case DOUBLE:
  1221  		f, err := strconv.ParseFloat(val, 64)
  1222  		if err != nil {
  1223  			return err
  1224  		}
  1225  		return p.WriteDouble(f)
  1226  	case LIST, SET:
  1227  		if !asJson {
  1228  			// OPT: Optimize this using json ast in-place parser
  1229  			vs := strings.Split(val, ",")
  1230  			if err := p.WriteListBegin(desc.Elem().Type(), len(vs)); err != nil {
  1231  				return err
  1232  			}
  1233  			for _, v := range vs {
  1234  				err := p.DecodeText(v, desc.Elem(), disallowUnknown, base64Binary, useFieldName, asJson)
  1235  				if err != nil {
  1236  					return err
  1237  				}
  1238  			}
  1239  			return p.WriteListEnd()
  1240  		} else {
  1241  			// OPT: Optimize this using json ast in-place parser
  1242  			vs := []interface{}{}
  1243  			if err := util.SonicUseInt64.UnmarshalFromString(val, &vs); err != nil {
  1244  				return err
  1245  			}
  1246  			if err := p.WriteListBegin(desc.Elem().Type(), len(vs)); err != nil {
  1247  				return err
  1248  			}
  1249  			for _, v := range vs {
  1250  				if err := p.WriteAnyWithDesc(desc.Elem(), v, true, false, true); err != nil {
  1251  					return err
  1252  				}
  1253  			}
  1254  			return p.WriteListEnd()
  1255  		}
  1256  	case MAP:
  1257  		//TODO: implement it for non-json
  1258  		if !asJson {
  1259  			return errNotImplemented
  1260  		}
  1261  		// OPT: Optimize this using json ast in-place parser
  1262  		vs := map[string]interface{}{}
  1263  		if err := util.SonicUseInt64.UnmarshalFromString(val, &vs); err != nil {
  1264  			return err
  1265  		}
  1266  		if err := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), len(vs)); err != nil {
  1267  			return err
  1268  		}
  1269  		for k, v := range vs {
  1270  			err := p.DecodeText(k, desc.Key(), disallowUnknown, base64Binary, useFieldName, asJson)
  1271  			if err != nil {
  1272  				return err
  1273  			}
  1274  			if err := p.WriteAnyWithDesc(desc.Elem(), v, true, false, true); err != nil {
  1275  				return err
  1276  			}
  1277  		}
  1278  		return p.WriteMapEnd()
  1279  	case STRUCT:
  1280  		//TODO: implement it for non-json
  1281  		if !asJson {
  1282  			return errNotImplemented
  1283  		}
  1284  		// OPT: Optimize this using json ast in-place parser
  1285  		var v = make(map[string]interface{})
  1286  		err := util.SonicUseInt64.UnmarshalFromString(val, &v)
  1287  		if err != nil {
  1288  			return err
  1289  		}
  1290  		return p.WriteAnyWithDesc(desc, v, true, false, true)
  1291  	default:
  1292  		return errDismatchPrimitive
  1293  	}
  1294  }
  1295  
  1296  // GoType2ThriftType a go primitive type to a thrift type
  1297  // The rules is:
  1298  //   - bool -> BOOL
  1299  //   - byte/int8 -> BYTE
  1300  //   - int16 -> I16
  1301  //   - int32 -> I32
  1302  //   - int64/int -> I64
  1303  //   - int -> I64
  1304  //   - float64/float32 -> DOUBLE
  1305  //   - string/[]byte -> STRING
  1306  //   - []interface{} -> LIST
  1307  //   - map[FieldID]interface{} -> STRUCT
  1308  //   - map[(int|string|interface{})]interface{} -> MAP
  1309  func GoType2ThriftType(val interface{}) (Type, error) {
  1310  	_, ok := val.(map[FieldID]interface{})
  1311  	if ok {
  1312  		return STRUCT, nil
  1313  	}
  1314  	_, ok = val.([]byte)
  1315  	if ok {
  1316  		return STRING, nil
  1317  	}
  1318  	switch reflect.TypeOf(val).Kind() {
  1319  	case reflect.Bool:
  1320  		return BOOL, nil
  1321  	case reflect.Int8, reflect.Uint8:
  1322  		return BYTE, nil
  1323  	case reflect.Int16, reflect.Uint16:
  1324  		return I16, nil
  1325  	case reflect.Int32, reflect.Uint32:
  1326  		return I32, nil
  1327  	case reflect.Int64, reflect.Uint64, reflect.Int, reflect.Uint:
  1328  		return I64, nil
  1329  	case reflect.Float64:
  1330  		return DOUBLE, nil
  1331  	case reflect.String:
  1332  		return STRING, nil
  1333  	case reflect.Slice:
  1334  		return LIST, nil
  1335  	case reflect.Map:
  1336  		return MAP, nil
  1337  	case reflect.Struct:
  1338  		return STRUCT, nil
  1339  	case reflect.Ptr:
  1340  		return GoType2ThriftType(reflect.ValueOf(val).Elem().Interface())
  1341  	default:
  1342  		return STOP, errUnsupportedType
  1343  	}
  1344  }
  1345  
  1346  // ReadAny reads a thrift value from buffer and convert it to go primitive type
  1347  // It basicallly obeys rules in `GoType2ThriftType`.
  1348  // Specially,
  1349  //   - For INT(8/16/32/64) type, the return type is corresponding int8/int16/int32/int64 by default;
  1350  //   - For MAP type, the output key type could be string, int or interface{}, depends on the input key's thrift type.
  1351  //   - for STRUCT type, the return type is map[thrift.FieldID]interface{}.
  1352  func (p *BinaryProtocol) ReadAny(typ Type, strAsBinary bool, byteAsInt8 bool) (interface{}, error) {
  1353  	switch typ {
  1354  	case BOOL:
  1355  		return p.ReadBool()
  1356  	case BYTE:
  1357  		if byteAsInt8 {
  1358  			n, e := p.ReadByte()
  1359  			return int8(n), e
  1360  		}
  1361  		return p.ReadByte()
  1362  	case I16:
  1363  		return p.ReadI16()
  1364  	case I32:
  1365  		return p.ReadI32()
  1366  	case I64:
  1367  		return p.ReadI64()
  1368  	case DOUBLE:
  1369  		return p.ReadDouble()
  1370  	case STRING:
  1371  		if strAsBinary {
  1372  			return p.ReadBinary(false)
  1373  		}
  1374  		return p.ReadString(false)
  1375  	case LIST, SET:
  1376  		elemType, size, e := p.ReadListBegin()
  1377  		if e != nil {
  1378  			return nil, e
  1379  		}
  1380  		ret := make([]interface{}, 0, size)
  1381  		for i := 0; i < size; i++ {
  1382  			v, e := p.ReadAny(elemType, strAsBinary, byteAsInt8)
  1383  			if e != nil {
  1384  				return nil, e
  1385  			}
  1386  			ret = append(ret, v)
  1387  		}
  1388  		return ret, p.ReadListEnd()
  1389  	case MAP:
  1390  		keyType, valueType, size, e := p.ReadMapBegin()
  1391  		if e != nil {
  1392  			return nil, e
  1393  		}
  1394  		if keyType == STRING {
  1395  			ret := make(map[string]interface{}, size)
  1396  			for i := 0; i < size; i++ {
  1397  				k, e := p.ReadString(false)
  1398  				if e != nil {
  1399  					return nil, e
  1400  				}
  1401  				v, e := p.ReadAny(valueType, strAsBinary, byteAsInt8)
  1402  				if e != nil {
  1403  					return nil, e
  1404  				}
  1405  				ret[k] = v
  1406  			}
  1407  			return ret, p.ReadMapEnd()
  1408  		} else if keyType.IsInt() {
  1409  			ret := make(map[int]interface{}, size)
  1410  			for i := 0; i < size; i++ {
  1411  				k, e := p.ReadInt(keyType)
  1412  				if e != nil {
  1413  					return nil, e
  1414  				}
  1415  				v, e := p.ReadAny(valueType, strAsBinary, byteAsInt8)
  1416  				if e != nil {
  1417  					return nil, e
  1418  				}
  1419  				ret[k] = v
  1420  			}
  1421  			return ret, p.ReadMapEnd()
  1422  		} else {
  1423  			m := make(map[interface{}]interface{}, size)
  1424  			for i := 0; i < size; i++ {
  1425  				k, e := p.ReadAny(keyType, strAsBinary, byteAsInt8)
  1426  				if e != nil {
  1427  					return nil, e
  1428  				}
  1429  				v, e := p.ReadAny(valueType, strAsBinary, byteAsInt8)
  1430  				if e != nil {
  1431  					return nil, e
  1432  				}
  1433  				switch x := k.(type) {
  1434  				case map[string]interface{}:
  1435  					m[&x] = v
  1436  				case map[int]interface{}:
  1437  					m[&x] = v
  1438  				case map[interface{}]interface{}:
  1439  					m[&x] = v
  1440  				case []interface{}:
  1441  					m[&x] = v
  1442  				case map[FieldID]interface{}:
  1443  					m[&x] = v
  1444  				default:
  1445  					m[k] = v
  1446  				}
  1447  			}
  1448  			return m, p.ReadMapEnd()
  1449  		}
  1450  	case STRUCT:
  1451  		ret := make(map[FieldID]interface{})
  1452  		for {
  1453  			_, typ, id, err := p.ReadFieldBegin()
  1454  			if err != nil {
  1455  				return nil, err
  1456  			}
  1457  			if typ == STOP {
  1458  				return ret, nil
  1459  			}
  1460  			v, e := p.ReadAny(typ, strAsBinary, byteAsInt8)
  1461  			if e != nil {
  1462  				return nil, e
  1463  			}
  1464  			ret[id] = v
  1465  		}
  1466  	default:
  1467  		return nil, errUnsupportedType
  1468  	}
  1469  }
  1470  
  1471  // WriteAny write any go primitive type to thrift data, and return top level thrift type
  1472  // It basically obeys rules in `GoType2ThriftType`.
  1473  // Specially,
  1474  //   - for MAP type, the key type should be string or int8/int16/int32/int64/int or interface{}.
  1475  //   - for STRUCT type, the val type should be map[thrift.FieldID]interface{}.
  1476  func (p *BinaryProtocol) WriteAny(val interface{}, sliceAsSet bool) (Type, error) {
  1477  	switch v := val.(type) {
  1478  	case bool:
  1479  		return BOOL, p.WriteBool(v)
  1480  	case byte:
  1481  		return BYTE, p.WriteByte(v)
  1482  	case int8:
  1483  		return BYTE, p.WriteByte(byte(v))
  1484  	case int16:
  1485  		return I16, p.WriteI16(v)
  1486  	case int32:
  1487  		return I32, p.WriteI32(v)
  1488  	case int64:
  1489  		return I64, p.WriteI64(v)
  1490  	case int:
  1491  		return I64, p.WriteI64(int64(v))
  1492  	case float64:
  1493  		return DOUBLE, p.WriteDouble(v)
  1494  	case float32:
  1495  		return DOUBLE, p.WriteDouble(float64(v))
  1496  	case string:
  1497  		return STRING, p.WriteString(v)
  1498  	case []byte:
  1499  		return STRING, p.WriteBinary(v)
  1500  	case []interface{}:
  1501  		if len(v) == 0 {
  1502  			return 0, fmt.Errorf("empty []interface is not supported")
  1503  		}
  1504  		et, e := GoType2ThriftType(v[0])
  1505  		if e != nil {
  1506  			return 0, e
  1507  		}
  1508  		if sliceAsSet {
  1509  			e = p.WriteSetBegin(et, len(v))
  1510  			if e != nil {
  1511  				return 0, e
  1512  			}
  1513  		} else {
  1514  			e = p.WriteListBegin(et, len(v))
  1515  			if e != nil {
  1516  				return 0, e
  1517  			}
  1518  		}
  1519  		for _, vv := range v {
  1520  			if _, e := p.WriteAny(vv, sliceAsSet); e != nil {
  1521  				return 0, e
  1522  			}
  1523  		}
  1524  		return LIST, p.WriteListEnd()
  1525  	case map[string]interface{}:
  1526  		if len(v) == 0 {
  1527  			return 0, fmt.Errorf("empty map[string]interface is not supported")
  1528  		}
  1529  		var firstVal interface{}
  1530  		for _, vv := range v {
  1531  			firstVal = vv
  1532  			break
  1533  		}
  1534  		et, e := GoType2ThriftType(firstVal)
  1535  		if e != nil {
  1536  			return 0, e
  1537  		}
  1538  		e = p.WriteMapBegin(STRING, et, len(v))
  1539  		if e != nil {
  1540  			return 0, e
  1541  		}
  1542  		for k, vv := range v {
  1543  			if e := p.WriteString(k); e != nil {
  1544  				return 0, e
  1545  			}
  1546  			if _, e := p.WriteAny(vv, sliceAsSet); e != nil {
  1547  				return 0, e
  1548  			}
  1549  		}
  1550  		return MAP, p.WriteMapEnd()
  1551  	case map[byte]interface{}, map[int]interface{}, map[int8]interface{}, map[int16]interface{}, map[int32]interface{}, map[int64]interface{}:
  1552  		vr := reflect.ValueOf(v)
  1553  		if vr.Len() == 0 {
  1554  			return 0, fmt.Errorf("empty map[int]interface{} is not supported")
  1555  		}
  1556  		it := vr.MapRange()
  1557  		it.Next()
  1558  		firstKey := it.Key().Interface()
  1559  		firstVal := it.Value().Interface()
  1560  		kt, e := GoType2ThriftType(firstKey)
  1561  		if e != nil {
  1562  			return 0, e
  1563  		}
  1564  		et, e := GoType2ThriftType(firstVal)
  1565  		if e != nil {
  1566  			return 0, e
  1567  		}
  1568  		e = p.WriteMapBegin(kt, et, vr.Len())
  1569  		if e != nil {
  1570  			return 0, e
  1571  		}
  1572  		if _, e := p.WriteAny(firstKey, sliceAsSet); e != nil {
  1573  			return 0, e
  1574  		}
  1575  		if _, e := p.WriteAny(firstVal, sliceAsSet); e != nil {
  1576  			return 0, e
  1577  		}
  1578  		for it.Next() {
  1579  			if _, e := p.WriteAny(it.Key().Interface(), sliceAsSet); e != nil {
  1580  				return 0, e
  1581  			}
  1582  			if _, e := p.WriteAny(it.Value().Interface(), sliceAsSet); e != nil {
  1583  				return 0, e
  1584  			}
  1585  		}
  1586  		return MAP, p.WriteMapEnd()
  1587  	case map[interface{}]interface{}:
  1588  		if len(v) == 0 {
  1589  			return 0, fmt.Errorf("empty map[int]interface{} is not supported")
  1590  		}
  1591  		var firstVal, firstKey interface{}
  1592  		for kk, vv := range v {
  1593  			firstVal = vv
  1594  			firstKey = kk
  1595  			break
  1596  		}
  1597  		kt, e := GoType2ThriftType(firstKey)
  1598  		if e != nil {
  1599  			return 0, e
  1600  		}
  1601  		et, e := GoType2ThriftType(firstVal)
  1602  		if e != nil {
  1603  			return 0, e
  1604  		}
  1605  		e = p.WriteMapBegin(kt, et, len(v))
  1606  		if e != nil {
  1607  			return 0, e
  1608  		}
  1609  		for k, vv := range v {
  1610  			switch kt := k.(type) {
  1611  			case *map[string]interface{}:
  1612  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1613  					return 0, err
  1614  				}
  1615  			case *map[int]interface{}:
  1616  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1617  					return 0, err
  1618  				}
  1619  			case *map[int8]interface{}:
  1620  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1621  					return 0, err
  1622  				}
  1623  			case *map[int16]interface{}:
  1624  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1625  					return 0, err
  1626  				}
  1627  			case *map[int32]interface{}:
  1628  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1629  					return 0, err
  1630  				}
  1631  			case *map[int64]interface{}:
  1632  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1633  					return 0, err
  1634  				}
  1635  			case *map[FieldID]interface{}:
  1636  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1637  					return 0, err
  1638  				}
  1639  			case *map[interface{}]interface{}:
  1640  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1641  					return 0, err
  1642  				}
  1643  			case *[]interface{}:
  1644  				if _, err := p.WriteAny(*kt, sliceAsSet); err != nil {
  1645  					return 0, err
  1646  				}
  1647  			default:
  1648  				if _, err := p.WriteAny(k, sliceAsSet); err != nil {
  1649  					return 0, err
  1650  				}
  1651  			}
  1652  			if _, e := p.WriteAny(vv, sliceAsSet); e != nil {
  1653  				return 0, e
  1654  			}
  1655  		}
  1656  		return MAP, p.WriteMapEnd()
  1657  	case map[FieldID]interface{}:
  1658  		e := p.WriteStructBegin("")
  1659  		if e != nil {
  1660  			return 0, e
  1661  		}
  1662  		for k, vv := range v {
  1663  			ft, e := GoType2ThriftType(vv)
  1664  			if e != nil {
  1665  				return 0, e
  1666  			}
  1667  			if e := p.WriteFieldBegin("", ft, k); e != nil {
  1668  				return 0, e
  1669  			}
  1670  			if _, e := p.WriteAny(vv, sliceAsSet); e != nil {
  1671  				return 0, e
  1672  			}
  1673  		}
  1674  		return STRUCT, p.WriteFieldStop()
  1675  	default:
  1676  		return 0, errUnsupportedType
  1677  	}
  1678  }
  1679  
  1680  // WriteAnyWithDesc explain desc and val and write them into buffer
  1681  //   - LIST/SET will be converted from []interface{}
  1682  //   - MAP will be converted from map[string]interface{} or map[int]interface{}
  1683  //   - STRUCT will be converted from map[FieldID]interface{}
  1684  func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{}, cast bool, disallowUnknown bool, useFieldName bool) error {
  1685  	switch desc.Type() {
  1686  	case STOP:
  1687  		return nil
  1688  	case BOOL:
  1689  		v, ok := val.(bool)
  1690  		if !ok {
  1691  			if !cast {
  1692  				return errDismatchPrimitive
  1693  			} else {
  1694  				var err error
  1695  				v, err = primitive.ToBool(val)
  1696  				if err != nil {
  1697  					return err
  1698  				}
  1699  			}
  1700  		}
  1701  		return p.WriteBool(v)
  1702  	case BYTE:
  1703  		v, ok := val.(byte)
  1704  		if !ok {
  1705  			if !cast {
  1706  				return errDismatchPrimitive
  1707  			} else {
  1708  				vv, err := primitive.ToInt64(val)
  1709  				if err != nil {
  1710  					return err
  1711  				}
  1712  				v = byte(vv)
  1713  			}
  1714  		}
  1715  		return p.WriteByte(v)
  1716  	case I16:
  1717  		v, ok := val.(int16)
  1718  		if !ok {
  1719  			if !cast {
  1720  				return errDismatchPrimitive
  1721  			} else {
  1722  				vv, err := primitive.ToInt64(val)
  1723  				if err != nil {
  1724  					return err
  1725  				}
  1726  				v = int16(vv)
  1727  			}
  1728  		}
  1729  		return p.WriteI16(v)
  1730  	case I32:
  1731  		v, ok := val.(int32)
  1732  		if !ok {
  1733  			if !cast {
  1734  				return errDismatchPrimitive
  1735  			} else {
  1736  				vv, err := primitive.ToInt64(val)
  1737  				if err != nil {
  1738  					return err
  1739  				}
  1740  				v = int32(vv)
  1741  			}
  1742  		}
  1743  		return p.WriteI32(v)
  1744  	case I64:
  1745  		v, ok := val.(int64)
  1746  		if !ok {
  1747  			if !cast {
  1748  				return errDismatchPrimitive
  1749  			} else {
  1750  				vv, err := primitive.ToInt64(val)
  1751  				if err != nil {
  1752  					return err
  1753  				}
  1754  				v = int64(vv)
  1755  			}
  1756  		}
  1757  		return p.WriteI64(v)
  1758  	case DOUBLE:
  1759  		v, ok := val.(float64)
  1760  		if !ok {
  1761  			if !cast {
  1762  				return errDismatchPrimitive
  1763  			} else {
  1764  				vv, err := primitive.ToFloat64(val)
  1765  				if err != nil {
  1766  					return err
  1767  				}
  1768  				v = float64(vv)
  1769  			}
  1770  		}
  1771  		return p.WriteDouble(v)
  1772  	case STRING:
  1773  		v, ok := val.(string)
  1774  		if !ok {
  1775  			vv, ok := val.([]byte)
  1776  			if !ok {
  1777  				if !cast {
  1778  					return errDismatchPrimitive
  1779  				} else {
  1780  					vv, err := primitive.ToString(val)
  1781  					if err != nil {
  1782  						return err
  1783  					}
  1784  					v = string(vv)
  1785  				}
  1786  			}
  1787  			return p.WriteBinary(vv)
  1788  		}
  1789  		return p.WriteString(v)
  1790  	case SET, LIST:
  1791  		vs, ok := val.([]interface{})
  1792  		if !ok {
  1793  			return errDismatchPrimitive
  1794  		}
  1795  		e := p.WriteSetBegin(desc.Elem().Type(), len(vs))
  1796  		if e != nil {
  1797  			return e
  1798  		}
  1799  		for _, v := range vs {
  1800  			if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1801  				return e
  1802  			}
  1803  		}
  1804  		return p.WriteSetEnd()
  1805  	case MAP:
  1806  		if kt := desc.Key().Type(); kt == STRING {
  1807  			vs, ok := val.(map[string]interface{})
  1808  			if !ok {
  1809  				return errDismatchPrimitive
  1810  			}
  1811  			e := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), len(vs))
  1812  			if e != nil {
  1813  				return e
  1814  			}
  1815  			for k, v := range vs {
  1816  				if e := p.WriteString(k); e != nil {
  1817  					return e
  1818  				}
  1819  				if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1820  					return e
  1821  				}
  1822  			}
  1823  		} else if kt.IsInt() {
  1824  			vi, ok := val.(map[int]interface{})
  1825  			if ok {
  1826  				e := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), len(vi))
  1827  				if e != nil {
  1828  					return e
  1829  				}
  1830  				for k, v := range vi {
  1831  					if e := p.WriteInt(kt, k); e != nil {
  1832  						return e
  1833  					}
  1834  					if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1835  						return e
  1836  					}
  1837  				}
  1838  				return nil
  1839  			}
  1840  			v2, ok := val.(map[int8]interface{})
  1841  			if ok {
  1842  				e := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), len(v2))
  1843  				if e != nil {
  1844  					return e
  1845  				}
  1846  				for k, v := range v2 {
  1847  					if e := p.WriteInt(kt, int(k)); e != nil {
  1848  						return e
  1849  					}
  1850  					if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1851  						return e
  1852  					}
  1853  				}
  1854  				return nil
  1855  			}
  1856  			v3, ok := val.(map[int16]interface{})
  1857  			if ok {
  1858  				e := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), len(v3))
  1859  				if e != nil {
  1860  					return e
  1861  				}
  1862  				for k, v := range v3 {
  1863  					if e := p.WriteInt(kt, int(k)); e != nil {
  1864  						return e
  1865  					}
  1866  					if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1867  						return e
  1868  					}
  1869  				}
  1870  				return nil
  1871  			}
  1872  			v4, ok := val.(map[int32]interface{})
  1873  			if ok {
  1874  				e := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), len(v4))
  1875  				if e != nil {
  1876  					return e
  1877  				}
  1878  				for k, v := range v4 {
  1879  					if e := p.WriteInt(kt, int(k)); e != nil {
  1880  						return e
  1881  					}
  1882  					if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1883  						return e
  1884  					}
  1885  				}
  1886  				return nil
  1887  			}
  1888  			v5, ok := val.(map[int64]interface{})
  1889  			if ok {
  1890  				e := p.WriteMapBegin(desc.Key().Type(), desc.Elem().Type(), len(v5))
  1891  				if e != nil {
  1892  					return e
  1893  				}
  1894  				for k, v := range v5 {
  1895  					if e := p.WriteInt(kt, int(k)); e != nil {
  1896  						return e
  1897  					}
  1898  					if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1899  						return e
  1900  					}
  1901  				}
  1902  				return nil
  1903  			}
  1904  			return errDismatchPrimitive
  1905  		} else {
  1906  			vv, ok := val.(map[interface{}]interface{})
  1907  			if !ok {
  1908  				return errDismatchPrimitive
  1909  			}
  1910  			for k, v := range vv {
  1911  				switch kt := k.(type) {
  1912  				case *map[string]interface{}:
  1913  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1914  						return err
  1915  					}
  1916  				case *map[int]interface{}:
  1917  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1918  						return err
  1919  					}
  1920  				case *map[int8]interface{}:
  1921  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1922  						return err
  1923  					}
  1924  				case *map[int16]interface{}:
  1925  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1926  						return err
  1927  					}
  1928  				case *map[int32]interface{}:
  1929  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1930  						return err
  1931  					}
  1932  				case *map[int64]interface{}:
  1933  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1934  						return err
  1935  					}
  1936  				case *map[FieldID]interface{}:
  1937  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1938  						return err
  1939  					}
  1940  				case *map[interface{}]interface{}:
  1941  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1942  						return err
  1943  					}
  1944  				case *[]interface{}:
  1945  					if err := p.WriteAnyWithDesc(desc.Key(), *kt, cast, disallowUnknown, useFieldName); err != nil {
  1946  						return err
  1947  					}
  1948  				default:
  1949  					if err := p.WriteAnyWithDesc(desc.Key(), k, cast, disallowUnknown, useFieldName); err != nil {
  1950  						return err
  1951  					}
  1952  				}
  1953  				if e := p.WriteAnyWithDesc(desc.Elem(), v, cast, disallowUnknown, useFieldName); e != nil {
  1954  					return e
  1955  				}
  1956  			}
  1957  		}
  1958  		return nil
  1959  	case STRUCT:
  1960  		if useFieldName {
  1961  			vs, ok := val.(map[string]interface{})
  1962  			if !ok {
  1963  				return errDismatchPrimitive
  1964  			}
  1965  			e := p.WriteStructBegin(desc.Name())
  1966  			if e != nil {
  1967  				return e
  1968  			}
  1969  			for id, v := range vs {
  1970  				f := desc.Struct().FieldByKey(id)
  1971  				if f == nil {
  1972  					if disallowUnknown {
  1973  						return errUnknonwField
  1974  					}
  1975  					continue
  1976  				}
  1977  				if e := p.WriteFieldBegin(f.Alias(), f.Type().Type(), f.ID()); e != nil {
  1978  					return e
  1979  				}
  1980  				if e := p.WriteAnyWithDesc(f.Type(), v, cast, disallowUnknown, useFieldName); e != nil {
  1981  					return e
  1982  				}
  1983  				if e := p.WriteFieldEnd(); e != nil {
  1984  					return e
  1985  				}
  1986  			}
  1987  		} else {
  1988  			vs, ok := val.(map[FieldID]interface{})
  1989  			if !ok {
  1990  				return errDismatchPrimitive
  1991  			}
  1992  			e := p.WriteStructBegin(desc.Name())
  1993  			if e != nil {
  1994  				return e
  1995  			}
  1996  			// var r = NewRequiresBitmap()
  1997  			// desc.Struct().Requires().CopyTo(r)
  1998  			for id, v := range vs {
  1999  				f := desc.Struct().FieldById(id)
  2000  				if f == nil {
  2001  					if disallowUnknown {
  2002  						return errUnknonwField
  2003  					}
  2004  					continue
  2005  				}
  2006  				// r.Set(f.ID(), OptionalRequireness)
  2007  				if e := p.WriteFieldBegin(f.Alias(), f.Type().Type(), f.ID()); e != nil {
  2008  					return e
  2009  				}
  2010  				if e := p.WriteAnyWithDesc(f.Type(), v, cast, disallowUnknown, useFieldName); e != nil {
  2011  					return e
  2012  				}
  2013  				if e := p.WriteFieldEnd(); e != nil {
  2014  					return e
  2015  				}
  2016  			}
  2017  			// if e = r.CheckRequires(desc.Struct(), false, nil); e != nil {
  2018  			// 	return e
  2019  			// }
  2020  			// FreeRequiresBitmap(r)
  2021  		}
  2022  		return p.WriteStructEnd()
  2023  	default:
  2024  		return errUnsupportedType
  2025  	}
  2026  }
  2027  
  2028  var typeSize = [256]int{
  2029  	STOP:   -1,
  2030  	VOID:   -1,
  2031  	BOOL:   1,
  2032  	I08:    1,
  2033  	I16:    2,
  2034  	I32:    4,
  2035  	I64:    8,
  2036  	DOUBLE: 8,
  2037  	STRING: -1,
  2038  	STRUCT: -1,
  2039  	MAP:    -1,
  2040  	SET:    -1,
  2041  	LIST:   -1,
  2042  	UTF8:   -1,
  2043  	UTF16:  -1,
  2044  }
  2045  
  2046  // TypeSize returns the size of the given type.
  2047  // -1 means variable size (LIST, SET, MAP, STRING)
  2048  // 0 means unknown type
  2049  func TypeSize(t Type) int {
  2050  	return typeSize[t]
  2051  }
  2052  
  2053  // SkipGo skips over the value for the given type using Go implementation.
  2054  func (p *BinaryProtocol) SkipGo(fieldType Type, maxDepth int) (err error) {
  2055  	if maxDepth <= 0 {
  2056  		return errExceedDepthLimit
  2057  	}
  2058  	switch fieldType {
  2059  	case BOOL:
  2060  		_, err = p.ReadBool()
  2061  		return
  2062  	case BYTE:
  2063  		_, err = p.ReadByte()
  2064  		return
  2065  	case I16:
  2066  		_, err = p.ReadI16()
  2067  		return
  2068  	case I32:
  2069  		_, err = p.ReadI32()
  2070  		return
  2071  	case I64:
  2072  		_, err = p.ReadI64()
  2073  		return
  2074  	case DOUBLE:
  2075  		_, err = p.ReadDouble()
  2076  		return
  2077  	case STRING:
  2078  		_, err = p.ReadString(false)
  2079  		return
  2080  	case STRUCT:
  2081  		// if _, err = p.ReadStructBegin(); err != nil {
  2082  		// 	return err
  2083  		// }
  2084  		for {
  2085  			_, typeId, _, _ := p.ReadFieldBegin()
  2086  			if typeId == STOP {
  2087  				break
  2088  			}
  2089  			//fastpath
  2090  			if n := typeSize[typeId]; n > 0 {
  2091  				p.Read += n
  2092  				if p.Read > len(p.Buf) {
  2093  					return io.EOF
  2094  				}
  2095  				continue
  2096  			}
  2097  			err := p.SkipGo(typeId, maxDepth-1)
  2098  			if err != nil {
  2099  				return err
  2100  			}
  2101  			p.ReadFieldEnd()
  2102  		}
  2103  		return p.ReadStructEnd()
  2104  	case MAP:
  2105  		keyType, valueType, size, err := p.ReadMapBegin()
  2106  		if err != nil {
  2107  			return err
  2108  		}
  2109  		//fastpath
  2110  		if k, v := typeSize[keyType], typeSize[valueType]; k > 0 && v > 0 {
  2111  			p.Read += (k + v) * size
  2112  			if p.Read > len(p.Buf) {
  2113  				return io.EOF
  2114  			}
  2115  		} else {
  2116  			if size > len(p.Buf)-p.Read {
  2117  				return errInvalidDataSize
  2118  			}
  2119  			for i := 0; i < size; i++ {
  2120  				err := p.SkipGo(keyType, maxDepth-1)
  2121  				if err != nil {
  2122  					return err
  2123  				}
  2124  				err = p.SkipGo(valueType, maxDepth-1)
  2125  				if err != nil {
  2126  					return err
  2127  				}
  2128  			}
  2129  		}
  2130  		return p.ReadMapEnd()
  2131  	case SET, LIST:
  2132  		elemType, size, err := p.ReadListBegin()
  2133  		if err != nil {
  2134  			return err
  2135  		}
  2136  		//fastpath
  2137  		if v := typeSize[elemType]; v > 0 {
  2138  			p.Read += v * size
  2139  			if p.Read > len(p.Buf) {
  2140  				return io.EOF
  2141  			}
  2142  		} else {
  2143  			if size > len(p.Buf)-p.Read {
  2144  				return errInvalidDataSize
  2145  			}
  2146  			for i := 0; i < size; i++ {
  2147  				err := p.SkipGo(elemType, maxDepth-1)
  2148  				if err != nil {
  2149  					return err
  2150  				}
  2151  			}
  2152  		}
  2153  		return p.ReadListEnd()
  2154  	default:
  2155  		return
  2156  	}
  2157  }
  2158  
  2159  // next ...
  2160  func (p *BinaryProtocol) next(size int) ([]byte, error) {
  2161  	if size <= 0 {
  2162  		panic(errors.New("invalid size"))
  2163  	}
  2164  
  2165  	l := len(p.Buf)
  2166  	d := p.Read + size
  2167  	if d > l {
  2168  		return nil, io.EOF
  2169  	}
  2170  
  2171  	ret := (p.Buf)[p.Read:d]
  2172  	p.Read = d
  2173  	return ret, nil
  2174  }
  2175  
  2176  // BinaryEncoding is the implementation of Encoding for binary encoding.
  2177  type BinaryEncoding struct{}
  2178  
  2179  // EncodeBool encodes a bool value.
  2180  func (BinaryEncoding) EncodeBool(b []byte, v bool) {
  2181  	if v {
  2182  		b[0] = 1
  2183  	} else {
  2184  		b[0] = 0
  2185  	}
  2186  }
  2187  
  2188  // EncodeByte encodes a byte value.
  2189  func (BinaryEncoding) EncodeByte(b []byte, v byte) {
  2190  	b[0] = byte(v)
  2191  }
  2192  
  2193  // EncodeInt16 encodes a int16 value.
  2194  func (BinaryEncoding) EncodeInt16(b []byte, v int16) {
  2195  	binary.BigEndian.PutUint16(b, uint16(v))
  2196  }
  2197  
  2198  // EncodeInt32 encodes a int32 value.
  2199  func (BinaryEncoding) EncodeInt32(b []byte, v int32) {
  2200  	binary.BigEndian.PutUint32(b, uint32(v))
  2201  }
  2202  
  2203  // EncodeInt64 encodes a int64 value.
  2204  func (BinaryEncoding) EncodeInt64(b []byte, v int64) {
  2205  	binary.BigEndian.PutUint64(b, uint64(v))
  2206  }
  2207  
  2208  func (BinaryEncoding) EncodeDouble(b []byte, v float64) {
  2209  	binary.BigEndian.PutUint64(b, math.Float64bits(v))
  2210  }
  2211  
  2212  // EncodeString encodes a string value.
  2213  func (BinaryEncoding) EncodeString(b []byte, v string) {
  2214  	binary.BigEndian.PutUint32(b, uint32(len(v)))
  2215  	copy(b[4:], v)
  2216  }
  2217  
  2218  // EncodeBinary encodes a binary value.
  2219  func (BinaryEncoding) EncodeBinary(b []byte, v []byte) {
  2220  	binary.BigEndian.PutUint32(b, uint32(len(v)))
  2221  	copy(b[4:], v)
  2222  }
  2223  
  2224  // EncodeFieldBegin encodes a field begin.
  2225  func (BinaryEncoding) EncodeFieldBegin(b []byte, t Type, id FieldID) {
  2226  	b[0] = byte(t)
  2227  	binary.BigEndian.PutUint16(b[1:], uint16(id))
  2228  }
  2229  
  2230  // EncodeFieldEnd encodes a field end.
  2231  func (BinaryEncoding) DecodeBool(b []byte) bool {
  2232  	return int8(b[0]) == 1
  2233  }
  2234  
  2235  // DecodeByte decodes a byte value.
  2236  func (BinaryEncoding) DecodeByte(b []byte) byte {
  2237  	return byte(b[0])
  2238  }
  2239  
  2240  // DecodeInt16 decodes a int16 value.
  2241  func (BinaryEncoding) DecodeInt16(b []byte) int16 {
  2242  	return int16(binary.BigEndian.Uint16(b))
  2243  }
  2244  
  2245  // DecodeInt32 decodes a int32 value.
  2246  func (BinaryEncoding) DecodeInt32(b []byte) int32 {
  2247  	return int32(binary.BigEndian.Uint32(b))
  2248  }
  2249  
  2250  // DecodeInt64 decodes a int64 value.
  2251  func (BinaryEncoding) DecodeInt64(b []byte) int64 {
  2252  	return int64(binary.BigEndian.Uint64(b))
  2253  }
  2254  
  2255  // DecodeDouble decodes a double value.
  2256  func (BinaryEncoding) DecodeDouble(b []byte) float64 {
  2257  	return math.Float64frombits(binary.BigEndian.Uint64(b))
  2258  }
  2259  
  2260  // DecodeString decodes a string value.
  2261  func (d BinaryEncoding) DecodeString(b []byte) (value string) {
  2262  	size := d.DecodeInt32(b)
  2263  	v := (*rt.GoString)(unsafe.Pointer(&value))
  2264  	v.Ptr = rt.IndexPtr(*(*unsafe.Pointer)(unsafe.Pointer(&b)), byteTypeSize, 4)
  2265  	v.Len = int(size)
  2266  	return
  2267  }
  2268  
  2269  // DecodeBinary decodes a binary value.
  2270  func (d BinaryEncoding) DecodeBytes(b []byte) (value []byte) {
  2271  	size := d.DecodeInt32(b)
  2272  	v := (*rt.GoSlice)(unsafe.Pointer(&value))
  2273  	v.Ptr = rt.IndexPtr(*(*unsafe.Pointer)(unsafe.Pointer(&b)), byteTypeSize, 4)
  2274  	v.Len = int(size)
  2275  	v.Cap = int(size)
  2276  	return
  2277  }