You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

raw.go 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. package bson
  7. import (
  8. "errors"
  9. "io"
  10. "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
  11. )
  12. // ErrNilReader indicates that an operation was attempted on a nil bson.Reader.
  13. var ErrNilReader = errors.New("nil reader")
  14. var errValidateDone = errors.New("validation loop complete")
  15. // Raw is a wrapper around a byte slice. It will interpret the slice as a
  16. // BSON document. This type is a wrapper around a bsoncore.Document. Errors returned from the
  17. // methods on this type and associated types come from the bsoncore package.
  18. type Raw []byte
  19. // NewFromIOReader reads in a document from the given io.Reader and constructs a Raw from
  20. // it.
  21. func NewFromIOReader(r io.Reader) (Raw, error) {
  22. doc, err := bsoncore.NewDocumentFromReader(r)
  23. return Raw(doc), err
  24. }
  25. // Validate validates the document. This method only validates the first document in
  26. // the slice, to validate other documents, the slice must be resliced.
  27. func (r Raw) Validate() (err error) { return bsoncore.Document(r).Validate() }
  28. // Lookup search the document, potentially recursively, for the given key. If
  29. // there are multiple keys provided, this method will recurse down, as long as
  30. // the top and intermediate nodes are either documents or arrays.If an error
  31. // occurs or if the value doesn't exist, an empty RawValue is returned.
  32. func (r Raw) Lookup(key ...string) RawValue {
  33. return convertFromCoreValue(bsoncore.Document(r).Lookup(key...))
  34. }
  35. // LookupErr searches the document and potentially subdocuments or arrays for the
  36. // provided key. Each key provided to this method represents a layer of depth.
  37. func (r Raw) LookupErr(key ...string) (RawValue, error) {
  38. val, err := bsoncore.Document(r).LookupErr(key...)
  39. return convertFromCoreValue(val), err
  40. }
  41. // Elements returns this document as a slice of elements. The returned slice will contain valid
  42. // elements. If the document is not valid, the elements up to the invalid point will be returned
  43. // along with an error.
  44. func (r Raw) Elements() ([]RawElement, error) {
  45. elems, err := bsoncore.Document(r).Elements()
  46. relems := make([]RawElement, 0, len(elems))
  47. for _, elem := range elems {
  48. relems = append(relems, RawElement(elem))
  49. }
  50. return relems, err
  51. }
  52. // Values returns this document as a slice of values. The returned slice will contain valid values.
  53. // If the document is not valid, the values up to the invalid point will be returned along with an
  54. // error.
  55. func (r Raw) Values() ([]RawValue, error) {
  56. vals, err := bsoncore.Document(r).Values()
  57. rvals := make([]RawValue, 0, len(vals))
  58. for _, val := range vals {
  59. rvals = append(rvals, convertFromCoreValue(val))
  60. }
  61. return rvals, err
  62. }
  63. // Index searches for and retrieves the element at the given index. This method will panic if
  64. // the document is invalid or if the index is out of bounds.
  65. func (r Raw) Index(index uint) RawElement { return RawElement(bsoncore.Document(r).Index(index)) }
  66. // IndexErr searches for and retrieves the element at the given index.
  67. func (r Raw) IndexErr(index uint) (RawElement, error) {
  68. elem, err := bsoncore.Document(r).IndexErr(index)
  69. return RawElement(elem), err
  70. }
  71. // String implements the fmt.Stringer interface.
  72. func (r Raw) String() string { return bsoncore.Document(r).String() }
  73. // readi32 is a helper function for reading an int32 from slice of bytes.
  74. func readi32(b []byte) int32 {
  75. _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
  76. return int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
  77. }