podman

Форк
0
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

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 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.
22
type Raw []byte
23

24
// NewFromIOReader reads in a document from the given io.Reader and constructs a Raw from
25
// it.
26
func NewFromIOReader(r io.Reader) (Raw, error) {
27
	doc, err := bsoncore.NewDocumentFromReader(r)
28
	return 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.
33
func (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.
39
func (r Raw) Lookup(key ...string) RawValue {
40
	return 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.
45
func (r Raw) LookupErr(key ...string) (RawValue, error) {
46
	val, err := bsoncore.Document(r).LookupErr(key...)
47
	return 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.
53
func (r Raw) Elements() ([]RawElement, error) {
54
	elems, err := bsoncore.Document(r).Elements()
55
	relems := make([]RawElement, 0, len(elems))
56
	for _, elem := range elems {
57
		relems = append(relems, RawElement(elem))
58
	}
59
	return 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.
65
func (r Raw) Values() ([]RawValue, error) {
66
	vals, err := bsoncore.Document(r).Values()
67
	rvals := make([]RawValue, 0, len(vals))
68
	for _, val := range vals {
69
		rvals = append(rvals, convertFromCoreValue(val))
70
	}
71
	return 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.
76
func (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.
79
func (r Raw) IndexErr(index uint) (RawElement, error) {
80
	elem, err := bsoncore.Document(r).IndexErr(index)
81
	return RawElement(elem), err
82
}
83

84
// String implements the fmt.Stringer interface.
85
func (r Raw) String() string { return bsoncore.Document(r).String() }
86

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.