podman
85 строк · 3.3 Кб
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
7package bson
8
9import (
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.
17var ErrNilReader = errors.New("nil reader")
18
19// Raw is a wrapper around a byte slice. It will interpret the slice as a
20// BSON document. This type is a wrapper around a bsoncore.Document. Errors returned from the
21// methods on this type and associated types come from the bsoncore package.
22type Raw []byte
23
24// NewFromIOReader reads in a document from the given io.Reader and constructs a Raw from
25// it.
26func NewFromIOReader(r io.Reader) (Raw, error) {
27doc, err := bsoncore.NewDocumentFromReader(r)
28return Raw(doc), err
29}
30
31// Validate validates the document. This method only validates the first document in
32// the slice, to validate other documents, the slice must be resliced.
33func (r Raw) Validate() (err error) { return bsoncore.Document(r).Validate() }
34
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.
39func (r Raw) Lookup(key ...string) RawValue {
40return convertFromCoreValue(bsoncore.Document(r).Lookup(key...))
41}
42
43// LookupErr searches the document and potentially subdocuments or arrays for the
44// provided key. Each key provided to this method represents a layer of depth.
45func (r Raw) LookupErr(key ...string) (RawValue, error) {
46val, err := bsoncore.Document(r).LookupErr(key...)
47return convertFromCoreValue(val), err
48}
49
50// Elements returns this document as a slice of elements. The returned slice will contain valid
51// elements. If the document is not valid, the elements up to the invalid point will be returned
52// along with an error.
53func (r Raw) Elements() ([]RawElement, error) {
54elems, err := bsoncore.Document(r).Elements()
55relems := make([]RawElement, 0, len(elems))
56for _, elem := range elems {
57relems = append(relems, RawElement(elem))
58}
59return relems, err
60}
61
62// Values returns this document as a slice of values. The returned slice will contain valid values.
63// If the document is not valid, the values up to the invalid point will be returned along with an
64// error.
65func (r Raw) Values() ([]RawValue, error) {
66vals, err := bsoncore.Document(r).Values()
67rvals := make([]RawValue, 0, len(vals))
68for _, val := range vals {
69rvals = append(rvals, convertFromCoreValue(val))
70}
71return rvals, err
72}
73
74// Index searches for and retrieves the element at the given index. This method will panic if
75// the document is invalid or if the index is out of bounds.
76func (r Raw) Index(index uint) RawElement { return RawElement(bsoncore.Document(r).Index(index)) }
77
78// IndexErr searches for and retrieves the element at the given index.
79func (r Raw) IndexErr(index uint) (RawElement, error) {
80elem, err := bsoncore.Document(r).IndexErr(index)
81return RawElement(elem), err
82}
83
84// String implements the fmt.Stringer interface.
85func (r Raw) String() string { return bsoncore.Document(r).String() }
86