[GODRIVER-1701] Add structcodec option equivalent to mgo's useJSONTagFallback Created: 22/Jul/20  Updated: 28/Oct/23  Resolved: 21/Aug/20

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

Type: Improvement Priority: Major - P3
Reporter: Andrew Hodel Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 0
Labels: mgocompat
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux 4.9, Mongo 3.6.14



 Description   

NEW DESCRIPTION

mgo supports a non-default option to fallback to using json tags for structs when bson tags aren't present, so an option should be added to StructCodec to do the same.

 

PREVIOUS DESCRIPTION

Basically if you have a struct that has something like:

 

```

WsId         string      `json:"ws_id"`

```

 

Running find() on a collection will not return the ws_id field for the document, but if you define the struct as:

 

```

Ws_Id         string      `json:"ws_id"`

```

 

It will work.

 

Read this - https://github.com/golang/go/issues/40351



 Comments   
Comment by Githook User [ 21/Aug/20 ]

Author:

{'name': 'Isabella Siu', 'email': 'sakurablossom@blueblueworld.com', 'username': 'iwysiu'}

Message: GODRIVER-1701 add JsonFallbackStructTagParser (#480)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/04b7cf242d9607b8ee1b53c9b8768da90e2348b4

Comment by Andrew Hodel [ 18/Aug/20 ]

Without a doubt, it is just such a square and easy solution.

Comment by Isabella Siu (Inactive) [ 10/Aug/20 ]

andrewhodel@gmail.com Different tag names allow us to support a different set of options from the json library, so we will not support parsing json tags by default. For example, the xml and json libraries do not support parsing each other's tags. However, as mgo did offer an option to turn on using json tags as a fallback, we will add it as an option to our structcodec.

Comment by Andrew Hodel [ 10/Aug/20 ]

That's not the truth here though, you are decreasing your user base by not fixing this.

 

Most people just write json in the struct, what's the point of not doing it to make a better product?

It is the user interface/access level of the project, nobody really asks for features but this is a pure software bug.  It seems to contradict the project not to make this right for the users.

Comment by Isabella Siu (Inactive) [ 10/Aug/20 ]

Hi andrewhodel@gmail.com,

If you would like to use multiple struct tags, you can use this syntax: 

WsId     string   `bson:“ws_id” json:“ws_id”`

Comment by Andrew Hodel [ 10/Aug/20 ]

How often is it that you retrieve BSON data that you aren't using a json api?

Or, Dude if it's mongodb you are using json.

Comment by Andrew Hodel [ 10/Aug/20 ]

No, because I am still using JSON in other places.

Why close the issue, it's not resolved.  It has to work with both as that's how people define it.

Comment by Isabella Siu (Inactive) [ 28/Jul/20 ]

Hi andrewhodel@gmail.com,

For bson decoding, you should use bson struct tags instead of json ones, so instead you would use:

WsId         string      `bson:"ws_id"`

Does this solve your issue?

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