@@ -160,64 +160,69 @@ func (v Val) Interface() interface{} {
160
160
161
161
// MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.
162
162
func (v Val ) MarshalBSONValue () (bsontype.Type , []byte , error ) {
163
+ return v .MarshalAppendBSONValue (nil )
164
+ }
165
+
166
+ // MarshalAppendBSONValue is similar to MarshalBSONValue, but allows the caller to specify a slice
167
+ // to add the bytes to.
168
+ func (v Val ) MarshalAppendBSONValue (dst []byte ) (bsontype.Type , []byte , error ) {
163
169
t := v .Type ()
164
- var data []byte
165
170
switch v .Type () {
166
171
case bsontype .Double :
167
- data = bsoncore .AppendDouble (data , v .Double ())
172
+ dst = bsoncore .AppendDouble (dst , v .Double ())
168
173
case bsontype .String :
169
- data = bsoncore .AppendString (data , v .String ())
174
+ dst = bsoncore .AppendString (dst , v .String ())
170
175
case bsontype .EmbeddedDocument :
171
176
switch v .primitive .(type ) {
172
177
case Doc :
173
- t , data , _ = v .primitive .(Doc ).MarshalBSONValue () // Doc.MarshalBSONValue never returns an error.
178
+ t , dst , _ = v .primitive .(Doc ).MarshalBSONValue () // Doc.MarshalBSONValue never returns an error.
174
179
case MDoc :
175
- t , data , _ = v .primitive .(MDoc ).MarshalBSONValue () // MDoc.MarshalBSONValue never returns an error.
180
+ t , dst , _ = v .primitive .(MDoc ).MarshalBSONValue () // MDoc.MarshalBSONValue never returns an error.
176
181
}
177
182
case bsontype .Array :
178
- t , data , _ = v .Array ().MarshalBSONValue () // Arr.MarshalBSON never returns an error.
183
+ t , dst , _ = v .Array ().MarshalBSONValue () // Arr.MarshalBSON never returns an error.
179
184
case bsontype .Binary :
180
185
subtype , bindata := v .Binary ()
181
- data = bsoncore .AppendBinary (data , subtype , bindata )
186
+ dst = bsoncore .AppendBinary (dst , subtype , bindata )
182
187
case bsontype .Undefined :
183
188
case bsontype .ObjectID :
184
- data = bsoncore .AppendObjectID (data , v .ObjectID ())
189
+ dst = bsoncore .AppendObjectID (dst , v .ObjectID ())
185
190
case bsontype .Boolean :
186
- data = bsoncore .AppendBoolean (data , v .Boolean ())
191
+ dst = bsoncore .AppendBoolean (dst , v .Boolean ())
187
192
case bsontype .DateTime :
188
- data = bsoncore .AppendDateTime (data , int64 (v .DateTime ()))
193
+ dst = bsoncore .AppendDateTime (dst , int64 (v .DateTime ()))
189
194
case bsontype .Null :
190
195
case bsontype .Regex :
191
196
pattern , options := v .Regex ()
192
- data = bsoncore .AppendRegex (data , pattern , options )
197
+ dst = bsoncore .AppendRegex (dst , pattern , options )
193
198
case bsontype .DBPointer :
194
199
ns , ptr := v .DBPointer ()
195
- data = bsoncore .AppendDBPointer (data , ns , ptr )
200
+ dst = bsoncore .AppendDBPointer (dst , ns , ptr )
196
201
case bsontype .JavaScript :
197
- data = bsoncore .AppendJavaScript (data , string (v .JavaScript ()))
202
+ dst = bsoncore .AppendJavaScript (dst , string (v .JavaScript ()))
198
203
case bsontype .Symbol :
199
- data = bsoncore .AppendSymbol (data , string (v .Symbol ()))
204
+ dst = bsoncore .AppendSymbol (dst , string (v .Symbol ()))
200
205
case bsontype .CodeWithScope :
201
206
code , doc := v .CodeWithScope ()
202
207
var scope []byte
203
208
scope , _ = doc .MarshalBSON () // Doc.MarshalBSON never returns an error.
204
- data = bsoncore .AppendCodeWithScope (data , code , scope )
209
+ dst = bsoncore .AppendCodeWithScope (dst , code , scope )
205
210
case bsontype .Int32 :
206
- data = bsoncore .AppendInt32 (data , v .Int32 ())
211
+ dst = bsoncore .AppendInt32 (dst , v .Int32 ())
207
212
case bsontype .Timestamp :
208
213
t , i := v .Timestamp ()
209
- data = bsoncore .AppendTimestamp (data , t , i )
214
+ dst = bsoncore .AppendTimestamp (dst , t , i )
210
215
case bsontype .Int64 :
211
- data = bsoncore .AppendInt64 (data , v .Int64 ())
216
+ dst = bsoncore .AppendInt64 (dst , v .Int64 ())
212
217
case bsontype .Decimal128 :
213
- data = bsoncore .AppendDecimal128 (data , v .Decimal128 ())
218
+ dst = bsoncore .AppendDecimal128 (dst , v .Decimal128 ())
214
219
case bsontype .MinKey :
215
220
case bsontype .MaxKey :
216
221
default :
217
222
panic (fmt .Errorf ("invalid BSON type %v" , t ))
218
223
}
219
224
220
- return t , data , nil
225
+ return t , dst , nil
221
226
}
222
227
223
228
// UnmarshalBSONValue implements the bsoncodec.ValueUnmarshaler interface.
0 commit comments