raw.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Raw is a raw encoded BSON document. It can be used to delay BSON document decoding or precompute
  15. // a BSON encoded document.
  16. //
  17. // A Raw must be a full BSON document. Use the RawValue type for individual BSON values.
  18. type Raw []byte
  19. // ReadDocument reads a BSON document from the io.Reader and returns it as a bson.Raw. If the
  20. // reader contains multiple BSON documents, only the first document is read.
  21. func ReadDocument(r io.Reader) (Raw, error) {
  22. doc, err := bsoncore.NewDocumentFromReader(r)
  23. return Raw(doc), err
  24. }
  25. // NewFromIOReader reads a BSON document from the io.Reader and returns it as a bson.Raw. If the
  26. // reader contains multiple BSON documents, only the first document is read.
  27. //
  28. // Deprecated: Use ReadDocument instead.
  29. func NewFromIOReader(r io.Reader) (Raw, error) {
  30. return ReadDocument(r)
  31. }
  32. // Validate validates the document. This method only validates the first document in
  33. // the slice, to validate other documents, the slice must be resliced.
  34. func (r Raw) Validate() (err error) { return bsoncore.Document(r).Validate() }
  35. // Lookup search the document, potentially recursively, for the given key. If
  36. // there are multiple keys provided, this method will recurse down, as long as
  37. // the top and intermediate nodes are either documents or arrays.If an error
  38. // occurs or if the value doesn't exist, an empty RawValue is returned.
  39. func (r Raw) Lookup(key ...string) RawValue {
  40. return convertFromCoreValue(bsoncore.Document(r).Lookup(key...))
  41. }
  42. // LookupErr searches the document and potentially subdocuments or arrays for the
  43. // provided key. Each key provided to this method represents a layer of depth.
  44. func (r Raw) LookupErr(key ...string) (RawValue, error) {
  45. val, err := bsoncore.Document(r).LookupErr(key...)
  46. return convertFromCoreValue(val), err
  47. }
  48. // Elements returns this document as a slice of elements. The returned slice will contain valid
  49. // elements. If the document is not valid, the elements up to the invalid point will be returned
  50. // along with an error.
  51. func (r Raw) Elements() ([]RawElement, error) {
  52. elems, err := bsoncore.Document(r).Elements()
  53. relems := make([]RawElement, 0, len(elems))
  54. for _, elem := range elems {
  55. relems = append(relems, RawElement(elem))
  56. }
  57. return relems, err
  58. }
  59. // Values returns this document as a slice of values. The returned slice will contain valid values.
  60. // If the document is not valid, the values up to the invalid point will be returned along with an
  61. // error.
  62. func (r Raw) Values() ([]RawValue, error) {
  63. vals, err := bsoncore.Document(r).Values()
  64. rvals := make([]RawValue, 0, len(vals))
  65. for _, val := range vals {
  66. rvals = append(rvals, convertFromCoreValue(val))
  67. }
  68. return rvals, err
  69. }
  70. // Index searches for and retrieves the element at the given index. This method will panic if
  71. // the document is invalid or if the index is out of bounds.
  72. func (r Raw) Index(index uint) RawElement { return RawElement(bsoncore.Document(r).Index(index)) }
  73. // IndexErr searches for and retrieves the element at the given index.
  74. func (r Raw) IndexErr(index uint) (RawElement, error) {
  75. elem, err := bsoncore.Document(r).IndexErr(index)
  76. return RawElement(elem), err
  77. }
  78. // String returns the BSON document encoded as Extended JSON.
  79. func (r Raw) String() string { return bsoncore.Document(r).String() }