Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed May 14, 2024
1 parent 51517bc commit 7938e90
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
4 changes: 2 additions & 2 deletions bson/bsoncodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ type decodeAdapter struct {
var _ ValueDecoder = decodeAdapter{}
var _ typeDecoder = decodeAdapter{}

func decodeTypeOrValueWithInfo(vd ValueDecoder, td typeDecoder, dc DecodeContext, vr ValueReader, t reflect.Type, convert bool) (reflect.Value, error) {
if td != nil {
func decodeTypeOrValueWithInfo(vd ValueDecoder, dc DecodeContext, vr ValueReader, t reflect.Type, convert bool) (reflect.Value, error) {
if td := vd.(typeDecoder); td != nil {
val, err := td.decodeType(dc, vr, t)
if err == nil && convert && val.Type() != t {
// This conversion step is necessary for slices and maps. If a user declares variables like:
Expand Down
6 changes: 2 additions & 4 deletions bson/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func dDecodeValue(dc DecodeContext, vr ValueReader, val reflect.Value) error {
if err != nil {
return err
}
tEmptyTypeDecoder, _ := decoder.(typeDecoder)

// Use the elements in the provided value if it's non nil. Otherwise, allocate a new D instance.
var elems D
Expand All @@ -155,7 +154,7 @@ func dDecodeValue(dc DecodeContext, vr ValueReader, val reflect.Value) error {
}

// Pass false for convert because we don't need to call reflect.Value.Convert for tEmpty.
elem, err := decodeTypeOrValueWithInfo(decoder, tEmptyTypeDecoder, dc, elemVr, tEmpty, false)
elem, err := decodeTypeOrValueWithInfo(decoder, dc, elemVr, tEmpty, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -1274,7 +1273,6 @@ func decodeDefault(dc DecodeContext, vr ValueReader, val reflect.Value) ([]refle
if err != nil {
return nil, err
}
eTypeDecoder, _ := decoder.(typeDecoder)

idx := 0
for {
Expand All @@ -1286,7 +1284,7 @@ func decodeDefault(dc DecodeContext, vr ValueReader, val reflect.Value) ([]refle
return nil, err
}

elem, err := decodeTypeOrValueWithInfo(decoder, eTypeDecoder, dc, vr, eType, true)
elem, err := decodeTypeOrValueWithInfo(decoder, dc, vr, eType, true)
if err != nil {
return nil, newDecodeError(strconv.Itoa(idx), err)
}
Expand Down
2 changes: 1 addition & 1 deletion bson/empty_interface_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (eic emptyInterfaceCodec) decodeType(dc DecodeContext, vr ValueReader, t re
return emptyValue, err
}

elem, err := decodeTypeOrValueWithInfo(decoder, decoder.(typeDecoder), dc, vr, rtype, true)
elem, err := decodeTypeOrValueWithInfo(decoder, dc, vr, rtype, true)
if err != nil {
return emptyValue, err
}
Expand Down
3 changes: 1 addition & 2 deletions bson/map_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func (mc *mapCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect.Va
if err != nil {
return err
}
eTypeDecoder, _ := decoder.(typeDecoder)

if eType == tEmpty {
dc.ancestor = val.Type()
Expand All @@ -182,7 +181,7 @@ func (mc *mapCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect.Va
return err
}

elem, err := decodeTypeOrValueWithInfo(decoder, eTypeDecoder, dc, vr, eType, true)
elem, err := decodeTypeOrValueWithInfo(decoder, dc, vr, eType, true)
if err != nil {
return newDecodeError(key, err)
}
Expand Down

0 comments on commit 7938e90

Please sign in to comment.