Skip to content

Commit

Permalink
Merge Development (#48)
Browse files Browse the repository at this point in the history
* add DropAllIndexes() method (#25)

Create a new method to drop all the indexes of a collection
in a single call

* readme: credit @feliixx for #25 (#26)

* send metadata during handshake (#28)

fix [#484](https://github.com/go-mgo/mgo/issues/484)

Annotate connections with metadata provided by the
connecting client.

informations send:

{
 "aplication": {         // optional
   "name": "myAppName"
 }
 "driver": {
    "name": "mgo",
    "version": "v2"
  },
  "os": {
    "type": runtime.GOOS,
    "architecture": runtime.GOARCH
  }
}

to set "application.name", add `appname` param in options
of string connection URI,
for example : "mongodb://localhost:27017?appname=myAppName"

* Update README to add appName (#32)

* docs: elaborate on what appName does

* readme: add appName to changes

* add method CreateView() (#33)

Fix #30.

Thanks to @feliixx for the time and effort.

* readme: credit @feliixx in the README (#36)

* Don't panic on indexed int64 fields (#23)

* Stop all db instances after tests (go-mgo#462)

If all tests pass, the builds for mongo earlier than 2.6 are still failing.
Running a clean up fixes the issue.

* fixing int64 type failing when getting indexes and trying to type them

* requested changes relating to case statement and panic

* Update README.md to credit @mapete94.

* tests: ensure indexed int64 fields do not cause a panic in Indexes()

See:
* #23
* https://github.com/go-mgo/mgo/issues/475
* go-mgo#476

* Add collation option to collection.Create() (#37)

- Allow specifying the default collation for the collection when creating it.
- Add some documentation to query.Collation() method.

fix #29

* Test against MongoDB 3.4.x (#35)

* test against MongoDB 3.4.x

* tests: use listIndexes to assert index state for 3.4+

* make test pass against v3.4.x

  - skip `TestViewWithCollation` because of SERVER-31049,
    cf: https://jira.mongodb.org/browse/SERVER-31049

  - add versionAtLeast() method in init.js script to better
    detect server version

fixes #31

* Introduce constants for BSON element types (#41)

* bson.Unmarshal returns time in UTC (#42)

* readme: add missing features / credit

* Adds missing collation feature description (by @feliixx).
* Adds missing 3.4 tests description (by @feliixx).
* Adds BSON constants description (by @bozaro).
* Adds UTC time.Time unmarshalling (by @gazoon).

* fix golint, go vet and gofmt warnings (#44)

Fixes #43

* readme: credit @feliixx (#46)

* Fix GetBSON() method usage (#40)

* Fix GetBSON() method usage

Original issue
---

You can't use type with custom GetBSON() method mixed with structure field type and structure field reference type.

For example, you can't create custom GetBSON() for Bar type:

```
struct Foo {
	a  Bar
	b *Bar
}
```

Type implementation (`func (t Bar) GetBSON()` ) would crash on `Foo.b = nil` value encoding.

Reference implementation (`func (t *Bar) GetBSON()` ) would not call on `Foo.a` value encoding.

After this change
---

For type implementation  `func (t Bar) GetBSON()` would not call on `Foo.b = nil` value encoding.
In this case `nil` value would be seariazied as `nil` BSON value.

For reference implementation `func (t *Bar) GetBSON()` would call even on `Foo.a` value encoding.

* Minor refactoring

* readme: credit @bozaro (#47)
  • Loading branch information
domodwyer authored Oct 11, 2017
1 parent 3dbb487 commit c4a7121
Show file tree
Hide file tree
Showing 33 changed files with 951 additions and 629 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ install:
- go get gopkg.in/check.v1
- go get gopkg.in/yaml.v2
- go get gopkg.in/tomb.v2
- go get github.com/golang/lint/golint

before_script:
- golint ./... | grep -v 'ID' | cat
- go vet github.com/globalsign/mgo/bson github.com/globalsign/mgo/txn github.com/globalsign/mgo
- export NOIPV6=1
- make startdb

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
* Hides SASL warnings ([details](https://github.com/globalsign/mgo/pull/7))
* Support for partial indexes ([detials](https://github.com/domodwyer/mgo/commit/5efe8eccb028238d93c222828cae4806aeae9f51))
* Fixes timezone handling ([details](https://github.com/go-mgo/mgo/pull/464))
* Integration tests run against newest MongoDB 3.2 releases ([details](https://github.com/globalsign/mgo/pull/4), [more](https://github.com/globalsign/mgo/pull/24))
* Integration tests run against MongoDB 3.2 & 3.4 releases ([details](https://github.com/globalsign/mgo/pull/4), [more](https://github.com/globalsign/mgo/pull/24), [more](https://github.com/globalsign/mgo/pull/35))
* Improved multi-document transaction performance ([details](https://github.com/globalsign/mgo/pull/10), [more](https://github.com/globalsign/mgo/pull/11), [more](https://github.com/globalsign/mgo/pull/16))
* Fixes cursor timeouts ([details](https://jira.mongodb.org/browse/SERVER-24899))
* Support index hints and timeouts for count queries ([details](https://github.com/globalsign/mgo/pull/17))
* Don't panic when handling indexed `int64` fields ([detials](https://github.com/go-mgo/mgo/issues/475))
* Supports dropping all indexes on a collection ([details](https://github.com/globalsign/mgo/pull/25))
* Annotates log entries/profiler output with optional appName on 3.4+ ([details](https://github.com/globalsign/mgo/pull/28))
* Support for read-only [views](https://docs.mongodb.com/manual/core/views/) in 3.4+ ([details](https://github.com/globalsign/mgo/pull/33))
* Support for [collations](https://docs.mongodb.com/manual/reference/collation/) in 3.4+ ([details](https://github.com/globalsign/mgo/pull/37))
* Provide BSON constants for convenience/sanity ([details](https://github.com/globalsign/mgo/pull/41))
* Consistently unmarshal time.Time values as UTC ([details](https://github.com/globalsign/mgo/pull/42))
* Enforces best practise coding guidelines ([details](https://github.com/globalsign/mgo/pull/44))
* GetBSON correctly handles structs with both fields and pointers ([details](https://github.com/globalsign/mgo/pull/40))

---

### Thanks to
* @bozaro
* @BenLubar
* @carter2000
* @cezarsa
Expand All @@ -37,6 +43,7 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
* @feliixx
* @fmpwizard
* @jameinel
* @gazoon
* @mapete94
* @Reenjii
* @smoya
Expand Down
2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type getNonceCmd struct {

type getNonceResult struct {
Nonce string
Err string "$err"
Err string `bson:"$err"`
Code int
}

Expand Down
10 changes: 5 additions & 5 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ func (s *S) TestAuthLoginCachingWithNewSession(c *C) {
}

func (s *S) TestAuthLoginCachingAcrossPool(c *C) {
// Logins are cached even when the conenction goes back
// Logins are cached even when the connection goes back
// into the pool.

session, err := mgo.Dial("localhost:40002")
Expand Down Expand Up @@ -934,7 +934,7 @@ func (s *S) TestAuthX509Cred(c *C) {
x509Subject := "CN=localhost,OU=Client,O=MGO,L=MGO,ST=MGO,C=GO"

externalDB := session.DB("$external")
var x509User mgo.User = mgo.User{
var x509User = mgo.User{
Username: x509Subject,
OtherDBRoles: map[string][]mgo.Role{"admin": {mgo.RoleRoot}},
}
Expand Down Expand Up @@ -1080,11 +1080,11 @@ func (kerberosSuite *KerberosSuite) TestAuthKerberosURL(c *C) {
c.Skip("no -kerberos")
}
c.Logf("Connecting to %s...", kerberosHost)
connectUri := url.QueryEscape(kerberosUser) + "@" + kerberosHost + "?authMechanism=GSSAPI"
connectURI := url.QueryEscape(kerberosUser) + "@" + kerberosHost + "?authMechanism=GSSAPI"
if runtime.GOOS == "windows" {
connectUri = url.QueryEscape(kerberosUser) + ":" + url.QueryEscape(getWindowsKerberosPassword()) + "@" + kerberosHost + "?authMechanism=GSSAPI"
connectURI = url.QueryEscape(kerberosUser) + ":" + url.QueryEscape(getWindowsKerberosPassword()) + "@" + kerberosHost + "?authMechanism=GSSAPI"
}
session, err := mgo.Dial(connectUri)
session, err := mgo.Dial(connectURI)
c.Assert(err, IsNil)
defer session.Close()
n, err := session.DB("kerberos").C("test").Find(M{}).Count()
Expand Down
65 changes: 50 additions & 15 deletions bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,40 @@ import (
// --------------------------------------------------------------------------
// The public API.

// A value implementing the bson.Getter interface will have its GetBSON
// Element types constants from BSON specification.
const (
ElementFloat64 byte = 0x01
ElementString byte = 0x02
ElementDocument byte = 0x03
ElementArray byte = 0x04
ElementBinary byte = 0x05
Element06 byte = 0x06
ElementObjectId byte = 0x07
ElementBool byte = 0x08
ElementDatetime byte = 0x09
ElementNil byte = 0x0A
ElementRegEx byte = 0x0B
ElementDBPointer byte = 0x0C
ElementJavaScriptWithoutScope byte = 0x0D
ElementSymbol byte = 0x0E
ElementJavaScriptWithScope byte = 0x0F
ElementInt32 byte = 0x10
ElementTimestamp byte = 0x11
ElementInt64 byte = 0x12
ElementDecimal128 byte = 0x13
ElementMinKey byte = 0xFF
ElementMaxKey byte = 0x7F

BinaryGeneric byte = 0x00
BinaryFunction byte = 0x01
BinaryBinaryOld byte = 0x02
BinaryUUIDOld byte = 0x03
BinaryUUID byte = 0x04
BinaryMD5 byte = 0x05
BinaryUserDefined byte = 0x80
)

// Getter interface: a value implementing the bson.Getter interface will have its GetBSON
// method called when the given value has to be marshalled, and the result
// of this method will be marshaled in place of the actual object.
//
Expand All @@ -66,12 +99,12 @@ type Getter interface {
GetBSON() (interface{}, error)
}

// A value implementing the bson.Setter interface will receive the BSON
// Setter interface: a value implementing the bson.Setter interface will receive the BSON
// value via the SetBSON method during unmarshaling, and the object
// itself will not be changed as usual.
//
// If setting the value works, the method should return nil or alternatively
// bson.SetZero to set the respective field to its zero value (nil for
// bson.ErrSetZero to set the respective field to its zero value (nil for
// pointer types). If SetBSON returns a value of type bson.TypeError, the
// BSON value will be omitted from a map or slice being decoded and the
// unmarshalling will continue. If it returns any other non-nil error, the
Expand All @@ -97,10 +130,10 @@ type Setter interface {
SetBSON(raw Raw) error
}

// SetZero may be returned from a SetBSON method to have the value set to
// ErrSetZero may be returned from a SetBSON method to have the value set to
// its respective zero value. When used in pointer values, this will set the
// field to nil rather than to the pre-allocated value.
var SetZero = errors.New("set to zero")
var ErrSetZero = errors.New("set to zero")

// M is a convenient alias for a map[string]interface{} map, useful for
// dealing with BSON in a native way. For instance:
Expand Down Expand Up @@ -156,7 +189,7 @@ type Raw struct {
// documents in general.
type RawD []RawDocElem

// See the RawD type.
// RawDocElem elements of RawD type.
type RawDocElem struct {
Name string
Value Raw
Expand All @@ -166,7 +199,7 @@ type RawDocElem struct {
// long. MongoDB objects by default have such a property set in their "_id"
// property.
//
// http://www.mongodb.org/display/DOCS/Object+IDs
// http://www.mongodb.org/display/DOCS/Object+Ids
type ObjectId string

// ObjectIdHex returns an ObjectId from the provided hex representation.
Expand All @@ -192,7 +225,7 @@ func IsObjectIdHex(s string) bool {

// objectIdCounter is atomically incremented when generating a new ObjectId
// using NewObjectId() function. It's used as a counter part of an id.
var objectIdCounter uint32 = readRandomUint32()
var objectIdCounter = readRandomUint32()

// readRandomUint32 returns a random objectIdCounter.
func readRandomUint32() uint32 {
Expand Down Expand Up @@ -300,12 +333,12 @@ func (id *ObjectId) UnmarshalJSON(data []byte) error {
return nil
}
if len(data) != 26 || data[0] != '"' || data[25] != '"' {
return errors.New(fmt.Sprintf("invalid ObjectId in JSON: %s", string(data)))
return fmt.Errorf("invalid ObjectId in JSON: %s", string(data))
}
var buf [12]byte
_, err := hex.Decode(buf[:], data[1:25])
if err != nil {
return errors.New(fmt.Sprintf("invalid ObjectId in JSON: %s (%s)", string(data), err))
return fmt.Errorf("invalid ObjectId in JSON: %s (%s)", string(data), err)
}
*id = ObjectId(string(buf[:]))
return nil
Expand Down Expand Up @@ -571,12 +604,12 @@ func Unmarshal(in []byte, out interface{}) (err error) {
d := newDecoder(in)
d.readDocTo(v)
if d.i < len(d.in) {
return errors.New("Document is corrupted")
return errors.New("document is corrupted")
}
case reflect.Struct:
return errors.New("Unmarshal can't deal with struct values. Use a pointer.")
return errors.New("unmarshal can't deal with struct values. Use a pointer")
default:
return errors.New("Unmarshal needs a map or a pointer to a struct.")
return errors.New("unmarshal needs a map or a pointer to a struct")
}
return nil
}
Expand All @@ -600,13 +633,15 @@ func (raw Raw) Unmarshal(out interface{}) (err error) {
return &TypeError{v.Type(), raw.Kind}
}
case reflect.Struct:
return errors.New("Raw Unmarshal can't deal with struct values. Use a pointer.")
return errors.New("raw Unmarshal can't deal with struct values. Use a pointer")
default:
return errors.New("Raw Unmarshal needs a map or a valid pointer.")
return errors.New("raw Unmarshal needs a map or a valid pointer")
}
return nil
}

// TypeError store details for type error occuring
// during unmarshaling
type TypeError struct {
Type reflect.Type
Kind byte
Expand Down
Loading

0 comments on commit c4a7121

Please sign in to comment.