[GODRIVER-1808] Unmarshaling BSON into a struct with interface fields containing concrete value types doesn't work as expected. Created: 11/Dec/20 Updated: 08/Jan/24 |
|
| Status: | Backlog |
| Project: | Go Driver |
| Component/s: | BSON |
| Affects Version/s: | 1.4.4 |
| Fix Version/s: | 2.0.0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | C1tas Wcniwohcrf | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 2 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
go 1.15.6 |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Epic Link: | Go Driver 2.0 BSON | ||||||||
| Quarter: | FY24Q3 | ||||||||
| Backwards Compatibility: | Major Change | ||||||||
| Description |
|
when set slice point to struct. bson can not decode value in to the origin object compare to json package. json works well
|
| Comments |
| Comment by Ed Pelc [ 07/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Ran into this when following the pattern from [eagain|https://eagain.net/articles/go-json-kind/] to init an interface field to bson/json.Raw. It'd be nice if there was a builtin option to register the above type so that we know there are tests for it as it is quite long.
The bson equivalent doesn't work with above. You have to use an explicit `Msg bson.Raw` field on `Envelope` which requires a different type or defining a custom bson unmarshaler. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sébastien GLON [ 09/Jun/23 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi,
And you can register it, like that:
Do not lost the `RegisterTypeMapEntry` to force decode array and embeded docuement | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Matt Dale [ 14/Oct/22 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Results of investigation: E.g.
See StructCodec.describeStruct, which takes a reflect.Type instead of a reflect.Value, meaning it can't access the original value that might have additional type information. Additionally, the struct description caching (see here) complicates getting different values for the fields of the same struct type. Confirmed this is still an issue as of Go Driver v1.10.2. The fix for this issue is to consider the type of the value stored in the interface field, if present, instead of just the field type. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Kevin Albertson [ 14/Dec/20 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi wangyuhengs@outlook.com, thank you for the report, and for the test script! Off-hand, I was able to run the script and observe the behavior you describe. This may not be a bug, and would be a backwards breaking change if we alter behavior of how the default decoder treats an empty interface. I think you could accomplish this by registering a custom BSON decoder (see go doc bsoncode for more info). |