Skip to content
/ mgo Public
forked from go-mgo/mgo

Commit

Permalink
Merge v2-unstable into v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Aug 1, 2016
2 parents 29cc868 + 9a2573d commit 090c469
Show file tree
Hide file tree
Showing 121 changed files with 15,085 additions and 639 deletions.
45 changes: 45 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
language: go

go_import_path: gopkg.in/mgo.v2-unstable

addons:
apt:
packages:

env:
global:
- BUCKET=https://niemeyer.s3.amazonaws.com
matrix:
- GO=1.4.1 MONGODB=x86_64-2.2.7
- GO=1.4.1 MONGODB=x86_64-2.4.14
- GO=1.4.1 MONGODB=x86_64-2.6.11
- GO=1.4.1 MONGODB=x86_64-3.0.9
- GO=1.4.1 MONGODB=x86_64-3.2.3-nojournal
- GO=1.5.3 MONGODB=x86_64-3.0.9
- GO=1.6 MONGODB=x86_64-3.0.9

install:
- eval "$(gimme $GO)"

- wget $BUCKET/mongodb-linux-$MONGODB.tgz
- tar xzvf mongodb-linux-$MONGODB.tgz
- export PATH=$PWD/mongodb-linux-$MONGODB/bin:$PATH

- wget $BUCKET/daemontools.tar.gz
- tar xzvf daemontools.tar.gz
- export PATH=$PWD/daemontools:$PATH

- go get gopkg.in/check.v1
- go get gopkg.in/yaml.v2
- go get gopkg.in/tomb.v2

before_script:
- export NOIPV6=1
- make startdb

script:
- (cd bson && go test -check.v)
- go test -check.v -fast
- (cd txn && go test -check.v)

# vim:sw=4:ts=4:et
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
startdb:
@testdb/setup.sh start
@harness/setup.sh start

stopdb:
@testdb/setup.sh stop
@harness/setup.sh stop
2 changes: 1 addition & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ func (s *S) TestAuthX509Cred(c *C) {
c.Skip("server does not support SSL")
}

clientCertPEM, err := ioutil.ReadFile("testdb/client.pem")
clientCertPEM, err := ioutil.ReadFile("harness/certs/client.pem")
c.Assert(err, IsNil)

clientCert, err := tls.X509KeyPair(clientCertPEM, clientCertPEM)
Expand Down
23 changes: 20 additions & 3 deletions bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -204,6 +205,7 @@ func readRandomUint32() uint32 {
// machineId stores machine id generated once and used in subsequent calls
// to NewObjectId function.
var machineId = readMachineId()
var processId = os.Getpid()

// readMachineId generates and returns a machine id.
// If this function fails to get the hostname it will cause a runtime error.
Expand Down Expand Up @@ -234,9 +236,8 @@ func NewObjectId() ObjectId {
b[5] = machineId[1]
b[6] = machineId[2]
// Pid, 2 bytes, specs don't specify endianness, but we use big endian.
pid := os.Getpid()
b[7] = byte(pid >> 8)
b[8] = byte(pid)
b[7] = byte(processId >> 8)
b[8] = byte(processId)
// Increment, 3 bytes, big endian
i := atomic.AddUint32(&objectIdCounter, 1)
b[9] = byte(i >> 16)
Expand Down Expand Up @@ -276,6 +277,22 @@ var nullBytes = []byte("null")

// UnmarshalJSON turns *bson.ObjectId into a json.Unmarshaller.
func (id *ObjectId) UnmarshalJSON(data []byte) error {
if len(data) > 0 && (data[0] == '{' || data[0] == 'O') {
var v struct {
Id json.RawMessage `json:"$oid"`
Func struct {
Id json.RawMessage
} `json:"$oidFunc"`
}
err := jdec(data, &v)
if err == nil {
if len(v.Id) > 0 {
data = []byte(v.Id)
} else {
data = []byte(v.Func.Id)
}
}
}
if len(data) == 2 && data[0] == '"' && data[1] == '"' || bytes.Equal(data, nullBytes) {
*id = ""
return nil
Expand Down
15 changes: 12 additions & 3 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func makeZeroDoc(value interface{}) (zero interface{}) {
case reflect.Ptr:
pv := reflect.New(v.Type().Elem())
zero = pv.Interface()
case reflect.Slice, reflect.Int:
case reflect.Slice, reflect.Int, reflect.Int64, reflect.Struct:
zero = reflect.New(t).Interface()
default:
panic("unsupported doc type")
panic("unsupported doc type: " + t.Name())
}
return zero
}
Expand Down Expand Up @@ -1055,7 +1055,7 @@ type inlineBadKeyMap struct {
}
type inlineUnexported struct {
M map[string]interface{} ",inline"
unexported ",inline"
unexported ",inline"
}
type unexported struct {
A int
Expand Down Expand Up @@ -1580,6 +1580,9 @@ func (s *S) TestObjectIdJSONMarshaling(c *C) {
}
}

// --------------------------------------------------------------------------
// Spec tests

type specTest struct {
Description string
Documents []struct {
Expand Down Expand Up @@ -1821,3 +1824,9 @@ func (s *S) BenchmarkUnmarshalRaw(c *C) {
panic(err)
}
}

func (s *S) BenchmarkNewObjectId(c *C) {
for i := 0; i < c.N; i++ {
bson.NewObjectId()
}
}
Loading

0 comments on commit 090c469

Please sign in to comment.