Ejemplo n.º 1
0
func (r *Response) DecodeMsg(mr *msgp.Reader) error {
	var bm msgp.Decodable

	if r.Body != nil {
		var ok bool
		bm, ok = r.Body.(msgp.Decodable)
		if !ok {
			return ErrNotDecodable
		}
	}

	if size, err := mr.ReadArrayHeader(); err != nil {
		return err
	} else if size != ResponsePackSize {
		return ErrBadPackSize
	}

	if typ, err := mr.ReadInt(); err != nil {
		return err
	} else if typ != ResponseType {
		return ErrBadMessageType
	}

	id, err := mr.ReadUint32()
	if err != nil {
		return err
	}

	r.ID = id

	// Error can be nil or a string.
	typ, err := mr.NextType()
	if err != nil {
		return err
	}

	switch typ {
	case msgp.StrType:
		s, err := mr.ReadString()
		if err != nil {
			return err
		}
		r.Error = s

	case msgp.NilType:
		if err := mr.ReadNil(); err != nil {
			return err
		}
		r.Error = ""

	default:
		return ErrBadErrorType
	}

	// Body can be nil.
	typ, err = mr.NextType()
	if err != nil {
		return err
	}

	if typ == msgp.NilType {
		r.hasBody = false
		return mr.ReadNil()
	}

	r.hasBody = true

	// Skip decoding the body if no value is present to decode into.
	// The caller is expected to read the body or skip it.
	if bm != nil {
		return decodeBody(mr, bm)
	}

	return nil

}