func (v Val) Equal(v2 Val) bool {
	if v.Type() != v2.Type() {
		return false
	}
	if v.IsZero() && v2.IsZero() {
		return true
	}
	switch v.Type() {
	case bsontype.Double, bsontype.DateTime, bsontype.Timestamp, bsontype.Int64:
		return bytes.Equal(v.bootstrap[0:8], v2.bootstrap[0:8])
	case bsontype.String:
		return v.string() == v2.string()
	case bsontype.EmbeddedDocument:
		return v.equalDocs(v2)
	case bsontype.Array:
		return v.Array().Equal(v2.Array())
	case bsontype.Binary:
		return v.primitive.(primitive.Binary).Equal(v2.primitive.(primitive.Binary))
	case bsontype.Undefined:
		return true
	case bsontype.ObjectID:
		return bytes.Equal(v.bootstrap[0:12], v2.bootstrap[0:12])
	case bsontype.Boolean:
		return v.bootstrap[0] == v2.bootstrap[0]
	case bsontype.Null:
		return true
	case bsontype.Regex:
		return v.primitive.(primitive.Regex).Equal(v2.primitive.(primitive.Regex))
	case bsontype.DBPointer:
		return v.primitive.(primitive.DBPointer).Equal(v2.primitive.(primitive.DBPointer))
	case bsontype.JavaScript:
		return v.JavaScript() == v2.JavaScript()
	case bsontype.Symbol:
		return v.Symbol() == v2.Symbol()
	case bsontype.CodeWithScope:
		code1, scope1 := v.primitive.(primitive.CodeWithScope).Code, v.primitive.(primitive.CodeWithScope).Scope
		code2, scope2 := v2.primitive.(primitive.CodeWithScope).Code, v2.primitive.(primitive.CodeWithScope).Scope
		return code1 == code2 && v.equalInterfaceDocs(scope1, scope2)
	case bsontype.Int32:
		return v.Int32() == v2.Int32()
	case bsontype.Decimal128:
		h, l := v.Decimal128().GetBytes()
		h2, l2 := v2.Decimal128().GetBytes()
		return h == h2 && l == l2
	case bsontype.MinKey:
		return true
	case bsontype.MaxKey:
		return true
	default:
		return false
	}
}
Equal compares v to v2 and returns true if they are equal. Unknown BSON types are
never equal. Two empty values are equal.
package bsonx
import (
	"bytes"
	"encoding/binary"
	"errors"
	"fmt"
	"math"
	"time"
	"go.mongodb.org/mongo-driver/bson/bsontype"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
type Val struct {
	t         bsontype.Type
	bootstrap [15]byte
	primitive interface{}
}
func (v Val) reset() Val {
	v.primitive = nil  
	v.t = bsontype.Type(0)
	v.bootstrap[0] = 0x00
	v.bootstrap[1] = 0x00
	v.bootstrap[2] = 0x00
	v.bootstrap[3] = 0x00
	v.bootstrap[4] = 0x00
	v.bootstrap[5] = 0x00
	v.bootstrap[6] = 0x00
	v.bootstrap[7] = 0x00
	v.bootstrap[8] = 0x00
	v.bootstrap[9] = 0x00
	v.bootstrap[10] = 0x00
	v.bootstrap[11] = 0x00
	v.bootstrap[12] = 0x00
	v.bootstrap[13] = 0x00
	v.bootstrap[14] = 0x00
	return v
}
func (v Val) string() string {
	if v.primitive != nil {
		return v.primitive.(string)
	}
	length := uint8(v.bootstrap[0])
	return string(v.bootstrap[1 : length+1])
}
func (v Val) writestring(str string) Val {
	switch {
	case len(str) < 15:
		v.bootstrap[0] = uint8(len(str))
		copy(v.bootstrap[1:], str)
	default:
		v.primitive = str
	}
	return v
}
func (v Val) i64() int64 {
	return int64(v.bootstrap[0]) | int64(v.bootstrap[1])<<8 | int64(v.bootstrap[2])<<16 |
		int64(v.bootstrap[3])<<24 | int64(v.bootstrap[4])<<32 | int64(v.bootstrap[5])<<40 |
		int64(v.bootstrap[6])<<48 | int64(v.bootstrap[7])<<56
}
func (v Val) writei64(i64 int64) Val {
	v.bootstrap[0] = byte(i64)
	v.bootstrap[1] = byte(i64 >> 8)
	v.bootstrap[2] = byte(i64 >> 16)
	v.bootstrap[3] = byte(i64 >> 24)
	v.bootstrap[4] = byte(i64 >> 32)
	v.bootstrap[5] = byte(i64 >> 40)
	v.bootstrap[6] = byte(i64 >> 48)
	v.bootstrap[7] = byte(i64 >> 56)
	return v
}
func (v Val) IsZero() bool { return v.t == bsontype.Type(0) || v.t == bsontype.Null }
func (v Val) String() string {
	return fmt.Sprintf("%v", v.Interface())
}
func (v Val) Interface() interface{} {
	switch v.Type() {
	case bsontype.Double:
		return v.Double()
	case bsontype.String:
		return v.StringValue()
	case bsontype.EmbeddedDocument:
		switch v.primitive.(type) {
		case Doc:
			return v.primitive.(Doc)
		case MDoc:
			return v.primitive.(MDoc)
		default:
			return primitive.Null{}
		}
	case bsontype.Array:
		return v.Array()
	case bsontype.Binary:
		return v.primitive.(primitive.Binary)
	case bsontype.Undefined:
		return primitive.Undefined{}
	case bsontype.ObjectID:
		return v.ObjectID()
	case bsontype.Boolean:
		return v.Boolean()
	case bsontype.DateTime:
		return v.DateTime()
	case bsontype.Null:
		return primitive.Null{}
	case bsontype.Regex:
		return v.primitive.(primitive.Regex)
	case bsontype.DBPointer:
		return v.primitive.(primitive.DBPointer)
	case bsontype.JavaScript:
		return v.JavaScript()
	case bsontype.Symbol:
		return v.Symbol()
	case bsontype.CodeWithScope:
		return v.primitive.(primitive.CodeWithScope)
	case bsontype.Int32:
		return v.Int32()
	case bsontype.Timestamp:
		t, i := v.Timestamp()
		return primitive.Timestamp{T: t, I: i}
	case bsontype.Int64:
		return v.Int64()
	case bsontype.Decimal128:
		return v.Decimal128()
	case bsontype.MinKey:
		return primitive.MinKey{}
	case bsontype.MaxKey:
		return primitive.MaxKey{}
	default:
		return primitive.Null{}
	}
}
func (v Val) MarshalBSONValue() (bsontype.Type, []byte, error) {
	return v.MarshalAppendBSONValue(nil)
}
func (v Val) MarshalAppendBSONValue(dst []byte) (bsontype.Type, []byte, error) {
	t := v.Type()
	switch v.Type() {
	case bsontype.Double:
		dst = bsoncore.AppendDouble(dst, v.Double())
	case bsontype.String:
		dst = bsoncore.AppendString(dst, v.String())
	case bsontype.EmbeddedDocument:
		switch v.primitive.(type) {
		case Doc:
			t, dst, _ = v.primitive.(Doc).MarshalBSONValue()  
		case MDoc:
			t, dst, _ = v.primitive.(MDoc).MarshalBSONValue()  
		}
	case bsontype.Array:
		t, dst, _ = v.Array().MarshalBSONValue()  
	case bsontype.Binary:
		subtype, bindata := v.Binary()
		dst = bsoncore.AppendBinary(dst, subtype, bindata)
	case bsontype.Undefined:
	case bsontype.ObjectID:
		dst = bsoncore.AppendObjectID(dst, v.ObjectID())
	case bsontype.Boolean:
		dst = bsoncore.AppendBoolean(dst, v.Boolean())
	case bsontype.DateTime:
		dst = bsoncore.AppendDateTime(dst, int64(v.DateTime()))
	case bsontype.Null:
	case bsontype.Regex:
		pattern, options := v.Regex()
		dst = bsoncore.AppendRegex(dst, pattern, options)
	case bsontype.DBPointer:
		ns, ptr := v.DBPointer()
		dst = bsoncore.AppendDBPointer(dst, ns, ptr)
	case bsontype.JavaScript:
		dst = bsoncore.AppendJavaScript(dst, string(v.JavaScript()))
	case bsontype.Symbol:
		dst = bsoncore.AppendSymbol(dst, string(v.Symbol()))
	case bsontype.CodeWithScope:
		code, doc := v.CodeWithScope()
		var scope []byte
		scope, _ = doc.MarshalBSON()  
		dst = bsoncore.AppendCodeWithScope(dst, code, scope)
	case bsontype.Int32:
		dst = bsoncore.AppendInt32(dst, v.Int32())
	case bsontype.Timestamp:
		t, i := v.Timestamp()
		dst = bsoncore.AppendTimestamp(dst, t, i)
	case bsontype.Int64:
		dst = bsoncore.AppendInt64(dst, v.Int64())
	case bsontype.Decimal128:
		dst = bsoncore.AppendDecimal128(dst, v.Decimal128())
	case bsontype.MinKey:
	case bsontype.MaxKey:
	default:
		panic(fmt.Errorf("invalid BSON type %v", t))
	}
	return t, dst, nil
}
func (v *Val) UnmarshalBSONValue(t bsontype.Type, data []byte) error {
	if v == nil {
		return errors.New("cannot unmarshal into nil Value")
	}
	var err error
	var ok = true
	var rem []byte
	switch t {
	case bsontype.Double:
		var f64 float64
		f64, rem, ok = bsoncore.ReadDouble(data)
		*v = Double(f64)
	case bsontype.String:
		var str string
		str, rem, ok = bsoncore.ReadString(data)
		*v = String(str)
	case bsontype.EmbeddedDocument:
		var raw []byte
		var doc Doc
		raw, rem, ok = bsoncore.ReadDocument(data)
		doc, err = ReadDoc(raw)
		*v = Document(doc)
	case bsontype.Array:
		var raw []byte
		arr := make(Arr, 0)
		raw, rem, ok = bsoncore.ReadArray(data)
		err = arr.UnmarshalBSONValue(t, raw)
		*v = Array(arr)
	case bsontype.Binary:
		var subtype byte
		var bindata []byte
		subtype, bindata, rem, ok = bsoncore.ReadBinary(data)
		*v = Binary(subtype, bindata)
	case bsontype.Undefined:
		*v = Undefined()
	case bsontype.ObjectID:
		var oid primitive.ObjectID
		oid, rem, ok = bsoncore.ReadObjectID(data)
		*v = ObjectID(oid)
	case bsontype.Boolean:
		var b bool
		b, rem, ok = bsoncore.ReadBoolean(data)
		*v = Boolean(b)
	case bsontype.DateTime:
		var dt int64
		dt, rem, ok = bsoncore.ReadDateTime(data)
		*v = DateTime(dt)
	case bsontype.Null:
		*v = Null()
	case bsontype.Regex:
		var pattern, options string
		pattern, options, rem, ok = bsoncore.ReadRegex(data)
		*v = Regex(pattern, options)
	case bsontype.DBPointer:
		var ns string
		var ptr primitive.ObjectID
		ns, ptr, rem, ok = bsoncore.ReadDBPointer(data)
		*v = DBPointer(ns, ptr)
	case bsontype.JavaScript:
		var js string
		js, rem, ok = bsoncore.ReadJavaScript(data)
		*v = JavaScript(js)
	case bsontype.Symbol:
		var symbol string
		symbol, rem, ok = bsoncore.ReadSymbol(data)
		*v = Symbol(symbol)
	case bsontype.CodeWithScope:
		var raw []byte
		var code string
		var scope Doc
		code, raw, rem, ok = bsoncore.ReadCodeWithScope(data)
		scope, err = ReadDoc(raw)
		*v = CodeWithScope(code, scope)
	case bsontype.Int32:
		var i32 int32
		i32, rem, ok = bsoncore.ReadInt32(data)
		*v = Int32(i32)
	case bsontype.Timestamp:
		var i, t uint32
		t, i, rem, ok = bsoncore.ReadTimestamp(data)
		*v = Timestamp(t, i)
	case bsontype.Int64:
		var i64 int64
		i64, rem, ok = bsoncore.ReadInt64(data)
		*v = Int64(i64)
	case bsontype.Decimal128:
		var d128 primitive.Decimal128
		d128, rem, ok = bsoncore.ReadDecimal128(data)
		*v = Decimal128(d128)
	case bsontype.MinKey:
		*v = MinKey()
	case bsontype.MaxKey:
		*v = MaxKey()
	default:
		err = fmt.Errorf("invalid BSON type %v", t)
	}
	if !ok && err == nil {
		err = bsoncore.NewInsufficientBytesError(data, rem)
	}
	return err
}
func (v Val) Type() bsontype.Type {
	if v.t == bsontype.Type(0) {
		return bsontype.Null
	}
	return v.t
}
func (v Val) IsNumber() bool {
	switch v.Type() {
	case bsontype.Double, bsontype.Int32, bsontype.Int64, bsontype.Decimal128:
		return true
	default:
		return false
	}
}
func (v Val) Double() float64 {
	if v.t != bsontype.Double {
		panic(ElementTypeError{"bson.Value.Double", v.t})
	}
	return math.Float64frombits(binary.LittleEndian.Uint64(v.bootstrap[0:8]))
}
func (v Val) DoubleOK() (float64, bool) {
	if v.t != bsontype.Double {
		return 0, false
	}
	return math.Float64frombits(binary.LittleEndian.Uint64(v.bootstrap[0:8])), true
}
func (v Val) StringValue() string {
	if v.t != bsontype.String {
		panic(ElementTypeError{"bson.Value.StringValue", v.t})
	}
	return v.string()
}
func (v Val) StringValueOK() (string, bool) {
	if v.t != bsontype.String {
		return "", false
	}
	return v.string(), true
}
func (v Val) asDoc() Doc {
	doc, ok := v.primitive.(Doc)
	if ok {
		return doc
	}
	mdoc := v.primitive.(MDoc)
	for k, v := range mdoc {
		doc = append(doc, Elem{k, v})
	}
	return doc
}
func (v Val) asMDoc() MDoc {
	mdoc, ok := v.primitive.(MDoc)
	if ok {
		return mdoc
	}
	mdoc = make(MDoc)
	doc := v.primitive.(Doc)
	for _, elem := range doc {
		mdoc[elem.Key] = elem.Value
	}
	return mdoc
}
func (v Val) Document() Doc {
	if v.t != bsontype.EmbeddedDocument {
		panic(ElementTypeError{"bson.Value.Document", v.t})
	}
	return v.asDoc()
}
func (v Val) DocumentOK() (Doc, bool) {
	if v.t != bsontype.EmbeddedDocument {
		return nil, false
	}
	return v.asDoc(), true
}
func (v Val) MDocument() MDoc {
	if v.t != bsontype.EmbeddedDocument {
		panic(ElementTypeError{"bson.Value.MDocument", v.t})
	}
	return v.asMDoc()
}
func (v Val) MDocumentOK() (MDoc, bool) {
	if v.t != bsontype.EmbeddedDocument {
		return nil, false
	}
	return v.asMDoc(), true
}
func (v Val) Array() Arr {
	if v.t != bsontype.Array {
		panic(ElementTypeError{"bson.Value.Array", v.t})
	}
	return v.primitive.(Arr)
}
func (v Val) ArrayOK() (Arr, bool) {
	if v.t != bsontype.Array {
		return nil, false
	}
	return v.primitive.(Arr), true
}
func (v Val) Binary() (byte, []byte) {
	if v.t != bsontype.Binary {
		panic(ElementTypeError{"bson.Value.Binary", v.t})
	}
	bin := v.primitive.(primitive.Binary)
	return bin.Subtype, bin.Data
}
func (v Val) BinaryOK() (byte, []byte, bool) {
	if v.t != bsontype.Binary {
		return 0x00, nil, false
	}
	bin := v.primitive.(primitive.Binary)
	return bin.Subtype, bin.Data, true
}
func (v Val) Undefined() {
	if v.t != bsontype.Undefined {
		panic(ElementTypeError{"bson.Value.Undefined", v.t})
	}
	return
}
func (v Val) UndefinedOK() bool {
	if v.t != bsontype.Undefined {
		return false
	}
	return true
}
func (v Val) ObjectID() primitive.ObjectID {
	if v.t != bsontype.ObjectID {
		panic(ElementTypeError{"bson.Value.ObjectID", v.t})
	}
	var oid primitive.ObjectID
	copy(oid[:], v.bootstrap[:12])
	return oid
}
func (v Val) ObjectIDOK() (primitive.ObjectID, bool) {
	if v.t != bsontype.ObjectID {
		return primitive.ObjectID{}, false
	}
	var oid primitive.ObjectID
	copy(oid[:], v.bootstrap[:12])
	return oid, true
}
func (v Val) Boolean() bool {
	if v.t != bsontype.Boolean {
		panic(ElementTypeError{"bson.Value.Boolean", v.t})
	}
	return v.bootstrap[0] == 0x01
}
func (v Val) BooleanOK() (bool, bool) {
	if v.t != bsontype.Boolean {
		return false, false
	}
	return v.bootstrap[0] == 0x01, true
}
func (v Val) DateTime() int64 {
	if v.t != bsontype.DateTime {
		panic(ElementTypeError{"bson.Value.DateTime", v.t})
	}
	return v.i64()
}
func (v Val) DateTimeOK() (int64, bool) {
	if v.t != bsontype.DateTime {
		return 0, false
	}
	return v.i64(), true
}
func (v Val) Time() time.Time {
	if v.t != bsontype.DateTime {
		panic(ElementTypeError{"bson.Value.Time", v.t})
	}
	i := v.i64()
	return time.Unix(int64(i)/1000, int64(i)%1000*1000000)
}
func (v Val) TimeOK() (time.Time, bool) {
	if v.t != bsontype.DateTime {
		return time.Time{}, false
	}
	i := v.i64()
	return time.Unix(int64(i)/1000, int64(i)%1000*1000000), true
}
func (v Val) Null() {
	if v.t != bsontype.Null && v.t != bsontype.Type(0) {
		panic(ElementTypeError{"bson.Value.Null", v.t})
	}
	return
}
func (v Val) NullOK() bool {
	if v.t != bsontype.Null && v.t != bsontype.Type(0) {
		return false
	}
	return true
}
func (v Val) Regex() (pattern, options string) {
	if v.t != bsontype.Regex {
		panic(ElementTypeError{"bson.Value.Regex", v.t})
	}
	regex := v.primitive.(primitive.Regex)
	return regex.Pattern, regex.Options
}
func (v Val) RegexOK() (pattern, options string, ok bool) {
	if v.t != bsontype.Regex {
		return "", "", false
	}
	regex := v.primitive.(primitive.Regex)
	return regex.Pattern, regex.Options, true
}
func (v Val) DBPointer() (string, primitive.ObjectID) {
	if v.t != bsontype.DBPointer {
		panic(ElementTypeError{"bson.Value.DBPointer", v.t})
	}
	dbptr := v.primitive.(primitive.DBPointer)
	return dbptr.DB, dbptr.Pointer
}
func (v Val) DBPointerOK() (string, primitive.ObjectID, bool) {
	if v.t != bsontype.DBPointer {
		return "", primitive.ObjectID{}, false
	}
	dbptr := v.primitive.(primitive.DBPointer)
	return dbptr.DB, dbptr.Pointer, true
}
func (v Val) JavaScript() string {
	if v.t != bsontype.JavaScript {
		panic(ElementTypeError{"bson.Value.JavaScript", v.t})
	}
	return v.string()
}
func (v Val) JavaScriptOK() (string, bool) {
	if v.t != bsontype.JavaScript {
		return "", false
	}
	return v.string(), true
}
func (v Val) Symbol() string {
	if v.t != bsontype.Symbol {
		panic(ElementTypeError{"bson.Value.Symbol", v.t})
	}
	return v.string()
}
func (v Val) SymbolOK() (string, bool) {
	if v.t != bsontype.Symbol {
		return "", false
	}
	return v.string(), true
}
func (v Val) CodeWithScope() (string, Doc) {
	if v.t != bsontype.CodeWithScope {
		panic(ElementTypeError{"bson.Value.CodeWithScope", v.t})
	}
	cws := v.primitive.(primitive.CodeWithScope)
	return string(cws.Code), cws.Scope.(Doc)
}
func (v Val) CodeWithScopeOK() (string, Doc, bool) {
	if v.t != bsontype.CodeWithScope {
		return "", nil, false
	}
	cws := v.primitive.(primitive.CodeWithScope)
	return string(cws.Code), cws.Scope.(Doc), true
}
func (v Val) Int32() int32 {
	if v.t != bsontype.Int32 {
		panic(ElementTypeError{"bson.Value.Int32", v.t})
	}
	return int32(v.bootstrap[0]) | int32(v.bootstrap[1])<<8 |
		int32(v.bootstrap[2])<<16 | int32(v.bootstrap[3])<<24
}
func (v Val) Int32OK() (int32, bool) {
	if v.t != bsontype.Int32 {
		return 0, false
	}
	return int32(v.bootstrap[0]) | int32(v.bootstrap[1])<<8 |
			int32(v.bootstrap[2])<<16 | int32(v.bootstrap[3])<<24,
		true
}
func (v Val) Timestamp() (t, i uint32) {
	if v.t != bsontype.Timestamp {
		panic(ElementTypeError{"bson.Value.Timestamp", v.t})
	}
	return uint32(v.bootstrap[4]) | uint32(v.bootstrap[5])<<8 |
			uint32(v.bootstrap[6])<<16 | uint32(v.bootstrap[7])<<24,
		uint32(v.bootstrap[0]) | uint32(v.bootstrap[1])<<8 |
			uint32(v.bootstrap[2])<<16 | uint32(v.bootstrap[3])<<24
}
func (v Val) TimestampOK() (t uint32, i uint32, ok bool) {
	if v.t != bsontype.Timestamp {
		return 0, 0, false
	}
	return uint32(v.bootstrap[4]) | uint32(v.bootstrap[5])<<8 |
			uint32(v.bootstrap[6])<<16 | uint32(v.bootstrap[7])<<24,
		uint32(v.bootstrap[0]) | uint32(v.bootstrap[1])<<8 |
			uint32(v.bootstrap[2])<<16 | uint32(v.bootstrap[3])<<24,
		true
}
func (v Val) Int64() int64 {
	if v.t != bsontype.Int64 {
		panic(ElementTypeError{"bson.Value.Int64", v.t})
	}
	return v.i64()
}
func (v Val) Int64OK() (int64, bool) {
	if v.t != bsontype.Int64 {
		return 0, false
	}
	return v.i64(), true
}
func (v Val) Decimal128() primitive.Decimal128 {
	if v.t != bsontype.Decimal128 {
		panic(ElementTypeError{"bson.Value.Decimal128", v.t})
	}
	return v.primitive.(primitive.Decimal128)
}
func (v Val) Decimal128OK() (primitive.Decimal128, bool) {
	if v.t != bsontype.Decimal128 {
		return primitive.Decimal128{}, false
	}
	return v.primitive.(primitive.Decimal128), true
}
func (v Val) MinKey() {
	if v.t != bsontype.MinKey {
		panic(ElementTypeError{"bson.Value.MinKey", v.t})
	}
	return
}
func (v Val) MinKeyOK() bool {
	if v.t != bsontype.MinKey {
		return false
	}
	return true
}
func (v Val) MaxKey() {
	if v.t != bsontype.MaxKey {
		panic(ElementTypeError{"bson.Value.MaxKey", v.t})
	}
	return
}
func (v Val) MaxKeyOK() bool {
	if v.t != bsontype.MaxKey {
		return false
	}
	return true
}