[GODRIVER-1011] Struct decoder only works for lowercased fields Created: 30/Apr/19  Updated: 28/Oct/23  Resolved: 05/Sep/19

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

Type: Bug Priority: Major - P3
Reporter: Divjot Arora (Inactive) Assignee: Divjot Arora (Inactive)
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

If no struct tags are provided, the struct decoder will only work if the lowercased struct field name matches the BSON key. This doesn't work for camel-cased fields.

For example, if the BSON key is "firstBatch", the struct decoder will not find it in the struct because it is looking for "firstbatch".

Filing this as a question ticket because I'm not sure what we want the behavior to be. At the very least, we should document this behavior and encourage users to always use struct tags.



 Comments   
Comment by Githook User [ 05/Sep/19 ]

Author:

{'name': 'Divjot Arora', 'username': 'divjotarora', 'email': 'divjot.arora@10gen.com'}

Message: Change struct decoder to be case-insensitive.

GODRIVER-1011

Change-Id: I31674da7af7405f659dbbe7d0aaeeadca7fdb946
Branch: release/1.1
https://github.com/mongodb/mongo-go-driver/commit/9183af527db11a7cab445207ddcbaa99ac9918d6

Comment by Githook User [ 05/Sep/19 ]

Author:

{'name': 'Divjot Arora', 'username': 'divjotarora', 'email': 'divjot.arora@10gen.com'}

Message: Change struct decoder to be case-insensitive.

GODRIVER-1011

Change-Id: I31674da7af7405f659dbbe7d0aaeeadca7fdb946
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/3e47e0b83c87c1f96ea2fa207b16dff5fa747e84

Comment by Divjot Arora (Inactive) [ 04/Sep/19 ]

CR: https://review.gerrithub.io/c/mongodb/mongo-go-driver/+/467447

Comment by David Golden [ 30/Apr/19 ]

encoding/json says:

To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match.

We should match that behavior as that will be users' default expectation.

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