Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package bsonx |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "encoding/binary" |
| 12 | "errors" |
| 13 | "fmt" |
| 14 | "math" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/mongodb/mongo-go-driver/bson/bsontype" |
| 18 | "github.com/mongodb/mongo-go-driver/bson/primitive" |
| 19 | "github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore" |
| 20 | ) |
| 21 | |
| 22 | // Val represents a BSON value. |
| 23 | type Val struct { |
| 24 | // NOTE: The bootstrap is a small amount of space that'll be on the stack. At 15 bytes this |
| 25 | // doesn't make this type any larger, since there are 7 bytes of padding and we want an int64 to |
| 26 | // store small values (e.g. boolean, double, int64, etc...). The primitive property is where all |
| 27 | // of the larger values go. They will use either Go primitives or the primitive.* types. |
| 28 | t bsontype.Type |
| 29 | bootstrap [15]byte |
| 30 | primitive interface{} |
| 31 | } |
| 32 | |
| 33 | func (v Val) reset() Val { |
| 34 | v.primitive = nil // clear out any pointers so we don't accidentally stop them from being garbage collected. |
| 35 | v.t = bsontype.Type(0) |
| 36 | v.bootstrap[0] = 0x00 |
| 37 | v.bootstrap[1] = 0x00 |
| 38 | v.bootstrap[2] = 0x00 |
| 39 | v.bootstrap[3] = 0x00 |
| 40 | v.bootstrap[4] = 0x00 |
| 41 | v.bootstrap[5] = 0x00 |
| 42 | v.bootstrap[6] = 0x00 |
| 43 | v.bootstrap[7] = 0x00 |
| 44 | v.bootstrap[8] = 0x00 |
| 45 | v.bootstrap[9] = 0x00 |
| 46 | v.bootstrap[10] = 0x00 |
| 47 | v.bootstrap[11] = 0x00 |
| 48 | v.bootstrap[12] = 0x00 |
| 49 | v.bootstrap[13] = 0x00 |
| 50 | v.bootstrap[14] = 0x00 |
| 51 | return v |
| 52 | } |
| 53 | |
| 54 | func (v Val) string() string { |
| 55 | if v.primitive != nil { |
| 56 | return v.primitive.(string) |
| 57 | } |
| 58 | // The string will either end with a null byte or it fills the entire bootstrap space. |
| 59 | length := uint8(v.bootstrap[0]) |
| 60 | return string(v.bootstrap[1 : length+1]) |
| 61 | } |
| 62 | |
| 63 | func (v Val) writestring(str string) Val { |
| 64 | switch { |
| 65 | case len(str) < 15: |
| 66 | v.bootstrap[0] = uint8(len(str)) |
| 67 | copy(v.bootstrap[1:], str) |
| 68 | default: |
| 69 | v.primitive = str |
| 70 | } |
| 71 | return v |
| 72 | } |
| 73 | |
| 74 | func (v Val) i64() int64 { |
| 75 | return int64(v.bootstrap[0]) | int64(v.bootstrap[1])<<8 | int64(v.bootstrap[2])<<16 | |
| 76 | int64(v.bootstrap[3])<<24 | int64(v.bootstrap[4])<<32 | int64(v.bootstrap[5])<<40 | |
| 77 | int64(v.bootstrap[6])<<48 | int64(v.bootstrap[7])<<56 |
| 78 | } |
| 79 | |
| 80 | func (v Val) writei64(i64 int64) Val { |
| 81 | v.bootstrap[0] = byte(i64) |
| 82 | v.bootstrap[1] = byte(i64 >> 8) |
| 83 | v.bootstrap[2] = byte(i64 >> 16) |
| 84 | v.bootstrap[3] = byte(i64 >> 24) |
| 85 | v.bootstrap[4] = byte(i64 >> 32) |
| 86 | v.bootstrap[5] = byte(i64 >> 40) |
| 87 | v.bootstrap[6] = byte(i64 >> 48) |
| 88 | v.bootstrap[7] = byte(i64 >> 56) |
| 89 | return v |
| 90 | } |
| 91 | |
| 92 | // IsZero returns true if this value is zero or a BSON null. |
| 93 | func (v Val) IsZero() bool { return v.t == bsontype.Type(0) || v.t == bsontype.Null } |
| 94 | |
| 95 | func (v Val) String() string { |
| 96 | // TODO(GODRIVER-612): When bsoncore has appenders for extended JSON use that here. |
| 97 | return fmt.Sprintf("%v", v.Interface()) |
| 98 | } |
| 99 | |
| 100 | // Interface returns the Go value of this Value as an empty interface. |
| 101 | // |
| 102 | // This method will return nil if it is empty, otherwise it will return a Go primitive or a |
| 103 | // primitive.* instance. |
| 104 | func (v Val) Interface() interface{} { |
| 105 | switch v.Type() { |
| 106 | case bsontype.Double: |
| 107 | return v.Double() |
| 108 | case bsontype.String: |
| 109 | return v.StringValue() |
| 110 | case bsontype.EmbeddedDocument: |
| 111 | switch v.primitive.(type) { |
| 112 | case Doc: |
| 113 | return v.primitive.(Doc) |
| 114 | case MDoc: |
| 115 | return v.primitive.(MDoc) |
| 116 | default: |
| 117 | return primitive.Null{} |
| 118 | } |
| 119 | case bsontype.Array: |
| 120 | return v.Array() |
| 121 | case bsontype.Binary: |
| 122 | return v.primitive.(primitive.Binary) |
| 123 | case bsontype.Undefined: |
| 124 | return primitive.Undefined{} |
| 125 | case bsontype.ObjectID: |
| 126 | return v.ObjectID() |
| 127 | case bsontype.Boolean: |
| 128 | return v.Boolean() |
| 129 | case bsontype.DateTime: |
| 130 | return v.DateTime() |
| 131 | case bsontype.Null: |
| 132 | return primitive.Null{} |
| 133 | case bsontype.Regex: |
| 134 | return v.primitive.(primitive.Regex) |
| 135 | case bsontype.DBPointer: |
| 136 | return v.primitive.(primitive.DBPointer) |
| 137 | case bsontype.JavaScript: |
| 138 | return v.JavaScript() |
| 139 | case bsontype.Symbol: |
| 140 | return v.Symbol() |
| 141 | case bsontype.CodeWithScope: |
| 142 | return v.primitive.(primitive.CodeWithScope) |
| 143 | case bsontype.Int32: |
| 144 | return v.Int32() |
| 145 | case bsontype.Timestamp: |
| 146 | t, i := v.Timestamp() |
| 147 | return primitive.Timestamp{T: t, I: i} |
| 148 | case bsontype.Int64: |
| 149 | return v.Int64() |
| 150 | case bsontype.Decimal128: |
| 151 | return v.Decimal128() |
| 152 | case bsontype.MinKey: |
| 153 | return primitive.MinKey{} |
| 154 | case bsontype.MaxKey: |
| 155 | return primitive.MaxKey{} |
| 156 | default: |
| 157 | return primitive.Null{} |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // MarshalBSONValue implements the bsoncodec.ValueMarshaler interface. |
| 162 | func (v Val) MarshalBSONValue() (bsontype.Type, []byte, error) { |
| 163 | return v.MarshalAppendBSONValue(nil) |
| 164 | } |
| 165 | |
| 166 | // MarshalAppendBSONValue is similar to MarshalBSONValue, but allows the caller to specify a slice |
| 167 | // to add the bytes to. |
| 168 | func (v Val) MarshalAppendBSONValue(dst []byte) (bsontype.Type, []byte, error) { |
| 169 | t := v.Type() |
| 170 | switch v.Type() { |
| 171 | case bsontype.Double: |
| 172 | dst = bsoncore.AppendDouble(dst, v.Double()) |
| 173 | case bsontype.String: |
| 174 | dst = bsoncore.AppendString(dst, v.String()) |
| 175 | case bsontype.EmbeddedDocument: |
| 176 | switch v.primitive.(type) { |
| 177 | case Doc: |
| 178 | t, dst, _ = v.primitive.(Doc).MarshalBSONValue() // Doc.MarshalBSONValue never returns an error. |
| 179 | case MDoc: |
| 180 | t, dst, _ = v.primitive.(MDoc).MarshalBSONValue() // MDoc.MarshalBSONValue never returns an error. |
| 181 | } |
| 182 | case bsontype.Array: |
| 183 | t, dst, _ = v.Array().MarshalBSONValue() // Arr.MarshalBSON never returns an error. |
| 184 | case bsontype.Binary: |
| 185 | subtype, bindata := v.Binary() |
| 186 | dst = bsoncore.AppendBinary(dst, subtype, bindata) |
| 187 | case bsontype.Undefined: |
| 188 | case bsontype.ObjectID: |
| 189 | dst = bsoncore.AppendObjectID(dst, v.ObjectID()) |
| 190 | case bsontype.Boolean: |
| 191 | dst = bsoncore.AppendBoolean(dst, v.Boolean()) |
| 192 | case bsontype.DateTime: |
| 193 | dst = bsoncore.AppendDateTime(dst, int64(v.DateTime())) |
| 194 | case bsontype.Null: |
| 195 | case bsontype.Regex: |
| 196 | pattern, options := v.Regex() |
| 197 | dst = bsoncore.AppendRegex(dst, pattern, options) |
| 198 | case bsontype.DBPointer: |
| 199 | ns, ptr := v.DBPointer() |
| 200 | dst = bsoncore.AppendDBPointer(dst, ns, ptr) |
| 201 | case bsontype.JavaScript: |
| 202 | dst = bsoncore.AppendJavaScript(dst, string(v.JavaScript())) |
| 203 | case bsontype.Symbol: |
| 204 | dst = bsoncore.AppendSymbol(dst, string(v.Symbol())) |
| 205 | case bsontype.CodeWithScope: |
| 206 | code, doc := v.CodeWithScope() |
| 207 | var scope []byte |
| 208 | scope, _ = doc.MarshalBSON() // Doc.MarshalBSON never returns an error. |
| 209 | dst = bsoncore.AppendCodeWithScope(dst, code, scope) |
| 210 | case bsontype.Int32: |
| 211 | dst = bsoncore.AppendInt32(dst, v.Int32()) |
| 212 | case bsontype.Timestamp: |
| 213 | t, i := v.Timestamp() |
| 214 | dst = bsoncore.AppendTimestamp(dst, t, i) |
| 215 | case bsontype.Int64: |
| 216 | dst = bsoncore.AppendInt64(dst, v.Int64()) |
| 217 | case bsontype.Decimal128: |
| 218 | dst = bsoncore.AppendDecimal128(dst, v.Decimal128()) |
| 219 | case bsontype.MinKey: |
| 220 | case bsontype.MaxKey: |
| 221 | default: |
| 222 | panic(fmt.Errorf("invalid BSON type %v", t)) |
| 223 | } |
| 224 | |
| 225 | return t, dst, nil |
| 226 | } |
| 227 | |
| 228 | // UnmarshalBSONValue implements the bsoncodec.ValueUnmarshaler interface. |
| 229 | func (v *Val) UnmarshalBSONValue(t bsontype.Type, data []byte) error { |
| 230 | if v == nil { |
| 231 | return errors.New("cannot unmarshal into nil Value") |
| 232 | } |
| 233 | var err error |
| 234 | var ok = true |
| 235 | var rem []byte |
| 236 | switch t { |
| 237 | case bsontype.Double: |
| 238 | var f64 float64 |
| 239 | f64, rem, ok = bsoncore.ReadDouble(data) |
| 240 | *v = Double(f64) |
| 241 | case bsontype.String: |
| 242 | var str string |
| 243 | str, rem, ok = bsoncore.ReadString(data) |
| 244 | *v = String(str) |
| 245 | case bsontype.EmbeddedDocument: |
| 246 | var raw []byte |
| 247 | var doc Doc |
| 248 | raw, rem, ok = bsoncore.ReadDocument(data) |
| 249 | doc, err = ReadDoc(raw) |
| 250 | *v = Document(doc) |
| 251 | case bsontype.Array: |
| 252 | var raw []byte |
| 253 | arr := make(Arr, 0) |
| 254 | raw, rem, ok = bsoncore.ReadArray(data) |
| 255 | err = arr.UnmarshalBSONValue(t, raw) |
| 256 | *v = Array(arr) |
| 257 | case bsontype.Binary: |
| 258 | var subtype byte |
| 259 | var bindata []byte |
| 260 | subtype, bindata, rem, ok = bsoncore.ReadBinary(data) |
| 261 | *v = Binary(subtype, bindata) |
| 262 | case bsontype.Undefined: |
| 263 | *v = Undefined() |
| 264 | case bsontype.ObjectID: |
| 265 | var oid primitive.ObjectID |
| 266 | oid, rem, ok = bsoncore.ReadObjectID(data) |
| 267 | *v = ObjectID(oid) |
| 268 | case bsontype.Boolean: |
| 269 | var b bool |
| 270 | b, rem, ok = bsoncore.ReadBoolean(data) |
| 271 | *v = Boolean(b) |
| 272 | case bsontype.DateTime: |
| 273 | var dt int64 |
| 274 | dt, rem, ok = bsoncore.ReadDateTime(data) |
| 275 | *v = DateTime(dt) |
| 276 | case bsontype.Null: |
| 277 | *v = Null() |
| 278 | case bsontype.Regex: |
| 279 | var pattern, options string |
| 280 | pattern, options, rem, ok = bsoncore.ReadRegex(data) |
| 281 | *v = Regex(pattern, options) |
| 282 | case bsontype.DBPointer: |
| 283 | var ns string |
| 284 | var ptr primitive.ObjectID |
| 285 | ns, ptr, rem, ok = bsoncore.ReadDBPointer(data) |
| 286 | *v = DBPointer(ns, ptr) |
| 287 | case bsontype.JavaScript: |
| 288 | var js string |
| 289 | js, rem, ok = bsoncore.ReadJavaScript(data) |
| 290 | *v = JavaScript(js) |
| 291 | case bsontype.Symbol: |
| 292 | var symbol string |
| 293 | symbol, rem, ok = bsoncore.ReadSymbol(data) |
| 294 | *v = Symbol(symbol) |
| 295 | case bsontype.CodeWithScope: |
| 296 | var raw []byte |
| 297 | var code string |
| 298 | var scope Doc |
| 299 | code, raw, rem, ok = bsoncore.ReadCodeWithScope(data) |
| 300 | scope, err = ReadDoc(raw) |
| 301 | *v = CodeWithScope(code, scope) |
| 302 | case bsontype.Int32: |
| 303 | var i32 int32 |
| 304 | i32, rem, ok = bsoncore.ReadInt32(data) |
| 305 | *v = Int32(i32) |
| 306 | case bsontype.Timestamp: |
| 307 | var i, t uint32 |
| 308 | t, i, rem, ok = bsoncore.ReadTimestamp(data) |
| 309 | *v = Timestamp(t, i) |
| 310 | case bsontype.Int64: |
| 311 | var i64 int64 |
| 312 | i64, rem, ok = bsoncore.ReadInt64(data) |
| 313 | *v = Int64(i64) |
| 314 | case bsontype.Decimal128: |
| 315 | var d128 primitive.Decimal128 |
| 316 | d128, rem, ok = bsoncore.ReadDecimal128(data) |
| 317 | *v = Decimal128(d128) |
| 318 | case bsontype.MinKey: |
| 319 | *v = MinKey() |
| 320 | case bsontype.MaxKey: |
| 321 | *v = MaxKey() |
| 322 | default: |
| 323 | err = fmt.Errorf("invalid BSON type %v", t) |
| 324 | } |
| 325 | |
| 326 | if !ok && err == nil { |
| 327 | err = bsoncore.NewInsufficientBytesError(data, rem) |
| 328 | } |
| 329 | |
| 330 | return err |
| 331 | } |
| 332 | |
| 333 | // Type returns the BSON type of this value. |
| 334 | func (v Val) Type() bsontype.Type { |
| 335 | if v.t == bsontype.Type(0) { |
| 336 | return bsontype.Null |
| 337 | } |
| 338 | return v.t |
| 339 | } |
| 340 | |
| 341 | // IsNumber returns true if the type of v is a numberic BSON type. |
| 342 | func (v Val) IsNumber() bool { |
| 343 | switch v.Type() { |
| 344 | case bsontype.Double, bsontype.Int32, bsontype.Int64, bsontype.Decimal128: |
| 345 | return true |
| 346 | default: |
| 347 | return false |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Double returns the BSON double value the Value represents. It panics if the value is a BSON type |
| 352 | // other than double. |
| 353 | func (v Val) Double() float64 { |
| 354 | if v.t != bsontype.Double { |
| 355 | panic(ElementTypeError{"bson.Value.Double", v.t}) |
| 356 | } |
| 357 | return math.Float64frombits(binary.LittleEndian.Uint64(v.bootstrap[0:8])) |
| 358 | } |
| 359 | |
| 360 | // DoubleOK is the same as Double, but returns a boolean instead of panicking. |
| 361 | func (v Val) DoubleOK() (float64, bool) { |
| 362 | if v.t != bsontype.Double { |
| 363 | return 0, false |
| 364 | } |
| 365 | return math.Float64frombits(binary.LittleEndian.Uint64(v.bootstrap[0:8])), true |
| 366 | } |
| 367 | |
| 368 | // StringValue returns the BSON string the Value represents. It panics if the value is a BSON type |
| 369 | // other than string. |
| 370 | // |
| 371 | // NOTE: This method is called StringValue to avoid it implementing the |
| 372 | // fmt.Stringer interface. |
| 373 | func (v Val) StringValue() string { |
| 374 | if v.t != bsontype.String { |
| 375 | panic(ElementTypeError{"bson.Value.StringValue", v.t}) |
| 376 | } |
| 377 | return v.string() |
| 378 | } |
| 379 | |
| 380 | // StringValueOK is the same as StringValue, but returns a boolean instead of |
| 381 | // panicking. |
| 382 | func (v Val) StringValueOK() (string, bool) { |
| 383 | if v.t != bsontype.String { |
| 384 | return "", false |
| 385 | } |
| 386 | return v.string(), true |
| 387 | } |
| 388 | |
| 389 | func (v Val) asDoc() Doc { |
| 390 | doc, ok := v.primitive.(Doc) |
| 391 | if ok { |
| 392 | return doc |
| 393 | } |
| 394 | mdoc := v.primitive.(MDoc) |
| 395 | for k, v := range mdoc { |
| 396 | doc = append(doc, Elem{k, v}) |
| 397 | } |
| 398 | return doc |
| 399 | } |
| 400 | |
| 401 | func (v Val) asMDoc() MDoc { |
| 402 | mdoc, ok := v.primitive.(MDoc) |
| 403 | if ok { |
| 404 | return mdoc |
| 405 | } |
| 406 | doc := v.primitive.(Doc) |
| 407 | for _, elem := range doc { |
| 408 | mdoc[elem.Key] = elem.Value |
| 409 | } |
| 410 | return mdoc |
| 411 | } |
| 412 | |
| 413 | // Document returns the BSON embedded document value the Value represents. It panics if the value |
| 414 | // is a BSON type other than embedded document. |
| 415 | func (v Val) Document() Doc { |
| 416 | if v.t != bsontype.EmbeddedDocument { |
| 417 | panic(ElementTypeError{"bson.Value.Document", v.t}) |
| 418 | } |
| 419 | return v.asDoc() |
| 420 | } |
| 421 | |
| 422 | // DocumentOK is the same as Document, except it returns a boolean |
| 423 | // instead of panicking. |
| 424 | func (v Val) DocumentOK() (Doc, bool) { |
| 425 | if v.t != bsontype.EmbeddedDocument { |
| 426 | return nil, false |
| 427 | } |
| 428 | return v.asDoc(), true |
| 429 | } |
| 430 | |
| 431 | // MDocument returns the BSON embedded document value the Value represents. It panics if the value |
| 432 | // is a BSON type other than embedded document. |
| 433 | func (v Val) MDocument() MDoc { |
| 434 | if v.t != bsontype.EmbeddedDocument { |
| 435 | panic(ElementTypeError{"bson.Value.MDocument", v.t}) |
| 436 | } |
| 437 | return v.asMDoc() |
| 438 | } |
| 439 | |
| 440 | // MDocumentOK is the same as Document, except it returns a boolean |
| 441 | // instead of panicking. |
| 442 | func (v Val) MDocumentOK() (MDoc, bool) { |
| 443 | if v.t != bsontype.EmbeddedDocument { |
| 444 | return nil, false |
| 445 | } |
| 446 | return v.asMDoc(), true |
| 447 | } |
| 448 | |
| 449 | // Array returns the BSON array value the Value represents. It panics if the value is a BSON type |
| 450 | // other than array. |
| 451 | func (v Val) Array() Arr { |
| 452 | if v.t != bsontype.Array { |
| 453 | panic(ElementTypeError{"bson.Value.Array", v.t}) |
| 454 | } |
| 455 | return v.primitive.(Arr) |
| 456 | } |
| 457 | |
| 458 | // ArrayOK is the same as Array, except it returns a boolean |
| 459 | // instead of panicking. |
| 460 | func (v Val) ArrayOK() (Arr, bool) { |
| 461 | if v.t != bsontype.Array { |
| 462 | return nil, false |
| 463 | } |
| 464 | return v.primitive.(Arr), true |
| 465 | } |
| 466 | |
| 467 | // Binary returns the BSON binary value the Value represents. It panics if the value is a BSON type |
| 468 | // other than binary. |
| 469 | func (v Val) Binary() (byte, []byte) { |
| 470 | if v.t != bsontype.Binary { |
| 471 | panic(ElementTypeError{"bson.Value.Binary", v.t}) |
| 472 | } |
| 473 | bin := v.primitive.(primitive.Binary) |
| 474 | return bin.Subtype, bin.Data |
| 475 | } |
| 476 | |
| 477 | // BinaryOK is the same as Binary, except it returns a boolean instead of |
| 478 | // panicking. |
| 479 | func (v Val) BinaryOK() (byte, []byte, bool) { |
| 480 | if v.t != bsontype.Binary { |
| 481 | return 0x00, nil, false |
| 482 | } |
| 483 | bin := v.primitive.(primitive.Binary) |
| 484 | return bin.Subtype, bin.Data, true |
| 485 | } |
| 486 | |
| 487 | // Undefined returns the BSON undefined the Value represents. It panics if the value is a BSON type |
| 488 | // other than binary. |
| 489 | func (v Val) Undefined() { |
| 490 | if v.t != bsontype.Undefined { |
| 491 | panic(ElementTypeError{"bson.Value.Undefined", v.t}) |
| 492 | } |
| 493 | return |
| 494 | } |
| 495 | |
| 496 | // UndefinedOK is the same as Undefined, except it returns a boolean instead of |
| 497 | // panicking. |
| 498 | func (v Val) UndefinedOK() bool { |
| 499 | if v.t != bsontype.Undefined { |
| 500 | return false |
| 501 | } |
| 502 | return true |
| 503 | } |
| 504 | |
| 505 | // ObjectID returns the BSON ObjectID the Value represents. It panics if the value is a BSON type |
| 506 | // other than ObjectID. |
| 507 | func (v Val) ObjectID() primitive.ObjectID { |
| 508 | if v.t != bsontype.ObjectID { |
| 509 | panic(ElementTypeError{"bson.Value.ObjectID", v.t}) |
| 510 | } |
| 511 | var oid primitive.ObjectID |
| 512 | copy(oid[:], v.bootstrap[:12]) |
| 513 | return oid |
| 514 | } |
| 515 | |
| 516 | // ObjectIDOK is the same as ObjectID, except it returns a boolean instead of |
| 517 | // panicking. |
| 518 | func (v Val) ObjectIDOK() (primitive.ObjectID, bool) { |
| 519 | if v.t != bsontype.ObjectID { |
| 520 | return primitive.ObjectID{}, false |
| 521 | } |
| 522 | var oid primitive.ObjectID |
| 523 | copy(oid[:], v.bootstrap[:12]) |
| 524 | return oid, true |
| 525 | } |
| 526 | |
| 527 | // Boolean returns the BSON boolean the Value represents. It panics if the value is a BSON type |
| 528 | // other than boolean. |
| 529 | func (v Val) Boolean() bool { |
| 530 | if v.t != bsontype.Boolean { |
| 531 | panic(ElementTypeError{"bson.Value.Boolean", v.t}) |
| 532 | } |
| 533 | return v.bootstrap[0] == 0x01 |
| 534 | } |
| 535 | |
| 536 | // BooleanOK is the same as Boolean, except it returns a boolean instead of |
| 537 | // panicking. |
| 538 | func (v Val) BooleanOK() (bool, bool) { |
| 539 | if v.t != bsontype.Boolean { |
| 540 | return false, false |
| 541 | } |
| 542 | return v.bootstrap[0] == 0x01, true |
| 543 | } |
| 544 | |
| 545 | // DateTime returns the BSON datetime the Value represents. It panics if the value is a BSON type |
| 546 | // other than datetime. |
| 547 | func (v Val) DateTime() int64 { |
| 548 | if v.t != bsontype.DateTime { |
| 549 | panic(ElementTypeError{"bson.Value.DateTime", v.t}) |
| 550 | } |
| 551 | return v.i64() |
| 552 | } |
| 553 | |
| 554 | // DateTimeOK is the same as DateTime, except it returns a boolean instead of |
| 555 | // panicking. |
| 556 | func (v Val) DateTimeOK() (int64, bool) { |
| 557 | if v.t != bsontype.DateTime { |
| 558 | return 0, false |
| 559 | } |
| 560 | return v.i64(), true |
| 561 | } |
| 562 | |
| 563 | // Time returns the BSON datetime the Value represents as time.Time. It panics if the value is a BSON |
| 564 | // type other than datetime. |
| 565 | func (v Val) Time() time.Time { |
| 566 | if v.t != bsontype.DateTime { |
| 567 | panic(ElementTypeError{"bson.Value.Time", v.t}) |
| 568 | } |
| 569 | i := v.i64() |
| 570 | return time.Unix(int64(i)/1000, int64(i)%1000*1000000) |
| 571 | } |
| 572 | |
| 573 | // TimeOK is the same as Time, except it returns a boolean instead of |
| 574 | // panicking. |
| 575 | func (v Val) TimeOK() (time.Time, bool) { |
| 576 | if v.t != bsontype.DateTime { |
| 577 | return time.Time{}, false |
| 578 | } |
| 579 | i := v.i64() |
| 580 | return time.Unix(int64(i)/1000, int64(i)%1000*1000000), true |
| 581 | } |
| 582 | |
| 583 | // Null returns the BSON undefined the Value represents. It panics if the value is a BSON type |
| 584 | // other than binary. |
| 585 | func (v Val) Null() { |
| 586 | if v.t != bsontype.Null && v.t != bsontype.Type(0) { |
| 587 | panic(ElementTypeError{"bson.Value.Null", v.t}) |
| 588 | } |
| 589 | return |
| 590 | } |
| 591 | |
| 592 | // NullOK is the same as Null, except it returns a boolean instead of |
| 593 | // panicking. |
| 594 | func (v Val) NullOK() bool { |
| 595 | if v.t != bsontype.Null && v.t != bsontype.Type(0) { |
| 596 | return false |
| 597 | } |
| 598 | return true |
| 599 | } |
| 600 | |
| 601 | // Regex returns the BSON regex the Value represents. It panics if the value is a BSON type |
| 602 | // other than regex. |
| 603 | func (v Val) Regex() (pattern, options string) { |
| 604 | if v.t != bsontype.Regex { |
| 605 | panic(ElementTypeError{"bson.Value.Regex", v.t}) |
| 606 | } |
| 607 | regex := v.primitive.(primitive.Regex) |
| 608 | return regex.Pattern, regex.Options |
| 609 | } |
| 610 | |
| 611 | // RegexOK is the same as Regex, except that it returns a boolean |
| 612 | // instead of panicking. |
| 613 | func (v Val) RegexOK() (pattern, options string, ok bool) { |
| 614 | if v.t != bsontype.Regex { |
| 615 | return "", "", false |
| 616 | } |
| 617 | regex := v.primitive.(primitive.Regex) |
| 618 | return regex.Pattern, regex.Options, true |
| 619 | } |
| 620 | |
| 621 | // DBPointer returns the BSON dbpointer the Value represents. It panics if the value is a BSON type |
| 622 | // other than dbpointer. |
| 623 | func (v Val) DBPointer() (string, primitive.ObjectID) { |
| 624 | if v.t != bsontype.DBPointer { |
| 625 | panic(ElementTypeError{"bson.Value.DBPointer", v.t}) |
| 626 | } |
| 627 | dbptr := v.primitive.(primitive.DBPointer) |
| 628 | return dbptr.DB, dbptr.Pointer |
| 629 | } |
| 630 | |
| 631 | // DBPointerOK is the same as DBPoitner, except that it returns a boolean |
| 632 | // instead of panicking. |
| 633 | func (v Val) DBPointerOK() (string, primitive.ObjectID, bool) { |
| 634 | if v.t != bsontype.DBPointer { |
| 635 | return "", primitive.ObjectID{}, false |
| 636 | } |
| 637 | dbptr := v.primitive.(primitive.DBPointer) |
| 638 | return dbptr.DB, dbptr.Pointer, true |
| 639 | } |
| 640 | |
| 641 | // JavaScript returns the BSON JavaScript the Value represents. It panics if the value is a BSON type |
| 642 | // other than JavaScript. |
| 643 | func (v Val) JavaScript() string { |
| 644 | if v.t != bsontype.JavaScript { |
| 645 | panic(ElementTypeError{"bson.Value.JavaScript", v.t}) |
| 646 | } |
| 647 | return v.string() |
| 648 | } |
| 649 | |
| 650 | // JavaScriptOK is the same as Javascript, except that it returns a boolean |
| 651 | // instead of panicking. |
| 652 | func (v Val) JavaScriptOK() (string, bool) { |
| 653 | if v.t != bsontype.JavaScript { |
| 654 | return "", false |
| 655 | } |
| 656 | return v.string(), true |
| 657 | } |
| 658 | |
| 659 | // Symbol returns the BSON symbol the Value represents. It panics if the value is a BSON type |
| 660 | // other than symbol. |
| 661 | func (v Val) Symbol() string { |
| 662 | if v.t != bsontype.Symbol { |
| 663 | panic(ElementTypeError{"bson.Value.Symbol", v.t}) |
| 664 | } |
| 665 | return v.string() |
| 666 | } |
| 667 | |
| 668 | // SymbolOK is the same as Javascript, except that it returns a boolean |
| 669 | // instead of panicking. |
| 670 | func (v Val) SymbolOK() (string, bool) { |
| 671 | if v.t != bsontype.Symbol { |
| 672 | return "", false |
| 673 | } |
| 674 | return v.string(), true |
| 675 | } |
| 676 | |
| 677 | // CodeWithScope returns the BSON code with scope value the Value represents. It panics if the |
| 678 | // value is a BSON type other than code with scope. |
| 679 | func (v Val) CodeWithScope() (string, Doc) { |
| 680 | if v.t != bsontype.CodeWithScope { |
| 681 | panic(ElementTypeError{"bson.Value.CodeWithScope", v.t}) |
| 682 | } |
| 683 | cws := v.primitive.(primitive.CodeWithScope) |
| 684 | return string(cws.Code), cws.Scope.(Doc) |
| 685 | } |
| 686 | |
| 687 | // CodeWithScopeOK is the same as JavascriptWithScope, |
| 688 | // except that it returns a boolean instead of panicking. |
| 689 | func (v Val) CodeWithScopeOK() (string, Doc, bool) { |
| 690 | if v.t != bsontype.CodeWithScope { |
| 691 | return "", nil, false |
| 692 | } |
| 693 | cws := v.primitive.(primitive.CodeWithScope) |
| 694 | return string(cws.Code), cws.Scope.(Doc), true |
| 695 | } |
| 696 | |
| 697 | // Int32 returns the BSON int32 the Value represents. It panics if the value is a BSON type |
| 698 | // other than int32. |
| 699 | func (v Val) Int32() int32 { |
| 700 | if v.t != bsontype.Int32 { |
| 701 | panic(ElementTypeError{"bson.Value.Int32", v.t}) |
| 702 | } |
| 703 | return int32(v.bootstrap[0]) | int32(v.bootstrap[1])<<8 | |
| 704 | int32(v.bootstrap[2])<<16 | int32(v.bootstrap[3])<<24 |
| 705 | } |
| 706 | |
| 707 | // Int32OK is the same as Int32, except that it returns a boolean instead of |
| 708 | // panicking. |
| 709 | func (v Val) Int32OK() (int32, bool) { |
| 710 | if v.t != bsontype.Int32 { |
| 711 | return 0, false |
| 712 | } |
| 713 | return int32(v.bootstrap[0]) | int32(v.bootstrap[1])<<8 | |
| 714 | int32(v.bootstrap[2])<<16 | int32(v.bootstrap[3])<<24, |
| 715 | true |
| 716 | } |
| 717 | |
| 718 | // Timestamp returns the BSON timestamp the Value represents. It panics if the value is a |
| 719 | // BSON type other than timestamp. |
| 720 | func (v Val) Timestamp() (t, i uint32) { |
| 721 | if v.t != bsontype.Timestamp { |
| 722 | panic(ElementTypeError{"bson.Value.Timestamp", v.t}) |
| 723 | } |
| 724 | return uint32(v.bootstrap[4]) | uint32(v.bootstrap[5])<<8 | |
| 725 | uint32(v.bootstrap[6])<<16 | uint32(v.bootstrap[7])<<24, |
| 726 | uint32(v.bootstrap[0]) | uint32(v.bootstrap[1])<<8 | |
| 727 | uint32(v.bootstrap[2])<<16 | uint32(v.bootstrap[3])<<24 |
| 728 | } |
| 729 | |
| 730 | // TimestampOK is the same as Timestamp, except that it returns a boolean |
| 731 | // instead of panicking. |
| 732 | func (v Val) TimestampOK() (t uint32, i uint32, ok bool) { |
| 733 | if v.t != bsontype.Timestamp { |
| 734 | return 0, 0, false |
| 735 | } |
| 736 | return uint32(v.bootstrap[4]) | uint32(v.bootstrap[5])<<8 | |
| 737 | uint32(v.bootstrap[6])<<16 | uint32(v.bootstrap[7])<<24, |
| 738 | uint32(v.bootstrap[0]) | uint32(v.bootstrap[1])<<8 | |
| 739 | uint32(v.bootstrap[2])<<16 | uint32(v.bootstrap[3])<<24, |
| 740 | true |
| 741 | } |
| 742 | |
| 743 | // Int64 returns the BSON int64 the Value represents. It panics if the value is a BSON type |
| 744 | // other than int64. |
| 745 | func (v Val) Int64() int64 { |
| 746 | if v.t != bsontype.Int64 { |
| 747 | panic(ElementTypeError{"bson.Value.Int64", v.t}) |
| 748 | } |
| 749 | return v.i64() |
| 750 | } |
| 751 | |
| 752 | // Int64OK is the same as Int64, except that it returns a boolean instead of |
| 753 | // panicking. |
| 754 | func (v Val) Int64OK() (int64, bool) { |
| 755 | if v.t != bsontype.Int64 { |
| 756 | return 0, false |
| 757 | } |
| 758 | return v.i64(), true |
| 759 | } |
| 760 | |
| 761 | // Decimal128 returns the BSON decimal128 value the Value represents. It panics if the value is a |
| 762 | // BSON type other than decimal128. |
| 763 | func (v Val) Decimal128() primitive.Decimal128 { |
| 764 | if v.t != bsontype.Decimal128 { |
| 765 | panic(ElementTypeError{"bson.Value.Decimal128", v.t}) |
| 766 | } |
| 767 | return v.primitive.(primitive.Decimal128) |
| 768 | } |
| 769 | |
| 770 | // Decimal128OK is the same as Decimal128, except that it returns a boolean |
| 771 | // instead of panicking. |
| 772 | func (v Val) Decimal128OK() (primitive.Decimal128, bool) { |
| 773 | if v.t != bsontype.Decimal128 { |
| 774 | return primitive.Decimal128{}, false |
| 775 | } |
| 776 | return v.primitive.(primitive.Decimal128), true |
| 777 | } |
| 778 | |
| 779 | // MinKey returns the BSON minkey the Value represents. It panics if the value is a BSON type |
| 780 | // other than binary. |
| 781 | func (v Val) MinKey() { |
| 782 | if v.t != bsontype.MinKey { |
| 783 | panic(ElementTypeError{"bson.Value.MinKey", v.t}) |
| 784 | } |
| 785 | return |
| 786 | } |
| 787 | |
| 788 | // MinKeyOK is the same as MinKey, except it returns a boolean instead of |
| 789 | // panicking. |
| 790 | func (v Val) MinKeyOK() bool { |
| 791 | if v.t != bsontype.MinKey { |
| 792 | return false |
| 793 | } |
| 794 | return true |
| 795 | } |
| 796 | |
| 797 | // MaxKey returns the BSON maxkey the Value represents. It panics if the value is a BSON type |
| 798 | // other than binary. |
| 799 | func (v Val) MaxKey() { |
| 800 | if v.t != bsontype.MaxKey { |
| 801 | panic(ElementTypeError{"bson.Value.MaxKey", v.t}) |
| 802 | } |
| 803 | return |
| 804 | } |
| 805 | |
| 806 | // MaxKeyOK is the same as MaxKey, except it returns a boolean instead of |
| 807 | // panicking. |
| 808 | func (v Val) MaxKeyOK() bool { |
| 809 | if v.t != bsontype.MaxKey { |
| 810 | return false |
| 811 | } |
| 812 | return true |
| 813 | } |
| 814 | |
| 815 | // Equal compares v to v2 and returns true if they are equal. Unknown BSON types are |
| 816 | // never equal. Two empty values are equal. |
| 817 | func (v Val) Equal(v2 Val) bool { |
| 818 | if v.Type() != v2.Type() { |
| 819 | return false |
| 820 | } |
| 821 | if v.IsZero() && v2.IsZero() { |
| 822 | return true |
| 823 | } |
| 824 | |
| 825 | switch v.Type() { |
| 826 | case bsontype.Double, bsontype.DateTime, bsontype.Timestamp, bsontype.Int64: |
| 827 | return bytes.Equal(v.bootstrap[0:8], v2.bootstrap[0:8]) |
| 828 | case bsontype.String: |
| 829 | return v.string() == v2.string() |
| 830 | case bsontype.EmbeddedDocument: |
| 831 | return v.equalDocs(v2) |
| 832 | case bsontype.Array: |
| 833 | return v.Array().Equal(v2.Array()) |
| 834 | case bsontype.Binary: |
| 835 | return v.primitive.(primitive.Binary).Equal(v2.primitive.(primitive.Binary)) |
| 836 | case bsontype.Undefined: |
| 837 | return true |
| 838 | case bsontype.ObjectID: |
| 839 | return bytes.Equal(v.bootstrap[0:12], v2.bootstrap[0:12]) |
| 840 | case bsontype.Boolean: |
| 841 | return v.bootstrap[0] == v2.bootstrap[0] |
| 842 | case bsontype.Null: |
| 843 | return true |
| 844 | case bsontype.Regex: |
| 845 | return v.primitive.(primitive.Regex).Equal(v2.primitive.(primitive.Regex)) |
| 846 | case bsontype.DBPointer: |
| 847 | return v.primitive.(primitive.DBPointer).Equal(v2.primitive.(primitive.DBPointer)) |
| 848 | case bsontype.JavaScript: |
| 849 | return v.JavaScript() == v2.JavaScript() |
| 850 | case bsontype.Symbol: |
| 851 | return v.Symbol() == v2.Symbol() |
| 852 | case bsontype.CodeWithScope: |
| 853 | code1, scope1 := v.primitive.(primitive.CodeWithScope).Code, v.primitive.(primitive.CodeWithScope).Scope |
| 854 | code2, scope2 := v2.primitive.(primitive.CodeWithScope).Code, v2.primitive.(primitive.CodeWithScope).Scope |
| 855 | return code1 == code2 && v.equalInterfaceDocs(scope1, scope2) |
| 856 | case bsontype.Int32: |
| 857 | return v.Int32() == v2.Int32() |
| 858 | case bsontype.Decimal128: |
| 859 | h, l := v.Decimal128().GetBytes() |
| 860 | h2, l2 := v2.Decimal128().GetBytes() |
| 861 | return h == h2 && l == l2 |
| 862 | case bsontype.MinKey: |
| 863 | return true |
| 864 | case bsontype.MaxKey: |
| 865 | return true |
| 866 | default: |
| 867 | return false |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | func (v Val) equalDocs(v2 Val) bool { |
| 872 | _, ok1 := v.primitive.(MDoc) |
| 873 | _, ok2 := v2.primitive.(MDoc) |
| 874 | if ok1 || ok2 { |
| 875 | return v.asMDoc().Equal(v2.asMDoc()) |
| 876 | } |
| 877 | return v.asDoc().Equal(v2.asDoc()) |
| 878 | } |
| 879 | |
| 880 | func (Val) equalInterfaceDocs(i, i2 interface{}) bool { |
| 881 | switch d := i.(type) { |
| 882 | case MDoc: |
| 883 | d2, ok := i2.(IDoc) |
| 884 | if !ok { |
| 885 | return false |
| 886 | } |
| 887 | return d.Equal(d2) |
| 888 | case Doc: |
| 889 | d2, ok := i2.(IDoc) |
| 890 | if !ok { |
| 891 | return false |
| 892 | } |
| 893 | return d.Equal(d2) |
| 894 | case nil: |
| 895 | return i2 == nil |
| 896 | default: |
| 897 | return false |
| 898 | } |
| 899 | } |