Skip to content

Commit 712828f

Browse files
AlekSiCopilot
andauthored
Remove deprecated method (#146)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2026334 commit 712828f

7 files changed

Lines changed: 63 additions & 54 deletions

File tree

internal/util/must/must.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
// Package must provides helper functions that panic on error.
1616
package must
1717

18-
import (
19-
"fmt"
20-
)
21-
2218
// NoError panics if the error is not nil.
2319
//
2420
// Use that function only for static initialization, test code, or code that "can't" fail.
@@ -28,14 +24,3 @@ func NoError(err error) {
2824
panic(err)
2925
}
3026
}
31-
32-
// NotBeZero panics if argument has zero value.
33-
//
34-
// Use that function only for static initialization, test code, or code that "can't" fail.
35-
// When in doubt, don't.
36-
func NotBeZero[T comparable](v T) {
37-
var zero T
38-
if v == zero {
39-
panic(fmt.Sprintf("v has zero value (%#v)", v))
40-
}
41-
}

op_msg.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import (
2323
"github.com/FerretDB/wire/wirebson"
2424
)
2525

26-
// OpMsg is the main wire protocol message type.
26+
// OpMsg represents the OP_MSG wire protocol message type.
27+
// It stores BSON documents in the raw form.
2728
//
28-
// Message is checked during construction by [NewOpMsg], [MustOpMsg], or [OpMsg.UnmarshalBinaryNocopy].
29+
// Message is checked during construction by [NewOpMsg], [MustOpMsg], or [OpMsg.UnmarshalBinaryNocopy]
30+
// without decoding BSON documents inside.
2931
type OpMsg struct {
3032
// The order of fields is weird to make the struct smaller due to alignment.
3133
// The wire order is: flags, sections, optional checksum.
@@ -76,10 +78,6 @@ func (msg *OpMsg) msgbody() {}
7678

7779
// check implements [MsgBody].
7880
func (msg *OpMsg) check() error {
79-
if err := checkSections(msg.sections); err != nil {
80-
return lazyerrors.Error(err)
81-
}
82-
8381
for _, s := range msg.sections {
8482
for _, d := range s.documents {
8583
if _, err := d.DecodeDeep(); err != nil {

op_query.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"github.com/FerretDB/wire/wirebson"
2323
)
2424

25-
// OpQuery is a deprecated request message type.
25+
// OpQuery represents the deprecated OP_QUERY wire protocol message type.
26+
// It stores BSON documents in the raw form.
2627
//
27-
// Message is checked during construction by [NewOpQuery], [MustOpQuery], or [OpQuery.UnmarshalBinaryNocopy].
28+
// Message is checked during construction by [NewOpQuery], [MustOpQuery], or [OpQuery.UnmarshalBinaryNocopy]
29+
// without decoding BSON documents inside.
2830
type OpQuery struct {
2931
// The order of fields is weird to make the struct smaller due to alignment.
3032
// The wire order is: flags, collection name, number to skip, number to return, query, fields selector.

op_reply.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
"github.com/FerretDB/wire/wirebson"
2323
)
2424

25-
// OpReply is a deprecated response message type.
26-
//
27-
// Message is checked during construction by [NewOpReply], [MustOpReply], or [OpReply.UnmarshalBinaryNocopy].
28-
//
25+
// OpReply represents the deprecated OP_REPLY wire protocol message type.
26+
// It stores BSON documents in the raw form.
2927
// Only up to one returned document is supported.
28+
//
29+
// Message is checked during construction by [NewOpReply], [MustOpReply], or [OpReply.UnmarshalBinaryNocopy]
30+
// without decoding BSON documents inside.
3031
type OpReply struct {
3132
// The order of fields is weird to make the struct smaller due to alignment.
3233
// The wire order is: flags, cursor ID, starting from, documents.

wire_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func testMessages(t *testing.T, testCases []testCase) {
136136

137137
switch msgBody := msgBody.(type) {
138138
case *OpMsg:
139+
require.NoError(t, checkSections(msgBody.sections))
140+
139141
_, _ = msgBody.Document()
140142
_, _ = msgBody.DocumentDeep()
141143
_, _ = msgBody.DocumentRaw()
@@ -231,6 +233,8 @@ func fuzzMessages(f *testing.F, testCases []testCase) {
231233

232234
switch msgBody := msgBody.(type) {
233235
case *OpMsg:
236+
require.NoError(t, checkSections(msgBody.sections))
237+
234238
_, _ = msgBody.Document()
235239
_, _ = msgBody.DocumentDeep()
236240
_, _ = msgBody.DocumentRaw()

wirebson/array.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"go.mongodb.org/mongo-driver/v2/bson"
2828

2929
"github.com/FerretDB/wire/internal/util/lazyerrors"
30-
"github.com/FerretDB/wire/internal/util/must"
3130
)
3231

3332
// Array represents a BSON array in the (partially) decoded form.
@@ -161,7 +160,9 @@ func (arr *Array) SortInterface(less func(a, b any) bool) sort.Interface {
161160
// This method should accept a slice of bytes, not return it.
162161
// That would allow to avoid unnecessary allocations.
163162
func (arr *Array) Encode() (RawArray, error) {
164-
must.NotBeZero(arr)
163+
if arr == nil {
164+
panic("arr is nil")
165+
}
165166

166167
size := sizeArray(arr)
167168
buf := bytes.NewBuffer(make([]byte, 0, size))
@@ -187,7 +188,9 @@ func (arr *Array) Encode() (RawArray, error) {
187188
// by encoding Canonical Extended JSON v2 representation of the array.
188189
func (arr *Array) MarshalJSON() ([]byte, error) {
189190
// encoding/json does not call this method on nil
190-
must.NotBeZero(arr)
191+
if arr == nil {
192+
panic("arr is nil")
193+
}
191194

192195
a, err := ToDriver(arr)
193196
if err != nil {
@@ -206,15 +209,20 @@ func (arr *Array) MarshalJSON() ([]byte, error) {
206209
//
207210
// Receiver must not be nil.
208211
func (arr *Array) Decode() (*Array, error) {
209-
must.NotBeZero(arr)
212+
if arr == nil {
213+
panic("arr is nil")
214+
}
215+
210216
return arr, nil
211217
}
212218

213219
// UnmarshalJSON implements [json.Unmarshaler]
214220
// by decoding Canonical Extended JSON v2 representation of the array.
215221
func (arr *Array) UnmarshalJSON(b []byte) error {
216222
// encoding/json does not call this method on nil
217-
must.NotBeZero(arr)
223+
if arr == nil {
224+
panic("arr is nil")
225+
}
218226

219227
var a bson.A
220228
if err := bson.UnmarshalExtJSON(b, true, &a); err != nil {
@@ -228,7 +236,6 @@ func (arr *Array) UnmarshalJSON(b []byte) error {
228236

229237
switch v := v.(type) {
230238
case *Array:
231-
must.NotBeZero(v)
232239
*arr = *v
233240
return nil
234241
default:
@@ -244,10 +251,17 @@ func (arr *Array) Copy() *Array {
244251
for v := range arr.Values() {
245252
switch v := v.(type) {
246253
case Binary:
247-
must.NoError(res.Add(Binary{B: slices.Clip(slices.Clone(v.B)), Subtype: v.Subtype}))
254+
if err := res.Add(Binary{B: slices.Clip(slices.Clone(v.B)), Subtype: v.Subtype}); err != nil {
255+
panic(err)
256+
}
257+
248258
default:
249-
must.NoError(validBSONType(v))
250-
must.NoError(res.Add(v))
259+
if err := validBSONType(v); err != nil {
260+
panic(err)
261+
}
262+
if err := res.Add(v); err != nil {
263+
panic(err)
264+
}
251265
}
252266
}
253267

wirebson/document.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"go.mongodb.org/mongo-driver/v2/bson"
2626

2727
"github.com/FerretDB/wire/internal/util/lazyerrors"
28-
"github.com/FerretDB/wire/internal/util/must"
2928
)
3029

3130
// field represents a single Document field in the (partially) decoded form.
@@ -136,15 +135,6 @@ func (doc *Document) Get(name string) any {
136135
return nil
137136
}
138137

139-
// GetByIndex returns the name and the value of the field at the given index (between 0 and [Document.Len]-1).
140-
// It panics if index is out of bounds.
141-
//
142-
// Deprecated: use [Document.All] instead.
143-
func (doc *Document) GetByIndex(i int) (string, any) {
144-
f := doc.fields[i]
145-
return f.name, f.value
146-
}
147-
148138
// All returns an iterator over all field name/value pairs of the Document.
149139
func (doc *Document) All() iter.Seq2[string, any] {
150140
return func(yield func(string, any) bool) {
@@ -239,7 +229,9 @@ func (doc *Document) Command() string {
239229
// This method should accept a slice of bytes, not return it.
240230
// That would allow to avoid unnecessary allocations.
241231
func (doc *Document) Encode() (RawDocument, error) {
242-
must.NotBeZero(doc)
232+
if doc == nil {
233+
panic("doc is nil")
234+
}
243235

244236
size := sizeDocument(doc)
245237
buf := bytes.NewBuffer(make([]byte, 0, size))
@@ -265,7 +257,9 @@ func (doc *Document) Encode() (RawDocument, error) {
265257
// by encoding Canonical Extended JSON v2 representation of the document.
266258
func (doc *Document) MarshalJSON() ([]byte, error) {
267259
// encoding/json does not call this method on nil
268-
must.NotBeZero(doc)
260+
if doc == nil {
261+
panic("doc is nil")
262+
}
269263

270264
d, err := ToDriver(doc)
271265
if err != nil {
@@ -284,15 +278,20 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
284278
//
285279
// Receiver must not be nil.
286280
func (doc *Document) Decode() (*Document, error) {
287-
must.NotBeZero(doc)
281+
if doc == nil {
282+
panic("doc is nil")
283+
}
284+
288285
return doc, nil
289286
}
290287

291288
// UnmarshalJSON implements [json.Unmarshaler]
292289
// by decoding Canonical Extended JSON v2 representation of the document.
293290
func (doc *Document) UnmarshalJSON(b []byte) error {
294291
// encoding/json does not call this method on nil
295-
must.NotBeZero(doc)
292+
if doc == nil {
293+
panic("doc is nil")
294+
}
296295

297296
var d bson.D
298297
if err := bson.UnmarshalExtJSON(b, true, &d); err != nil {
@@ -306,7 +305,6 @@ func (doc *Document) UnmarshalJSON(b []byte) error {
306305

307306
switch v := v.(type) {
308307
case *Document:
309-
must.NotBeZero(v)
310308
*doc = *v
311309
return nil
312310
default:
@@ -322,10 +320,17 @@ func (doc *Document) Copy() *Document {
322320
for k, v := range doc.All() {
323321
switch v := v.(type) {
324322
case Binary:
325-
must.NoError(res.Add(k, Binary{B: slices.Clip(slices.Clone(v.B)), Subtype: v.Subtype}))
323+
if err := res.Add(k, Binary{B: slices.Clip(slices.Clone(v.B)), Subtype: v.Subtype}); err != nil {
324+
panic(err)
325+
}
326+
326327
default:
327-
must.NoError(validBSONType(v))
328-
must.NoError(res.Add(k, v))
328+
if err := validBSONType(v); err != nil {
329+
panic(err)
330+
}
331+
if err := res.Add(k, v); err != nil {
332+
panic(err)
333+
}
329334
}
330335
}
331336

0 commit comments

Comments
 (0)