forked from FerretDB/FerretDB
/
raw_document.go
183 lines (146 loc) · 4.56 KB
/
raw_document.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
)
// RawDocument represents a singe BSON document a.k.a object in the binary encoded form.
//
// It generally references a part of a larger slice, not a copy.
type RawDocument []byte
// Decode decodes a single BSON document that takes the whole byte slice.
//
// Only top-level fields are decoded;
// nested documents and arrays are converted to RawDocument and RawArray respectively,
// using raw's subslices without copying.
func (raw RawDocument) Decode() (*Document, error) {
res, err := raw.decode(decodeShallow)
if err != nil {
return nil, lazyerrors.Error(err)
}
return res, nil
}
// DecodeDeep decodes a single valid BSON document that takes the whole byte slice.
//
// All nested documents and arrays are decoded recursively.
func (raw RawDocument) DecodeDeep() (*Document, error) {
res, err := raw.decode(decodeDeep)
if err != nil {
return nil, lazyerrors.Error(err)
}
return res, nil
}
// Convert converts a single valid BSON document that takes the whole byte slice into [*types.Document].
func (raw RawDocument) Convert() (*types.Document, error) {
doc, err := raw.decode(decodeShallow)
if err != nil {
return nil, lazyerrors.Error(err)
}
res, err := doc.Convert()
if err != nil {
return nil, lazyerrors.Error(err)
}
return res, nil
}
// decode decodes a single BSON document that takes the whole byte slice.
func (raw RawDocument) decode(mode decodeMode) (*Document, error) {
l, err := FindRaw(raw)
if err != nil {
return nil, lazyerrors.Error(err)
}
if rl := len(raw); rl != l {
return nil, lazyerrors.Errorf("len(raw) = %d, l = %d: %w", rl, l, ErrDecodeInvalidInput)
}
res := MakeDocument(0)
offset := 4
for {
if err := decodeCheckOffset(raw, offset, 1); err != nil {
return nil, lazyerrors.Error(err)
}
t := tag(raw[offset])
if t == 0 {
if rl := len(raw); rl != offset+1 {
return nil, lazyerrors.Errorf("len(raw) = %d, offset = %d, got %s: %w", rl, offset, t, ErrDecodeInvalidInput)
}
return res, nil
}
offset++
if err := decodeCheckOffset(raw, offset, 1); err != nil {
return nil, lazyerrors.Error(err)
}
name, err := DecodeCString(raw[offset:])
if err != nil {
return nil, lazyerrors.Error(err)
}
offset += SizeCString(name)
var v any
// to check if we can even `raw[offset:]` below
if err = decodeCheckOffset(raw, offset, 0); err != nil {
return nil, lazyerrors.Error(err)
}
switch t { //nolint:exhaustive // other tags are handled by decodeScalarField
case tagDocument:
if l, err = FindRaw(raw[offset:]); err != nil {
return nil, lazyerrors.Errorf("no document at offset = %d: %w", offset, err)
}
rawDoc := RawDocument(raw[offset : offset+l])
offset += l
switch mode {
case decodeShallow:
v = rawDoc
case decodeDeep:
v, err = rawDoc.decode(decodeDeep)
}
case tagArray:
if l, err = FindRaw(raw[offset:]); err != nil {
return nil, lazyerrors.Errorf("no array at offset = %d: %w", offset, err)
}
rawArr := RawArray(raw[offset : offset+l])
offset += l
switch mode {
case decodeShallow:
v = rawArr
case decodeDeep:
v, err = rawArr.decode(decodeDeep)
}
default:
v, l, err = decodeScalarField(raw[offset:], t)
offset += l
}
if err != nil {
return nil, lazyerrors.Error(err)
}
must.NoError(res.Add(name, v))
}
}
// LogValue implements slog.LogValuer interface.
func (raw RawDocument) LogValue() slog.Value {
return slogValue(raw, 1)
}
// LogMessage returns a representation as a string.
// It may change over time.
func (raw RawDocument) LogMessage() string {
return logMessage(raw, logMaxFlowLength, "", 1)
}
// LogMessageBlock is a variant of [RawDocument.LogMessage] that never uses a flow style.
func (raw RawDocument) LogMessageBlock() string {
return logMessage(raw, 0, "", 1)
}
// check interfaces
var (
_ slog.LogValuer = RawDocument(nil)
)