Skip to content

Commit 4931a6c

Browse files
authored
Merge Development (globalsign#48)
* add DropAllIndexes() method (globalsign#25) Create a new method to drop all the indexes of a collection in a single call * readme: credit @feliixx for globalsign#25 (globalsign#26) * send metadata during handshake (globalsign#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 (globalsign#32) * docs: elaborate on what appName does * readme: add appName to changes * add method CreateView() (globalsign#33) Fix globalsign#30. Thanks to @feliixx for the time and effort. * readme: credit @feliixx in the README (globalsign#36) * Don't panic on indexed int64 fields (globalsign#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: * globalsign#23 * https://github.com/go-mgo/mgo/issues/475 * go-mgo#476 * Add collation option to collection.Create() (globalsign#37) - Allow specifying the default collation for the collection when creating it. - Add some documentation to query.Collation() method. fix globalsign#29 * Test against MongoDB 3.4.x (globalsign#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 globalsign#31 * Introduce constants for BSON element types (globalsign#41) * bson.Unmarshal returns time in UTC (globalsign#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 (globalsign#44) Fixes globalsign#43 * readme: credit @feliixx (globalsign#46) * Fix GetBSON() method usage (globalsign#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 (globalsign#47)
1 parent c8798fd commit 4931a6c

33 files changed

+951
-629
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ install:
3333
- go get gopkg.in/check.v1
3434
- go get gopkg.in/yaml.v2
3535
- go get gopkg.in/tomb.v2
36+
- go get github.com/golang/lint/golint
3637

3738
before_script:
39+
- golint ./... | grep -v 'ID' | cat
40+
- go vet github.com/globalsign/mgo/bson github.com/globalsign/mgo/txn github.com/globalsign/mgo
3841
- export NOIPV6=1
3942
- make startdb
4043

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,24 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
1717
* Hides SASL warnings ([details](https://github.com/globalsign/mgo/pull/7))
1818
* Support for partial indexes ([detials](https://github.com/domodwyer/mgo/commit/5efe8eccb028238d93c222828cae4806aeae9f51))
1919
* Fixes timezone handling ([details](https://github.com/go-mgo/mgo/pull/464))
20-
* 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))
20+
* 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))
2121
* 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))
2222
* Fixes cursor timeouts ([details](https://jira.mongodb.org/browse/SERVER-24899))
2323
* Support index hints and timeouts for count queries ([details](https://github.com/globalsign/mgo/pull/17))
2424
* Don't panic when handling indexed `int64` fields ([detials](https://github.com/go-mgo/mgo/issues/475))
2525
* Supports dropping all indexes on a collection ([details](https://github.com/globalsign/mgo/pull/25))
2626
* Annotates log entries/profiler output with optional appName on 3.4+ ([details](https://github.com/globalsign/mgo/pull/28))
2727
* Support for read-only [views](https://docs.mongodb.com/manual/core/views/) in 3.4+ ([details](https://github.com/globalsign/mgo/pull/33))
28+
* Support for [collations](https://docs.mongodb.com/manual/reference/collation/) in 3.4+ ([details](https://github.com/globalsign/mgo/pull/37))
29+
* Provide BSON constants for convenience/sanity ([details](https://github.com/globalsign/mgo/pull/41))
30+
* Consistently unmarshal time.Time values as UTC ([details](https://github.com/globalsign/mgo/pull/42))
31+
* Enforces best practise coding guidelines ([details](https://github.com/globalsign/mgo/pull/44))
32+
* GetBSON correctly handles structs with both fields and pointers ([details](https://github.com/globalsign/mgo/pull/40))
2833

2934
---
3035

3136
### Thanks to
37+
* @bozaro
3238
* @BenLubar
3339
* @carter2000
3440
* @cezarsa
@@ -37,6 +43,7 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
3743
* @feliixx
3844
* @fmpwizard
3945
* @jameinel
46+
* @gazoon
4047
* @mapete94
4148
* @Reenjii
4249
* @smoya

auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type getNonceCmd struct {
6161

6262
type getNonceResult struct {
6363
Nonce string
64-
Err string "$err"
64+
Err string `bson:"$err"`
6565
Code int
6666
}
6767

auth_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ func (s *S) TestAuthLoginCachingWithNewSession(c *C) {
580580
}
581581

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

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

936936
externalDB := session.DB("$external")
937-
var x509User mgo.User = mgo.User{
937+
var x509User = mgo.User{
938938
Username: x509Subject,
939939
OtherDBRoles: map[string][]mgo.Role{"admin": {mgo.RoleRoot}},
940940
}
@@ -1080,11 +1080,11 @@ func (kerberosSuite *KerberosSuite) TestAuthKerberosURL(c *C) {
10801080
c.Skip("no -kerberos")
10811081
}
10821082
c.Logf("Connecting to %s...", kerberosHost)
1083-
connectUri := url.QueryEscape(kerberosUser) + "@" + kerberosHost + "?authMechanism=GSSAPI"
1083+
connectURI := url.QueryEscape(kerberosUser) + "@" + kerberosHost + "?authMechanism=GSSAPI"
10841084
if runtime.GOOS == "windows" {
1085-
connectUri = url.QueryEscape(kerberosUser) + ":" + url.QueryEscape(getWindowsKerberosPassword()) + "@" + kerberosHost + "?authMechanism=GSSAPI"
1085+
connectURI = url.QueryEscape(kerberosUser) + ":" + url.QueryEscape(getWindowsKerberosPassword()) + "@" + kerberosHost + "?authMechanism=GSSAPI"
10861086
}
1087-
session, err := mgo.Dial(connectUri)
1087+
session, err := mgo.Dial(connectURI)
10881088
c.Assert(err, IsNil)
10891089
defer session.Close()
10901090
n, err := session.DB("kerberos").C("test").Find(M{}).Count()

bson/bson.go

+50-15
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,40 @@ import (
5656
// --------------------------------------------------------------------------
5757
// The public API.
5858

59-
// A value implementing the bson.Getter interface will have its GetBSON
59+
// Element types constants from BSON specification.
60+
const (
61+
ElementFloat64 byte = 0x01
62+
ElementString byte = 0x02
63+
ElementDocument byte = 0x03
64+
ElementArray byte = 0x04
65+
ElementBinary byte = 0x05
66+
Element06 byte = 0x06
67+
ElementObjectId byte = 0x07
68+
ElementBool byte = 0x08
69+
ElementDatetime byte = 0x09
70+
ElementNil byte = 0x0A
71+
ElementRegEx byte = 0x0B
72+
ElementDBPointer byte = 0x0C
73+
ElementJavaScriptWithoutScope byte = 0x0D
74+
ElementSymbol byte = 0x0E
75+
ElementJavaScriptWithScope byte = 0x0F
76+
ElementInt32 byte = 0x10
77+
ElementTimestamp byte = 0x11
78+
ElementInt64 byte = 0x12
79+
ElementDecimal128 byte = 0x13
80+
ElementMinKey byte = 0xFF
81+
ElementMaxKey byte = 0x7F
82+
83+
BinaryGeneric byte = 0x00
84+
BinaryFunction byte = 0x01
85+
BinaryBinaryOld byte = 0x02
86+
BinaryUUIDOld byte = 0x03
87+
BinaryUUID byte = 0x04
88+
BinaryMD5 byte = 0x05
89+
BinaryUserDefined byte = 0x80
90+
)
91+
92+
// Getter interface: a value implementing the bson.Getter interface will have its GetBSON
6093
// method called when the given value has to be marshalled, and the result
6194
// of this method will be marshaled in place of the actual object.
6295
//
@@ -66,12 +99,12 @@ type Getter interface {
6699
GetBSON() (interface{}, error)
67100
}
68101

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

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

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

159-
// See the RawD type.
192+
// RawDocElem elements of RawD type.
160193
type RawDocElem struct {
161194
Name string
162195
Value Raw
@@ -166,7 +199,7 @@ type RawDocElem struct {
166199
// long. MongoDB objects by default have such a property set in their "_id"
167200
// property.
168201
//
169-
// http://www.mongodb.org/display/DOCS/Object+IDs
202+
// http://www.mongodb.org/display/DOCS/Object+Ids
170203
type ObjectId string
171204

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

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

197230
// readRandomUint32 returns a random objectIdCounter.
198231
func readRandomUint32() uint32 {
@@ -300,12 +333,12 @@ func (id *ObjectId) UnmarshalJSON(data []byte) error {
300333
return nil
301334
}
302335
if len(data) != 26 || data[0] != '"' || data[25] != '"' {
303-
return errors.New(fmt.Sprintf("invalid ObjectId in JSON: %s", string(data)))
336+
return fmt.Errorf("invalid ObjectId in JSON: %s", string(data))
304337
}
305338
var buf [12]byte
306339
_, err := hex.Decode(buf[:], data[1:25])
307340
if err != nil {
308-
return errors.New(fmt.Sprintf("invalid ObjectId in JSON: %s (%s)", string(data), err))
341+
return fmt.Errorf("invalid ObjectId in JSON: %s (%s)", string(data), err)
309342
}
310343
*id = ObjectId(string(buf[:]))
311344
return nil
@@ -571,12 +604,12 @@ func Unmarshal(in []byte, out interface{}) (err error) {
571604
d := newDecoder(in)
572605
d.readDocTo(v)
573606
if d.i < len(d.in) {
574-
return errors.New("Document is corrupted")
607+
return errors.New("document is corrupted")
575608
}
576609
case reflect.Struct:
577-
return errors.New("Unmarshal can't deal with struct values. Use a pointer.")
610+
return errors.New("unmarshal can't deal with struct values. Use a pointer")
578611
default:
579-
return errors.New("Unmarshal needs a map or a pointer to a struct.")
612+
return errors.New("unmarshal needs a map or a pointer to a struct")
580613
}
581614
return nil
582615
}
@@ -600,13 +633,15 @@ func (raw Raw) Unmarshal(out interface{}) (err error) {
600633
return &TypeError{v.Type(), raw.Kind}
601634
}
602635
case reflect.Struct:
603-
return errors.New("Raw Unmarshal can't deal with struct values. Use a pointer.")
636+
return errors.New("raw Unmarshal can't deal with struct values. Use a pointer")
604637
default:
605-
return errors.New("Raw Unmarshal needs a map or a valid pointer.")
638+
return errors.New("raw Unmarshal needs a map or a valid pointer")
606639
}
607640
return nil
608641
}
609642

643+
// TypeError store details for type error occuring
644+
// during unmarshaling
610645
type TypeError struct {
611646
Type reflect.Type
612647
Kind byte

0 commit comments

Comments
 (0)