1// Copyright (C) MongoDB, Inc. 2017-present. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may 4// not use this file except in compliance with the License. You may obtain 5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7package bson 8 9import ( 10 "errors" 11 "fmt" 12 "reflect" 13 "sync" 14 15 "go.mongodb.org/mongo-driver/bson/bsoncodec" 16 "go.mongodb.org/mongo-driver/bson/bsonrw" 17) 18 19// ErrDecodeToNil is the error returned when trying to decode to a nil value 20var ErrDecodeToNil = errors.New("cannot Decode to nil value") 21 22// This pool is used to keep the allocations of Decoders down. This is only used for the Marshal* 23// methods and is not consumable from outside of this package. The Decoders retrieved from this pool 24// must have both Reset and SetRegistry called on them. 25var decPool = sync.Pool{ 26 New: func() interface{} { 27 return new(Decoder) 28 }, 29} 30 31// A Decoder reads and decodes BSON documents from a stream. It reads from a bsonrw.ValueReader as 32// the source of BSON data. 33type Decoder struct { 34 dc bsoncodec.DecodeContext 35 vr bsonrw.ValueReader 36} 37 38// NewDecoder returns a new decoder that uses the DefaultRegistry to read from vr. 39func NewDecoder(vr bsonrw.ValueReader) (*Decoder, error) { 40 if vr == nil { 41 return nil, errors.New("cannot create a new Decoder with a nil ValueReader") 42 } 43 44 return &Decoder{ 45 dc: bsoncodec.DecodeContext{Registry: DefaultRegistry}, 46 vr: vr, 47 }, nil 48} 49 50// NewDecoderWithContext returns a new decoder that uses DecodeContext dc to read from vr. 51func NewDecoderWithContext(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader) (*Decoder, error) { 52 if dc.Registry == nil { 53 dc.Registry = DefaultRegistry 54 } 55 if vr == nil { 56 return nil, errors.New("cannot create a new Decoder with a nil ValueReader") 57 } 58 59 return &Decoder{ 60 dc: dc, 61 vr: vr, 62 }, nil 63} 64 65// Decode reads the next BSON document from the stream and decodes it into the 66// value pointed to by val. 67// 68// The documentation for Unmarshal contains details about of BSON into a Go 69// value. 70func (d *Decoder) Decode(val interface{}) error { 71 if unmarshaler, ok := val.(Unmarshaler); ok { 72 // TODO(skriptble): Reuse a []byte here and use the AppendDocumentBytes method. 73 buf, err := bsonrw.Copier{}.CopyDocumentToBytes(d.vr) 74 if err != nil { 75 return err 76 } 77 return unmarshaler.UnmarshalBSON(buf) 78 } 79 80 rval := reflect.ValueOf(val) 81 switch rval.Kind() { 82 case reflect.Ptr: 83 if rval.IsNil() { 84 return ErrDecodeToNil 85 } 86 rval = rval.Elem() 87 case reflect.Map: 88 if rval.IsNil() { 89 return ErrDecodeToNil 90 } 91 default: 92 return fmt.Errorf("argument to Decode must be a pointer or a map, but got %v", rval) 93 } 94 decoder, err := d.dc.LookupDecoder(rval.Type()) 95 if err != nil { 96 return err 97 } 98 return decoder.DecodeValue(d.dc, d.vr, rval) 99} 100 101// Reset will reset the state of the decoder, using the same *DecodeContext used in 102// the original construction but using vr for reading. 103func (d *Decoder) Reset(vr bsonrw.ValueReader) error { 104 d.vr = vr 105 return nil 106} 107 108// SetRegistry replaces the current registry of the decoder with r. 109func (d *Decoder) SetRegistry(r *bsoncodec.Registry) error { 110 d.dc.Registry = r 111 return nil 112} 113 114// SetContext replaces the current registry of the decoder with dc. 115func (d *Decoder) SetContext(dc bsoncodec.DecodeContext) error { 116 d.dc = dc 117 return nil 118} 119