-
Notifications
You must be signed in to change notification settings - Fork 918
GODRIVER-3574 Align BSON interface slice decoding with json package. #2075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
3551518
b57049b
841cd96
779d6b0
0bb71be
033957f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1351,14 +1351,17 @@ func decodeDefault(dc DecodeContext, vr ValueReader, val reflect.Value) ([]refle | |
| } | ||
|
|
||
| var elem reflect.Value | ||
| if vDecoder == nil { | ||
| if vDecoder == nil && idx < val.Len() { | ||
| elem = val.Index(idx).Elem() | ||
| switch { | ||
| case elem.Kind() != reflect.Ptr || elem.IsNil(): | ||
| valueDecoder, err := dc.LookupDecoder(elem.Type()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !elem.CanSet() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is really nuanced. Can we add a comment here noting why we do this? "If an element is unsettable and allocated, it must be overwritten."
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also suggest making the following changes to make this more readable: isInterfaceSlice := eType.Kind() == reflect.Interface && val.Len() > 0
// If this is not an interface slice with pre-populated elements, we can look
// up the decoder for eType once.
var vDecoder ValueDecoder
if !isInterfaceSlice {
vDecoder, err = dc.LookupDecoder(eType)
if err != nil {
return nil, err
}
}
for {
// ...
if isInterfaceSlice && idx < val.Len() {
// Decode into an existing interface{} slot.
// ...
} else {
// For non-interface slices, or if we've exhuasted the pre-populated
// slots, we create a fresh value.
if vDecoder == nil {
// ...
}
//...
}
} |
||
| elem = reflect.New(elem.Type()).Elem() | ||
| } | ||
| err = valueDecoder.DecodeValue(dc, vr, elem) | ||
| if err != nil { | ||
| return nil, newDecodeError(strconv.Itoa(idx), err) | ||
|
|
@@ -1380,6 +1383,12 @@ func decodeDefault(dc DecodeContext, vr ValueReader, val reflect.Value) ([]refle | |
| } | ||
| } | ||
| } else { | ||
| if vDecoder == nil { | ||
| vDecoder, err = dc.LookupDecoder(eType) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| elem, err = decodeTypeOrValueWithInfo(vDecoder, dc, vr, eType) | ||
| if err != nil { | ||
| return nil, newDecodeError(strconv.Itoa(idx), err) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we rename this to "overwriting prepopulated slice?"