Merge lp:~jasmuth/mgo/skj-mgo-bson into lp:mgo/v2
Proposed by
John Asmuth
Status: | Needs review |
---|---|
Proposed branch: | lp:~jasmuth/mgo/skj-mgo-bson |
Merge into: | lp:mgo/v2 |
Diff against target: |
154 lines (+82/-3) 4 files modified
bson/bson.go (+1/-1) bson/bson_test.go (+24/-1) bson/decode.go (+38/-0) bson/encode.go (+19/-1) |
To merge this branch: | bzr merge lp:~jasmuth/mgo/skj-mgo-bson |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Gustavo Niemeyer | Pending | ||
Review via email: mp+114659@code.launchpad.net |
Description of the change
Added Encoder, Decoder types and test.
To post a comment you must log in.
Unmerged revisions
- 143. By John Asmuth
-
some people just don't appreciate named returns these days
- 142. By John Asmuth
-
forgot to fix import path back
- 141. By John Asmuth
-
added Encoder and Decoder types, and a test
Reviewers: mp+114659_ code.launchpad. net,
Message:
Please take a look.
Description:
Added Encoder, Decoder types and test.
https:/ /code.launchpad .net/~jasmuth/ mgo/skj- mgo-bson/ +merge/ 114659
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/6346093/
Affected files:
A [revision details]
M bson/bson.go
M bson/bson_test.go
M bson/decode.go
M bson/encode.go
Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>
Index: bson/bson.go ).UnixNano( ) / 1e6 * 1e6) ).UnixNano( )/1e6*1e6)
=== modified file 'bson/bson.go'
--- bson/bson.go 2012-03-26 17:54:06 +0000
+++ bson/bson.go 2012-07-11 21:14:05 +0000
@@ -294,7 +294,7 @@
// why this function exists. Using the time.Now function also works fine
// otherwise.
func Now() time.Time {
- return time.Unix(0, time.Now(
+ return time.Unix(0, time.Now(
}
// MongoTimestamp is a special internal type used by MongoDB that for some
Index: bson/bson_test.go
=== modified file 'bson/bson_test.go'
--- bson/bson_test.go 2012-06-22 20:01:34 +0000
+++ bson/bson_test.go 2012-07-11 21:14:05 +0000
@@ -28,11 +28,12 @@
package bson_test
import ( binary" net/gocheck" org/v2/ mgo/bson" eItems( c *C) { ([]byte{ }) (buf) item.obj) string( data), Equals, item.data, Commentf("Failed on item %d", eItems( c *C) { ([]byte( item.data) ) (buf)
+ "bytes"
"encoding/
"encoding/json"
"errors"
. "launchpad.
- "labix.
+ "skj-mgo-bson/bson"
"net/url"
"reflect"
"testing"
@@ -1307,3 +1308,25 @@
panic(err)
}
}
+
+func (s *S) TestEncodeSampl
+ for i, item := range sampleItems {
+ buf := bytes.NewBuffer
+ enc := bson.NewEncoder
+ err := enc.Encode(
+ data := buf.Bytes()
+ c.Assert(err, IsNil)
+ c.Assert(
i))
+ }
+}
+
+func (s *S) TestDecodeSampl
+ for i, item := range sampleItems {
+ value := bson.M{}
+ buf := bytes.NewReader
+ dec := bson.NewDecoder
+ err := dec.Decode(value)
+ c.Assert(err, IsNil)
+ c.Assert(value, DeepEquals, item.obj, Commentf("Failed on item %d", i))
+ }
+}
Index: bson/decode.go
=== modified file 'bson/decode.go'
--- bson/decode.go 2012-04-07 19:22:56 +0000
+++ bson/decode.go 2012-07-11 21:14:05 +0000
@@ -30,9 +30,11 @@
import (
"fmt"
"math"
+ "io"
"net/url"
"reflect"
"sync"
+ "errors"
"time"
)
@@ -701,3 +703,42 @@ New("Corrupted BSON stream")
}
return d.in[start : start+int(length)]
}
+
+type Decoder struct {
+ r io.Reader
+}
+
+func NewDecoder(r io.Reader) (d *Decoder) {
+ d = &Decoder{
+ r: r,
+ }
+ return
+}
+
+func (d *Decoder) Decode(pv interface{}) (err error) {
+ var lbuf [4]byte
+ n, err := d.r.Read(lbuf[:])
+ if n != 4 {
+ err = errors.
+ return
+ }
+ if err != nil {
+ return
+ }
+
+ length := (int(lbuf[0]) << 0) |
+ (int(lbuf[1]) << 8) |
+ (int(lbuf[2]) << 16) |
+ (int(lbuf[3]) << 24)
+
+ buf := make([]byte, length)
+ copy(buf[0:4], lbuf[:])
+ _, err = d.r.Read(buf[4:])
+ if err != nil {
+ return
+ }
+
+ err = Unmar...