[GODRIVER-1318] Make BSON unmarshaling error if target struct has no exported fields Created: 20/Sep/19  Updated: 18/Apr/23  Resolved: 18/Apr/23

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: David Golden Assignee: Unassigned
Resolution: Won't Do Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

We continue to see evidence of confused users who try to unmarshal into a struct like this:

type Foo struct {
    bar string `bson:"bar"`
    baz bool `bson:"baz"`
}

Perhaps we should consider returning an error, ErrNoExportedFields, if a user tries to unmarshal into such a struct. It deviates from the json package's behavior, but it does seem like a not-uncommon and very frustrating new user experience, so maybe it's worth it to get users to realize their error quickly (and avoid them needing to go to SO or the mailing list or Jira to ask what's wrong).

By contrast, a struct with mixed exported and unexported fields implies the user is at least thinking about visibility and if they try to unmarshal into that and it doesn't do what they expect, they probably understand visibility enough to figure it out. (Plus we can't error in that case the way we can if there are no exported fields.)



 Comments   
Comment by David Golden [ 27/Mar/23 ]

I don't think there's anything other than what we saw in the description. Non-expert Go users were making mistakes by not exporting any struct fields and it silently does nothing. If that hasn't been an issue reported by users since this ticket was filed, feel free to close it.

Generated at Thu Feb 08 08:36:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.