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 === 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().UnixNano() / 1e6 * 1e6) + return time.Unix(0, time.Now().UnixNano()/1e6*1e6) }
// 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 ( + "bytes" "encoding/binary" "encoding/json" "errors" . "launchpad.net/gocheck" - "labix.org/v2/mgo/bson" + "skj-mgo-bson/bson" "net/url" "reflect" "testing" @@ -1307,3 +1308,25 @@ panic(err) } } + +func (s *S) TestEncodeSampleItems(c *C) { + for i, item := range sampleItems { + buf := bytes.NewBuffer([]byte{}) + enc := bson.NewEncoder(buf) + err := enc.Encode(item.obj) + data := buf.Bytes() + c.Assert(err, IsNil) + c.Assert(string(data), Equals, item.data, Commentf("Failed on item %d", i)) + } +} + +func (s *S) TestDecodeSampleItems(c *C) { + for i, item := range sampleItems { + value := bson.M{} + buf := bytes.NewReader([]byte(item.data)) + dec := bson.NewDecoder(buf) + 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 @@ } 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.New("Corrupted BSON stream") + 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 = Unmarshal(buf, pv) + + return +}
Index: bson/encode.go === modified file 'bson/encode.go' --- bson/encode.go 2012-04-07 19:22:56 +0000 +++ bson/encode.go 2012-07-11 21:14:05 +0000 @@ -28,6 +28,7 @@ package bson
import ( + "io" "math" "net/url" "reflect" @@ -347,7 +348,7 @@ case time.Time: // MongoDB handles timestamps as milliseconds. e.addElemName('\x09', name) - e.addInt64(s.Unix() * 1000 + int64(s.Nanosecond() / 1e6)) + e.addInt64(s.Unix()*1000 + int64(s.Nanosecond()/1e6))
case url.URL: e.addElemName('\x02', name) @@ -419,3 +420,23 @@ func (e *encoder) addBytes(v ...byte) { e.out = append(e.out, v...) } + +type Encoder struct { + w io.Writer +} + +func NewEncoder(w io.Writer) (e *Encoder) { + e = &Encoder{ + w: w, + } + return +} + +func (e *Encoder) Encode(v interface{}) (err error) { + buf, err := Marshal(v) + if err != nil { + return + } + _, err = e.w.Write(buf) + return +}
« Back to merge proposal
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 = Unmarshal(buf, pv)
+
+ return
+}
Index: bson/encode.go
=== modified file 'bson/encode.go'
--- bson/encode.go 2012-04-07 19:22:56 +0000
+++ bson/encode.go 2012-07-11 21:14:05 +0000
@@ -28,6 +28,7 @@
package bson
import ( addElemName( '\x09', name) Nanosecond( ) / 1e6)) s.Unix( )*1000 + int64(s. Nanosecond( )/1e6))
+ "io"
"math"
"net/url"
"reflect"
@@ -347,7 +348,7 @@
case time.Time:
// MongoDB handles timestamps as milliseconds.
e.
- e.addInt64(s.Unix() * 1000 + int64(s.
+ e.addInt64(
case url.URL: addElemName( '\x02', name)
e.
@@ -419,3 +420,23 @@
func (e *encoder) addBytes(v ...byte) {
e.out = append(e.out, v...)
}
+
+type Encoder struct {
+ w io.Writer
+}
+
+func NewEncoder(w io.Writer) (e *Encoder) {
+ e = &Encoder{
+ w: w,
+ }
+ return
+}
+
+func (e *Encoder) Encode(v interface{}) (err error) {
+ buf, err := Marshal(v)
+ if err != nil {
+ return
+ }
+ _, err = e.w.Write(buf)
+ return
+}