Skip to content

Commit

Permalink
Merge Development (globalsign#111)
Browse files Browse the repository at this point in the history
* Brings in a patch on having flusher not suppress errors. (globalsign#81)

go-mgo#360

* Fallback to JSON tags when BSON tag isn't present (globalsign#91)

* Fallback to JSON tags when BSON tag isn't present


Cleanup.

* Add test to demonstrate tagging fallback.

- Test coverage for tagging test.

* socket: only send client metadata once per socket

Periodic cluster synchronisation calls isMaster() which currently resends the
"client" metadata every call - the spec specifies:

	isMaster commands issued after the initial connection handshake MUST NOT
	contain handshake arguments

	https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#connection-handshake

This hotfix prevents subsequent isMaster calls from sending the client metadata
again - fixes globalsign#101 and fixes globalsign#103.

Thanks to @changwoo-nam @qhenkart @canthefason @jyoon17 for spotting the initial
issue, opening tickets, and having the problem debugged with a PoC fix before I
even woke up.

* Cluster abended test 254 (globalsign#100)

* Add a test that mongo Server gets their abended reset as necessary.

See https://github.com/go-mgo/mgo/issues/254 and
https://github.com/go-mgo/mgo/pull/255/files

* Include the patch from Issue 255.

This brings in a test which fails without the patch, and passes with the
patch. Still to be tested, manual tcpkill of a socket.

* changeStream support (globalsign#97)

Add $changeStream support

* readme: credit @peterdeka and @steve-gray (globalsign#110)
  • Loading branch information
domodwyer authored Feb 20, 2018
1 parent ca8fc4f commit 26231d6
Show file tree
Hide file tree
Showing 14 changed files with 1,055 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_harness

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
* Minimise socket connection timeouts due to excessive locking ([details](https://github.com/globalsign/mgo/pull/52))
* Natively support X509 client authentication ([details](https://github.com/globalsign/mgo/pull/55))
* Gracefully recover from a temporarily unreachable server ([details](https://github.com/globalsign/mgo/pull/69))
* Use JSON tags when no explicit BSON are tags set ([details](https://github.com/globalsign/mgo/pull/91))
* Support [$changeStream](https://docs.mongodb.com/manual/changeStreams/) tailing on 3.6+ ([details](https://github.com/globalsign/mgo/pull/97))

---

Expand All @@ -51,6 +53,8 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
* @jameinel
* @gazoon
* @mapete94
* @peterdeka
* @Reenjii
* @smoya
* @steve-gray
* @wgallagher
16 changes: 14 additions & 2 deletions bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,21 @@ func getStructInfo(st reflect.Type) (*structInfo, error) {
info := fieldInfo{Num: i}

tag := field.Tag.Get("bson")
if tag == "" && strings.Index(string(field.Tag), ":") < 0 {
tag = string(field.Tag)

// Fall-back to JSON struct tag, if feature flag is set.
if tag == "" && useJSONTagFallback {
tag = field.Tag.Get("json")
}

// If there's no bson/json tag available.
if tag == "" {
// If there's no tag, and also no tag: value splits (i.e. no colon)
// then assume the entire tag is the value
if strings.Index(string(field.Tag), ":") < 0 {
tag = string(field.Tag)
}
}

if tag == "-" {
continue
}
Expand Down
54 changes: 54 additions & 0 deletions bson/compatability_test.go
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)
}
16 changes: 16 additions & 0 deletions bson/compatibility.go
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
}
Loading

0 comments on commit 26231d6

Please sign in to comment.