func topLevelBuilder(val interface{}) (sb *valueBuilder, err error) {
	ival := reflect.ValueOf(val)
	if ival.Kind() != reflect.Ptr {
		return nil, fmt.Errorf("expecting pointer value, received %v", ival.Type())
	}
	return newValueBuilder(ival.Elem()), nil
}
topLevelBuilder returns a valid unmarshalable valueBuilder or an error
package bson
import (
	"bytes"
	"fmt"
	"io"
	"reflect"
	"time"
)
type Unmarshaler interface {
	UnmarshalBson(buf *bytes.Buffer, kind byte)
}
func (builder *valueBuilder) canUnMarshal() Unmarshaler {
	if builder.map_.IsValid() {
		return nil
	}
	if builder.val.CanAddr() {
		if unmarshaler, ok := builder.val.Addr().Interface().(Unmarshaler); ok {
			return unmarshaler
		}
	}
	return nil
}
func Unmarshal(b []byte, val interface{}) (err error) {
	return UnmarshalFromBuffer(bytes.NewBuffer(b), val)
}
func UnmarshalFromStream(reader io.Reader, val interface{}) (err error) {
	lenbuf := make([]byte, 4)
	var n int
	n, err = io.ReadFull(reader, lenbuf)
	if err != nil {
		return err
	}
	if n != 4 {
		return io.ErrUnexpectedEOF
	}
	length := Pack.Uint32(lenbuf)
	b := make([]byte, length)
	Pack.PutUint32(b, length)
	n, err = io.ReadFull(reader, b[4:])
	if err != nil {
		if err == io.EOF {
			return io.ErrUnexpectedEOF
		}
		return err
	}
	if n != int(length-4) {
		return io.ErrUnexpectedEOF
	}
	return UnmarshalFromBuffer(bytes.NewBuffer(b), val)
}
func UnmarshalFromBuffer(buf *bytes.Buffer, val interface{}) (err error) {
	defer handleError(&err)
	if val == nil {
		Skip(buf, Object)
		return nil
	}
	if unmarshaler, ok := val.(Unmarshaler); ok {
		unmarshaler.UnmarshalBson(buf, EOO)
		return nil
	}
	sb, err := topLevelBuilder(val)
	if err != nil {
		return err
	}
	decodeDocument(buf, sb, EOO)
	sb.save()
	return nil
}
func decodeDocument(buf *bytes.Buffer, builder *valueBuilder, kind byte) {
	if kind != EOO && kind != Object && kind != Array {
		panic(NewBsonError("unexpected kind: %v", kind))
	}
	Next(buf, 4)
	for kind := NextByte(buf); kind != EOO; kind = NextByte(buf) {
		b2 := builder.initField(ReadCString(buf), kind)
		if b2 == nil {
			Skip(buf, kind)
			continue
		}
		if unmarshaler := b2.canUnMarshal(); unmarshaler != nil {
			unmarshaler.UnmarshalBson(buf, kind)
			continue
		}
		switch b2.val.Kind() {
		case reflect.String:
			b2.setString(DecodeString(buf, kind))
		case reflect.Int64:
			b2.setInt(DecodeInt64(buf, kind))
		case reflect.Int32:
			b2.setInt(int64(DecodeInt32(buf, kind)))
		case reflect.Int:
			b2.setInt(int64(DecodeInt(buf, kind)))
		case reflect.Uint64:
			b2.setUint(DecodeUint64(buf, kind))
		case reflect.Uint32:
			b2.setUint(uint64(DecodeUint32(buf, kind)))
		case reflect.Uint:
			b2.setUint(uint64(DecodeUint(buf, kind)))
		case reflect.Float64:
			b2.setFloat(DecodeFloat64(buf, kind))
		case reflect.Bool:
			b2.setBool(DecodeBool(buf, kind))
		case reflect.Struct:
			if b2.val.Type() == timeType {
				b2.setTime(DecodeTime(buf, kind))
			} else {
				decodeDocument(buf, b2, kind)
			}
		case reflect.Map, reflect.Array:
			decodeDocument(buf, b2, kind)
		case reflect.Slice:
			if b2.val.Type() == bytesType {
				b2.setBytes(DecodeBinary(buf, kind))
			} else {
				decodeDocument(buf, b2, kind)
			}
		case reflect.Interface:
			b2.setInterface(DecodeInterface(buf, kind))
		default:
			panic(NewBsonError("cannot unmarshal into %v", b2.val.Kind()))
		}
		b2.save()
	}
}
type valueBuilder struct {
	val reflect.Value
	map_ reflect.Value
	key  reflect.Value
	index int
}