blob: 03c86a122a0e1de39a46c3ea01d7cb46eaf4f6be [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// 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 "github.com/mongodb/mongo-go-driver/bson/bsoncodec"
16 "github.com/mongodb/mongo-go-driver/bson/bsonrw"
17)
18
19// This pool is used to keep the allocations of Decoders down. This is only used for the Marshal*
20// methods and is not consumable from outside of this package. The Decoders retrieved from this pool
21// must have both Reset and SetRegistry called on them.
22var decPool = sync.Pool{
23 New: func() interface{} {
24 return new(Decoder)
25 },
26}
27
28// A Decoder reads and decodes BSON documents from a stream. It reads from a bsonrw.ValueReader as
29// the source of BSON data.
30type Decoder struct {
31 dc bsoncodec.DecodeContext
32 vr bsonrw.ValueReader
33}
34
35// NewDecoder returns a new decoder that uses the DefaultRegistry to read from vr.
36func NewDecoder(vr bsonrw.ValueReader) (*Decoder, error) {
37 if vr == nil {
38 return nil, errors.New("cannot create a new Decoder with a nil ValueReader")
39 }
40
41 return &Decoder{
42 dc: bsoncodec.DecodeContext{Registry: DefaultRegistry},
43 vr: vr,
44 }, nil
45}
46
47// NewDecoderWithContext returns a new decoder that uses DecodeContext dc to read from vr.
48func NewDecoderWithContext(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader) (*Decoder, error) {
49 if dc.Registry == nil {
50 dc.Registry = DefaultRegistry
51 }
52 if vr == nil {
53 return nil, errors.New("cannot create a new Decoder with a nil ValueReader")
54 }
55
56 return &Decoder{
57 dc: dc,
58 vr: vr,
59 }, nil
60}
61
62// Decode reads the next BSON document from the stream and decodes it into the
63// value pointed to by val.
64//
65// The documentation for Unmarshal contains details about of BSON into a Go
66// value.
67func (d *Decoder) Decode(val interface{}) error {
68 if unmarshaler, ok := val.(Unmarshaler); ok {
69 // TODO(skriptble): Reuse a []byte here and use the AppendDocumentBytes method.
70 buf, err := bsonrw.Copier{}.CopyDocumentToBytes(d.vr)
71 if err != nil {
72 return err
73 }
74 return unmarshaler.UnmarshalBSON(buf)
75 }
76
77 rval := reflect.ValueOf(val)
78 if rval.Kind() != reflect.Ptr {
79 return fmt.Errorf("argument to Decode must be a pointer to a type, but got %v", rval)
80 }
81 rval = rval.Elem()
82 decoder, err := d.dc.LookupDecoder(rval.Type())
83 if err != nil {
84 return err
85 }
86 return decoder.DecodeValue(d.dc, d.vr, rval)
87}
88
89// Reset will reset the state of the decoder, using the same *DecodeContext used in
90// the original construction but using vr for reading.
91func (d *Decoder) Reset(vr bsonrw.ValueReader) error {
92 d.vr = vr
93 return nil
94}
95
96// SetRegistry replaces the current registry of the decoder with r.
97func (d *Decoder) SetRegistry(r *bsoncodec.Registry) error {
98 d.dc.Registry = r
99 return nil
100}
101
102// SetContext replaces the current registry of the decoder with dc.
103func (d *Decoder) SetContext(dc bsoncodec.DecodeContext) error {
104 d.dc = dc
105 return nil
106}