[GODRIVER-1343] Collection.FindOne is having problems with a struct that has a interface as attribute Created: 13/Oct/19 Updated: 22/Oct/19 Resolved: 22/Oct/19 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Sunny B | Assignee: | Isabella Siu (Inactive) |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
| Description |
|
Hi, Based on this issue In the same example, when we try to retrieve the document Collection.FindOne is also giving decode errors "no decoder found for main.Scannable"
I see Do we expect the decoding issue also be addressed in Please let me know Thank you |
| Comments |
| Comment by Divjot Arora (Inactive) [ 22/Oct/19 ] | |||||||||||||||||||||||||||||||||||||||||||
|
You can decode into map[string]interface{} (note this is the same as decoding into bson.M). This would yield something like:
Another option is to replace the interface field in the struct with a field that has a concrete type (i.e. replace IStatus with Status). I think we've reached a resolution, so I'm going to close this ticket. Feel free to leave a new comment or open a new ticket if you have any more questions about this. – Divjot | |||||||||||||||||||||||||||||||||||||||||||
| Comment by Sunny B [ 20/Oct/19 ] | |||||||||||||||||||||||||||||||||||||||||||
|
Hi divjot.arora, Thanks for the response. I have not seen any libraries for that. So, In order to avoid the decoding issue, I need to decode the result into map[string]interface{} first and then unmarshal into a concrete model. And if that struct any field had type as a interface{} we need to unmarshal to a corresponding struct. Do you see any other way? Thanks, Sunny
| |||||||||||||||||||||||||||||||||||||||||||
| Comment by Divjot Arora (Inactive) [ 16/Oct/19 ] | |||||||||||||||||||||||||||||||||||||||||||
|
Thanks for the code sample. The issue is not with the Container type, but instead with the IStatus field in it. When we get some data from the database that looks like {_id: ObjectID(), status: {code: "foo", message: "bar"}}, it's not possible for the driver to unmarshal the status subdocument into an IStatus type because IStatus has no defined structure. The driver has no way of knowing that the actual type it should unmarshal into is Status, not IStatus.
Do you know of any other libraries that accomplish the desired behavior? I've linked a code sample that tries to marshal a struct with an interface field and then unmarshal the result into another struct of the same type using the mgo and encoding/json libraries. For both, the struct can be marshalled but not unmarshalled. | |||||||||||||||||||||||||||||||||||||||||||
| Comment by Sunny B [ 16/Oct/19 ] | |||||||||||||||||||||||||||||||||||||||||||
|
Thanks, @isabella.siu for the quick response I put the example here which is similar to
As I described above, when we decode into Container object, its giving Please let me know Thanks, | |||||||||||||||||||||||||||||||||||||||||||
| Comment by Isabella Siu (Inactive) [ 15/Oct/19 ] | |||||||||||||||||||||||||||||||||||||||||||
|
What behavior would you expect from decoding into an interface? |