@@ -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.
149139func (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.
241231func (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.
266258func (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.
286280func (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.
293290func (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