forked from FerretDB/FerretDB
/
raw_array.go
112 lines (92 loc) · 2.92 KB
/
raw_array.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bson2
import (
"log/slog"
"strconv"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
// RawArray represents a single BSON array in the binary encoded form.
//
// It generally references a part of a larger slice, not a copy.
type RawArray []byte
// Decode decodes a single BSON array that takes the whole byte slice.
//
// Only top-level elements are decoded;
// nested documents and arrays are converted to RawDocument and RawArray respectively,
// using raw's subslices without copying.
func (raw RawArray) Decode() (*Array, error) {
res, err := raw.decode(decodeShallow)
if err != nil {
return nil, lazyerrors.Error(err)
}
return res, nil
}
// DecodeDeep decodes a single valid BSON array that takes the whole byte slice.
//
// All nested documents and arrays are decoded recursively.
func (raw RawArray) DecodeDeep() (*Array, error) {
res, err := raw.decode(decodeDeep)
if err != nil {
return nil, lazyerrors.Error(err)
}
return res, nil
}
// Convert converts a single valid BSON array that takes the whole byte slice into [*types.Array].
func (raw RawArray) Convert() (*types.Array, error) {
arr, err := raw.decode(decodeShallow)
if err != nil {
return nil, lazyerrors.Error(err)
}
res, err := arr.Convert()
if err != nil {
return nil, lazyerrors.Error(err)
}
return res, nil
}
// decode decodes a single BSON array that takes the whole byte slice.
func (raw RawArray) decode(mode decodeMode) (*Array, error) {
doc, err := RawDocument(raw).decode(mode)
if err != nil {
return nil, lazyerrors.Error(err)
}
res := &Array{
elements: make([]any, len(doc.fields)),
}
for i, f := range doc.fields {
if f.name != strconv.Itoa(i) {
return nil, lazyerrors.Errorf("invalid array index: %q", f.name)
}
res.elements[i] = f.value
}
return res, nil
}
// LogValue implements slog.LogValuer interface.
func (raw RawArray) LogValue() slog.Value {
return slogValue(raw, 1)
}
// LogMessage returns a representation as a string.
// It may change over time.
func (raw RawArray) LogMessage() string {
return logMessage(raw, logMaxFlowLength, "", 1)
}
// LogMessageBlock is a variant of [RawArray.LogMessage] that never uses a flow style.
func (raw RawArray) LogMessageBlock() string {
return logMessage(raw, 0, "", 1)
}
// check interfaces
var (
_ slog.LogValuer = RawArray(nil)
)