forked from domodwyer/mgo
-
Notifications
You must be signed in to change notification settings - Fork 229
Fallback to JSON tags when BSON tag isn't present #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package bson_test | ||
|
||
import ( | ||
"github.com/globalsign/mgo/bson" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
type mixedTagging struct { | ||
First string | ||
Second string `bson:"second_field"` | ||
Third string `json:"third_field"` | ||
Fourth string `bson:"fourth_field" json:"alternate"` | ||
} | ||
|
||
// TestTaggingFallback checks that tagging fallback can be used/works as expected. | ||
func (s *S) TestTaggingFallback(c *C) { | ||
initial := &mixedTagging{ | ||
First: "One", | ||
Second: "Two", | ||
Third: "Three", | ||
Fourth: "Four", | ||
} | ||
|
||
// Take only testing.T, leave only footprints. | ||
initialState := bson.JSONTagFallbackState() | ||
defer bson.SetJSONTagFallback(initialState) | ||
|
||
// Marshal with the new mode applied. | ||
bson.SetJSONTagFallback(true) | ||
bsonState, errBSON := bson.Marshal(initial) | ||
c.Assert(errBSON, IsNil) | ||
|
||
// Unmarshal into a generic map so that we can pick up the actual field names | ||
// selected. | ||
target := make(map[string]string) | ||
errUnmarshal := bson.Unmarshal(bsonState, target) | ||
c.Assert(errUnmarshal, IsNil) | ||
|
||
// No tag, so standard naming | ||
_, firstExists := target["first"] | ||
c.Assert(firstExists, Equals, true) | ||
|
||
// Just a BSON tag | ||
_, secondExists := target["second_field"] | ||
c.Assert(secondExists, Equals, true) | ||
|
||
// Just a JSON tag | ||
_, thirdExists := target["third_field"] | ||
c.Assert(thirdExists, Equals, true) | ||
|
||
// Should marshal 4th as fourth_field (since we have both tags) | ||
_, fourthExists := target["fourth_field"] | ||
c.Assert(fourthExists, Equals, true) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package bson | ||
|
||
// Current state of the JSON tag fallback option. | ||
var useJSONTagFallback = false | ||
|
||
// SetJSONTagFallback enables or disables the JSON-tag fallback for structure tagging. When this is enabled, structures | ||
// without BSON tags on a field will fall-back to using the JSON tag (if present). | ||
func SetJSONTagFallback(state bool) { | ||
useJSONTagFallback = state | ||
} | ||
|
||
// JSONTagFallbackState returns the current status of the JSON tag fallback compatability option. See SetJSONTagFallback | ||
// for more information. | ||
func JSONTagFallbackState() bool { | ||
return useJSONTagFallback | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
I am nitpicking here, but could you please document (readme.md) for ex that even if there are no json/bson tags in the field comment, the value of that comment field would still be used?
would be marshaled as
{"c_field": "value"}
This behaviour is not found in any other encoder that I know of, and hence could be quite confusing if used by chance.