Skip to content

Commit b9977e4

Browse files
author
Divjot Arora
committed
docs improvements
1 parent 92e7984 commit b9977e4

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

bson/bsoncodec/bsoncodec.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,19 @@ func (fn ValueDecoderFunc) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader,
160160
return fn(dc, vr, val)
161161
}
162162

163-
// typeDecoder is the interface implemented by types that can handle the decoding of a value by
163+
// typeDecoder is the interface implemented by types that can handle the decoding of a value given its type.
164164
type typeDecoder interface {
165165
decodeType(DecodeContext, bsonrw.ValueReader, reflect.Type) (reflect.Value, error)
166166
}
167167

168-
// typeDecoderFunc is an adapter function that allows a function with the correct signature to be
169-
// used as a TypeDecoder.
168+
// typeDecoderFunc is an adapter function that allows a function with the correct signature to be used as a typeDecoder.
170169
type typeDecoderFunc func(DecodeContext, bsonrw.ValueReader, reflect.Type) (reflect.Value, error)
171170

172171
func (fn typeDecoderFunc) decodeType(dc DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
173172
return fn(dc, vr, t)
174173
}
175174

176-
// ValueTypeDecoderFunc is an adapter struct that allows two functions with the correct signature
177-
// to be used as a ValueTypeDecoder.
175+
// decodeAdapter allows two functions with the correct signatures to be used as both a ValueDecoder and typeDecoder.
178176
type decodeAdapter struct {
179177
ValueDecoderFunc
180178
typeDecoderFunc
@@ -183,8 +181,8 @@ type decodeAdapter struct {
183181
var _ ValueDecoder = decodeAdapter{}
184182
var _ typeDecoder = decodeAdapter{}
185183

186-
// decodeTypeOrValue calls decoder.decodeType is decoder is a typeDecoder. Otherwise, it allocates a new element of
187-
// type t and calls decoder.DecodeValue on it.
184+
// decodeTypeOrValue calls decoder.decodeType is decoder is a typeDecoder. Otherwise, it allocates a new element of type
185+
// t and calls decoder.DecodeValue on it.
188186
func decodeTypeOrValue(decoder ValueDecoder, dc DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
189187
if typeDecoder, ok := decoder.(typeDecoder); ok {
190188
return typeDecoder.decodeType(dc, vr, t)

bson/bsoncodec/default_value_decoders.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (dvd DefaultValueDecoders) DDecodeValue(dc DecodeContext, vr bsonrw.ValueRe
132132
}
133133
typeDecoder, isTypeDecoder := decoder.(typeDecoder)
134134

135+
// Use the elements in the provided value if it's non nil. Otherwise, allocate a new D instance.
135136
var elems primitive.D
136137
if !val.IsNil() {
137138
val.SetLen(0)
@@ -149,7 +150,9 @@ func (dvd DefaultValueDecoders) DDecodeValue(dc DecodeContext, vr bsonrw.ValueRe
149150
}
150151

151152
// Delegate out to the typeDecoder for interface{} if it exists. If not, create a new interface{} value and
152-
// delegate out to the ValueDecoder.
153+
// delegate out to the ValueDecoder. This could be accomplished by calling decodeTypeOrValue, but this would
154+
// require casting decoder to typeDecoder for every element. Because decoder isn't changing, we can optimize and
155+
// only cast once.
153156
var elem reflect.Value
154157
if isTypeDecoder {
155158
elem, err = typeDecoder.decodeType(dc, elemVr, tEmpty)

0 commit comments

Comments
 (0)