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