[GODRIVER-607] All the field names are saved /converted lowercase Created: 19/Oct/18 Updated: 27/Oct/23 Resolved: 06/Nov/18 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Mihai B | Assignee: | Unassigned |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
I have a struct as below. After storing it I've found (to my horror) that all the field names have been converted to a lowercase version. type Order struct { TransactionID string InvoiceID int ID string } { "_id" : "efaa0eab-1200-439d-a9bc-5783e6574aaf", "transactionid" : "sample transaction ID", "invoiceid" : NumberLong(1), "id" : "efaa0eab-1200-439d-a9bc-5783e6574aaf"} |
| Comments |
| Comment by Kristofer Brandow (Inactive) [ 12/Nov/18 ] |
|
The StructTagParser handles generating the name for the field. |
| Comment by Mihai B [ 10/Nov/18 ] |
|
The structs have no tags so the StructTagCode is not helping it. However it looks like that I could register a patched `ValueEncoder`. |
| Comment by Kristofer Brandow (Inactive) [ 05/Nov/18 ] |
|
Closing due to inactivity. |
| Comment by Kristofer Brandow (Inactive) [ 24/Oct/18 ] |
|
Hi themihai, If you want to customize this behavior for everything, you can create a new bsoncodec.StructTagParser and assign it to bsoncodec.DefaultStructTagParser or use it while creating a new bsoncodec.StructTagCodec and register that codec with the *bsoncodec.Registry that you use.
--Kris |
| Comment by Mihai B [ 24/Oct/18 ] |
|
@Kris, would you consider an option to config the encoder(bson) to use the Go convention(case name + inline behaviour) ? I could add tags but that would mean modifying all my types and perhaps also creating new/duplicate types with bson tags for the packages that I don't own. An option to use the "standard" go behaviour would be quite handy for people migrating to mongo. |
| Comment by Calvin Lobo [ 23/Oct/18 ] |
|
I just wanted to elaborate on this a bit more, as I originally came here to ask for clarification, and then figured it out myself
You need to put a tag `bson:"MyDesiredName"` beside the struct field. |
| Comment by Kristofer Brandow (Inactive) [ 22/Oct/18 ] |
|
Hi themihai, This behavior is in line with how the community Go driver's BSON library behaves. If you wish to keep casing for your field names, please add struct tags or use a custom value encoder. Thanks, Kris |