/
array.go
150 lines (121 loc) · 3.4 KB
/
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
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
// 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 bson
import (
"bytes"
"encoding/binary"
"errors"
"log/slog"
"strconv"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
// Array represents a BSON array in the (partially) decoded form.
type Array struct {
elements []any
}
// NewArray creates a new Array from the given values.
func NewArray(values ...any) (*Array, error) {
res := &Array{
elements: make([]any, 0, len(values)),
}
for i, v := range values {
if err := res.Add(v); err != nil {
return nil, lazyerrors.Errorf("%d: %w", i, err)
}
}
return res, nil
}
// MakeArray creates a new empty Array with the given capacity.
func MakeArray(cap int) *Array {
return &Array{
elements: make([]any, 0, cap),
}
}
// ConvertArray converts [*types.Array] to Array.
func ConvertArray(arr *types.Array) (*Array, error) {
iter := arr.Iterator()
defer iter.Close()
elements := make([]any, arr.Len())
for {
i, v, err := iter.Next()
if err != nil {
if errors.Is(err, iterator.ErrIteratorDone) {
return &Array{
elements: elements,
}, nil
}
return nil, lazyerrors.Error(err)
}
v, err = convertFromTypes(v)
if err != nil {
return nil, lazyerrors.Error(err)
}
elements[i] = v
}
}
// Convert converts Array to [*types.Array], decoding raw documents and arrays on the fly.
func (arr *Array) Convert() (*types.Array, error) {
values := make([]any, len(arr.elements))
for i, f := range arr.elements {
v, err := convertToTypes(f)
if err != nil {
return nil, lazyerrors.Error(err)
}
values[i] = v
}
res, err := types.NewArray(values...)
if err != nil {
return nil, lazyerrors.Error(err)
}
return res, nil
}
// Add adds a new element to the Array.
func (arr *Array) Add(value any) error {
if err := validBSONType(value); err != nil {
return lazyerrors.Error(err)
}
arr.elements = append(arr.elements, value)
return nil
}
// Encode encodes BSON array.
//
// TODO https://github.com/FerretDB/FerretDB/issues/3759
// This method should accept a slice of bytes, not return it.
// That would allow to avoid unnecessary allocations.
func (arr *Array) Encode() (RawArray, error) {
size := sizeAny(arr)
buf := bytes.NewBuffer(make([]byte, 0, size))
if err := binary.Write(buf, binary.LittleEndian, uint32(size)); err != nil {
return nil, lazyerrors.Error(err)
}
for i, v := range arr.elements {
if err := encodeField(buf, strconv.Itoa(i), v); err != nil {
return nil, lazyerrors.Error(err)
}
}
if err := binary.Write(buf, binary.LittleEndian, byte(0)); err != nil {
return nil, lazyerrors.Error(err)
}
return buf.Bytes(), nil
}
// LogValue implements slog.LogValuer interface.
func (arr *Array) LogValue() slog.Value {
return slogValue(arr, 1)
}
// check interfaces
var (
_ slog.LogValuer = (*Array)(nil)
)