| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 | 
							- package mo
 
- import (
 
- 	"encoding/xml"
 
- 	"fmt"
 
- 	"time"
 
- )
 
- type Type byte
 
- // https://docs.mongodb.com/manual/reference/bson-types/
 
- const (
 
- 	TypeDouble     Type = 0x01 // float64
 
- 	TypeString     Type = 0x02 // string
 
- 	TypeObject     Type = 0x03 // M
 
- 	TypeArray      Type = 0x04 // A
 
- 	TypeBinary     Type = 0x05 // Binary reference https://bsonspec.org/spec.html subtype
 
- 	TypeObjectID   Type = 0x07 // ObjectID
 
- 	TypeBoolean    Type = 0x08 // bool
 
- 	TypeDateTime   Type = 0x09 // DateTime
 
- 	TypeNull       Type = 0x0A // Null represents the BSON null value.
 
- 	TypeRegex      Type = 0x0B // Regex
 
- 	TypeJavaScript Type = 0x0D // JavaScript
 
- 	TypeInt32      Type = 0x10 // int32
 
- 	TypeTimestamp  Type = 0x11 // Timestamp DO NOT USE, for internal MongoDB only: https://docs.mongodb.com/manual/reference/bson-types/#timestamps
 
- 	TypeInt64      Type = 0x12 // int64
 
- 	TypeDecimal128 Type = 0x13 // Decimal128
 
- 	TypeMinKey     Type = 0xFF // MinKey
 
- 	TypeMaxKey     Type = 0x7F // MaxKey
 
- 	TypeFloat64 = TypeDouble // alias
 
- 	TypeMap     = TypeObject
 
- 	TypeSlice   = TypeArray
 
- 	TypeBool    = TypeBoolean
 
- )
 
- var nameType = map[Type]string{
 
- 	TypeDouble:     "double",
 
- 	TypeString:     "string",
 
- 	TypeObject:     "object",
 
- 	TypeArray:      "array",
 
- 	TypeBinary:     "binary",
 
- 	TypeObjectID:   "objectID",
 
- 	TypeBoolean:    "boolean",
 
- 	TypeDateTime:   "datetime",
 
- 	TypeNull:       "null",
 
- 	TypeRegex:      "regex",
 
- 	TypeJavaScript: "javascript",
 
- 	TypeInt32:      "int32",
 
- 	TypeTimestamp:  "timestamp",
 
- 	TypeInt64:      "int64",
 
- 	TypeDecimal128: "decimal128",
 
- 	TypeMinKey:     "minKey",
 
- 	TypeMaxKey:     "maxKey",
 
- }
 
- var typeName = map[string]Type{
 
- 	"double":     TypeDouble,
 
- 	"string":     TypeString,
 
- 	"object":     TypeObject,
 
- 	"array":      TypeArray,
 
- 	"binary":     TypeBinary,
 
- 	"objectID":   TypeObjectID,
 
- 	"boolean":    TypeBoolean,
 
- 	"datetime":   TypeDateTime,
 
- 	"null":       TypeNull,
 
- 	"regex":      TypeRegex,
 
- 	"javascript": TypeJavaScript,
 
- 	"int32":      TypeInt32,
 
- 	"timestamp":  TypeTimestamp,
 
- 	"int64":      TypeInt64,
 
- 	"decimal128": TypeDecimal128,
 
- 	"minKey":     TypeMinKey,
 
- 	"maxKey":     TypeMaxKey,
 
- 	// alias
 
- 	"float64":  TypeDouble,
 
- 	"float":    TypeDouble,
 
- 	"map":      TypeObject,
 
- 	"slice":    TypeArray,
 
- 	"objectId": TypeObjectID,
 
- 	"bool":     TypeBoolean,
 
- 	"binData":  TypeBinary,
 
- 	"date":     TypeDateTime,
 
- 	"int":      TypeInt32,
 
- 	"long":     TypeInt64,
 
- 	"decimal":  TypeDecimal128,
 
- }
 
- func (t *Type) UnmarshalXMLAttr(attr xml.Attr) error {
 
- 	if v, ok := typeName[attr.Value]; ok {
 
- 		*t = v
 
- 		return nil
 
- 	}
 
- 	return fmt.Errorf("unknown mo.Type(%s)", attr.Value)
 
- }
 
- func (t *Type) String() string {
 
- 	if v, ok := nameType[*t]; ok {
 
- 		return fmt.Sprintf("mo.Type(%s)", v)
 
- 	}
 
- 	return fmt.Sprintf("mo.Type(%d)", t)
 
- }
 
- func (t *Type) Default() any {
 
- 	switch *t {
 
- 	case TypeDouble:
 
- 		return float64(0)
 
- 	case TypeString:
 
- 		return ""
 
- 	case TypeObject:
 
- 		return M{}
 
- 	case TypeArray:
 
- 		return A{}
 
- 	case TypeBinary:
 
- 		return Binary{}
 
- 	case TypeObjectID:
 
- 		return NilObjectID
 
- 	case TypeBoolean:
 
- 		return false
 
- 	case TypeDateTime:
 
- 		return DateTime(0)
 
- 	case TypeNull:
 
- 		return Null{}
 
- 	case TypeRegex:
 
- 		return Regex{}
 
- 	case TypeJavaScript:
 
- 		return JavaScript("")
 
- 	case TypeInt32:
 
- 		return int32(0)
 
- 	case TypeTimestamp:
 
- 		return Timestamp{}
 
- 	case TypeInt64:
 
- 		return int64(0)
 
- 	case TypeDecimal128:
 
- 		return NewDecimal128(0, 0)
 
- 	case TypeMinKey:
 
- 		return MinKey{}
 
- 	case TypeMaxKey:
 
- 		return MaxKey{}
 
- 	default:
 
- 		panic("unknown type")
 
- 	}
 
- }
 
- const (
 
- 	DefaultTimout = 10 * time.Second
 
- )
 
- const (
 
- 	DefaultDbName = "test"
 
- 	// ISODate 作为 DateTime 字符串时间模板, 来自 time.RFC3339 增加毫秒并移除 +7 偏移量, 见 time/format.go:96
 
- 	ISODate = "2006-01-02T15:04:05.000Z"
 
- )
 
- // https://www.mongodb.com/docs/v6.0/reference/operator/aggregation-pipeline/#aggregation-pipeline-stages
 
- const (
 
- 	PsMatch     = "$match"
 
- 	PsLookup    = "$lookup"
 
- 	PsProject   = "$project"
 
- 	PsGroup     = "$group"
 
- 	PsSort      = "$sort"
 
- 	PsLimit     = "$limit"
 
- 	PsSkip      = "$skip"
 
- 	PsSet       = "$set"
 
- 	PsDocuments = "$documents"
 
- )
 
- // https://www.mongodb.com/docs/v6.0/reference/operator/aggregation/#aggregation-pipeline-operators
 
- const (
 
- 	PoAdd = "$add"
 
- 	PoSum = "$sum"
 
- )
 
- const (
 
- 	PoCurrentDate = "$currentDate"
 
- )
 
- const (
 
- 	PoSet   = "$set"
 
- 	PoUnset = "$unset"
 
- )
 
- const (
 
- 	PoPush    = "$push"
 
- 	PoPull    = "$pull"
 
- 	PoPullAll = "$pullAll"
 
- )
 
 
  |