// encodeVal encodes a struct field.
421048
func encodeVal(buf *bytes.Buffer, path, name string, src interface{}) error {
	if src == nil {
		return encodeNull(buf, name)
	}
	rvsrc := reflect.ValueOf(src)
	if rvsrc.Kind() == reflect.Ptr && rvsrc.IsNil() {
		return encodeNull(buf, name)
	}
	src = indirect(rvsrc).Interface()

	// Try non-reflect first.
	switch srct := src.(type) {
	case Float:
		return encodeFloat(buf, name, srct)
	case String:
		return encodeString(buf, name, srct)
	case Map:
		return encodeEmbeddedDocument(buf, path, name, srct)
	case Slice:
		return encodeEmbeddedDocument(buf, path, name, srct)
	case BSON:
		_, err := buf.Write(srct)
		return err
	case Array:
		return encodeArray(buf, path, name, srct)
	case Binary:
		return encodeBinary(buf, name, srct)
	case Undefined:
		return encodeUndefined(buf, name)
	case ObjectId:
		return encodeObjectId(buf, path, name, srct)
	case Bool:
		return encodeBool(buf, name, srct)
	case UTCDateTime:
		return encodeUTCDateTime(buf, name, srct)
	case Null:
		return encodeNull(buf, name)
	case Regexp:
		return encodeRegexp(buf, name, srct)
	case DBPointer:
		return encodeDBPointer(buf, path, name, srct)
	case Javascript:
		return encodeJavascript(buf, name, srct)
	case Symbol:
		return encodeSymbol(buf, name, srct)
	case JavascriptScope:
		return encodeJavascriptScope(buf, path, name, srct)
	case Int32:
		return encodeInt32(buf, name, srct)
	case Timestamp:
		return encodeTimestamp(buf, name, srct)
	case Int64:
		return encodeInt64(buf, name, srct)
	case MinKey:
		return encodeMinKey(buf, name)
	case MaxKey:
		return encodeMaxKey(buf, name)
	case bool:
		return encodeBool(buf, name, Bool(srct))
	case int8:
		return encodeInt32(buf, name, Int32(srct))
	case int16:
		return encodeInt32(buf, name, Int32(srct))
	case int32:
		return encodeInt32(buf, name, Int32(srct))
	case int:
		return encodeInt64(buf, name, Int64(srct))
	case int64:
		return encodeInt64(buf, name, Int64(srct))
	case float64:
		return encodeFloat(buf, name, Float(srct))
	case string:
		return encodeString(buf, name, String(srct))
	case time.Time:
		return encodeUTCDateTime(buf, name,
			UTCDateTime(srct.UnixNano()/1000/1000))
	case []byte:
		return encodeBinary(buf, name, srct)
	default:
		// Fall back to reflect.
		switch rvsrc.Kind() {
		case reflect.Bool:
			return encodeBool(buf, name, Bool(rvsrc.Bool()))
		case reflect.Int8, reflect.Int16, reflect.Int32:
			return encodeInt32(buf, name, Int32(rvsrc.Int()))
		case reflect.Int, reflect.Int64:
			return encodeInt64(buf, name, Int64(rvsrc.Int()))
		case reflect.Float64:
			return encodeFloat(buf, name, Float(rvsrc.Float()))
		case reflect.Slice:
			a := make(Array, rvsrc.Len())
			for i := 0; i < rvsrc.Len(); i++ {
				a[i] = rvsrc.Index(i).Interface()
			}
			return encodeArray(buf, path, name, a)
		case reflect.String:
			return encodeString(buf, name, String(rvsrc.String()))
		}
	}
	return fmt.Errorf("%v, cannot encode %T.\n", path, src)
}
556818
func (w *ByteWriter) WriteVal(val interface{}, offset int) (int, error) {
	if s, isString := val.(string); isString {
		return w.WriteString(s, offset)
	}

	buf := bytes.NewBuffer(make([]byte, 0))

	err := binary.Write(buf, byteOrder, val)
	if err != nil {
		return 0, err
	}

	return w.Write(buf.Bytes(), offset)
}
1068277
func EncodeVarint(x uint32) []byte {
	if x>>7 == 0 {
		return []byte{
			byte(x),
		}
	}

	if x>>14 == 0 {
		return []byte{
			byte(0x80 | x>>7),
			byte(127 & x),
		}
	}

	if x>>21 == 0 {
		return []byte{
			byte(0x80 | x>>14),
			byte(0x80 | x>>7),
			byte(127 & x),
		}
	}

	return []byte{
		byte(0x80 | x>>21),
		byte(0x80 | x>>14),
		byte(0x80 | x>>7),
		byte(127 & x),
	}
}
1224457
func (enc *encoder) Encode(vs ...interface{}) (err error) {
	defer func() {
		err, _ = recover().(error)
	}()
	for _, v := range vs {
		enc.encode(reflect.ValueOf(v), 0)
	}
	return nil
}
91327
func (v UUIDValue) encode(w io.Writer) error {
	raw := rawValue{
		Type: v.valueType(),
	}

	return raw.encodeFixedSlice(w, v[:])
}
1030255
func (e *Error) Encode(w io.Writer, prver uint32) error {
	return WriteElements(w,
		e.Code,
		e.Data,
	)
}
1152131
func (e Encoder) Encode(dst interface{}) error {
	v := reflect.ValueOf(dst)
	n, err := encodeToNode(v, e.z)
	if err != nil {
		return err
	}
	s := n.values(e.d, e.e).Encode()
	l, err := io.WriteString(e.w, s)
	switch {
	case err != nil:
		return err
	case l != len(s):
		return errors.New("could not write data completely")
	}
	return nil
}
354401
func (f *FailInvalidOnionHmac) Encode(w io.Writer, pver uint32) error {
	return WriteElement(w, f.OnionSHA256[:])
}
400619
func encodeKeyValue(w io.Writer, p map[string]string) error {
	for k, v := range p {
		if err := writeKeyValuePair(w, k, v); err != nil {
			return err
		}
	}
	return nil
}
928499
func (enc *Encoder) Encode(m *Message) (err error) {

	_, err = enc.Write(m.Bytes())

	return
}
9855
func (msg *Init) Encode(w io.Writer, pver uint32) error {
	return WriteElements(w,
		msg.GlobalFeatures,
		msg.LocalFeatures,
	)
}
852125
func Encode(data interface{}) string {
	switch v := data.(type) {
	case string:
		return EncodeString(v)
	case int:
		return EncodeInt(v)
	case []interface{}:
		return EncodeList(v)
	case map[string]interface{}:
		return EncodeDict(v)
	default:
		panic("invalid type when encode")
	}
}
657476
func (enc *Encoder) Encode(v interface{}) error {
	if enc.err != nil {
		return enc.err
	}
	e := newEncodeState(enc.canonical)
	err := e.marshal(v)
	if err != nil {
		return err
	}

	if !enc.canonical {
		// Terminate each value with a newline.
		// This makes the output look a little nicer
		// when debugging, and some kind of space
		// is required if the encoded value was a number,
		// so that the reader knows there aren't more
		// digits coming.
		e.WriteByte('\n')
	}

	if _, err = enc.w.Write(e.Bytes()); err != nil {
		enc.err = err
	}
	encodeStatePool.Put(e)
	return err
}
1136635
func EncodeString(val interface{}) (string, error) {
	buf := new(bytes.Buffer)
	e := NewEncoder(buf)
	if err := e.Encode(val); err != nil {
		return "", err
	}
	return buf.String(), nil
}
881528
func Encode(v interface{}) ([]byte, error) {
	var buf bytes.Buffer
	zw := zlib.NewWriter(&buf)
	enc := json.NewEncoder(zw)

	if err := enc.Encode(v); err != nil {
		zw.Close()
		return nil, errors.Annotate(err, "failed to JSON-encode").Err()
	}
	if err := zw.Close(); err != nil {
		return nil, errors.Annotate(err, "failed to Close zlib Writer").Err()
	}
	return buf.Bytes(), nil
}
788627
func (v StringValue) encode(w io.Writer) error {
	raw := rawValue{
		Type: v.valueType(),
	}

	return raw.encodeString(w, string(v))
}
847035
func (e *Encoder) Encode(fields map[string]interface{}, fPort uint8) ([]byte, bool, error) {
	encoder := protocol.NewEncoder()
	for name, value := range fields {
		key, channel, err := parseName(name)
		if err != nil {
			continue
		}
		switch key {
		case valueKey:
			if val, ok := value.(float64); ok {
				encoder.AddPort(channel, float32(val))
			}
		}
	}
	return encoder.Bytes(), true, nil
}