podman
141 строка · 4.0 Кб
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{
26New: func() interface{} {
27return 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 {
34dc bsoncodec.DecodeContext
35vr bsonrw.ValueReader
36
37// We persist defaultDocumentM and defaultDocumentD on the Decoder to prevent overwriting from
38// (*Decoder).SetContext.
39defaultDocumentM bool
40defaultDocumentD bool
41}
42
43// NewDecoder returns a new decoder that uses the DefaultRegistry to read from vr.
44func NewDecoder(vr bsonrw.ValueReader) (*Decoder, error) {
45if vr == nil {
46return nil, errors.New("cannot create a new Decoder with a nil ValueReader")
47}
48
49return &Decoder{
50dc: bsoncodec.DecodeContext{Registry: DefaultRegistry},
51vr: vr,
52}, nil
53}
54
55// NewDecoderWithContext returns a new decoder that uses DecodeContext dc to read from vr.
56func NewDecoderWithContext(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader) (*Decoder, error) {
57if dc.Registry == nil {
58dc.Registry = DefaultRegistry
59}
60if vr == nil {
61return nil, errors.New("cannot create a new Decoder with a nil ValueReader")
62}
63
64return &Decoder{
65dc: dc,
66vr: vr,
67}, nil
68}
69
70// Decode reads the next BSON document from the stream and decodes it into the
71// value pointed to by val.
72//
73// The documentation for Unmarshal contains details about of BSON into a Go
74// value.
75func (d *Decoder) Decode(val interface{}) error {
76if unmarshaler, ok := val.(Unmarshaler); ok {
77// TODO(skriptble): Reuse a []byte here and use the AppendDocumentBytes method.
78buf, err := bsonrw.Copier{}.CopyDocumentToBytes(d.vr)
79if err != nil {
80return err
81}
82return unmarshaler.UnmarshalBSON(buf)
83}
84
85rval := reflect.ValueOf(val)
86switch rval.Kind() {
87case reflect.Ptr:
88if rval.IsNil() {
89return ErrDecodeToNil
90}
91rval = rval.Elem()
92case reflect.Map:
93if rval.IsNil() {
94return ErrDecodeToNil
95}
96default:
97return fmt.Errorf("argument to Decode must be a pointer or a map, but got %v", rval)
98}
99decoder, err := d.dc.LookupDecoder(rval.Type())
100if err != nil {
101return err
102}
103if d.defaultDocumentM {
104d.dc.DefaultDocumentM()
105}
106if d.defaultDocumentD {
107d.dc.DefaultDocumentD()
108}
109return decoder.DecodeValue(d.dc, d.vr, rval)
110}
111
112// Reset will reset the state of the decoder, using the same *DecodeContext used in
113// the original construction but using vr for reading.
114func (d *Decoder) Reset(vr bsonrw.ValueReader) error {
115d.vr = vr
116return nil
117}
118
119// SetRegistry replaces the current registry of the decoder with r.
120func (d *Decoder) SetRegistry(r *bsoncodec.Registry) error {
121d.dc.Registry = r
122return nil
123}
124
125// SetContext replaces the current registry of the decoder with dc.
126func (d *Decoder) SetContext(dc bsoncodec.DecodeContext) error {
127d.dc = dc
128return nil
129}
130
131// DefaultDocumentM will decode empty documents using the primitive.M type. This behavior is restricted to data typed as
132// "interface{}" or "map[string]interface{}".
133func (d *Decoder) DefaultDocumentM() {
134d.defaultDocumentM = true
135}
136
137// DefaultDocumentD will decode empty documents using the primitive.D type. This behavior is restricted to data typed as
138// "interface{}" or "map[string]interface{}".
139func (d *Decoder) DefaultDocumentD() {
140d.defaultDocumentD = true
141}
142