mgo

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
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.
Revision history for this message
John Asmuth (jasmuth) wrote :
Download full text (4.0 KiB)

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 = Unmar...

Read more...

lp:~jasmuth/mgo/skj-mgo-bson updated
142. By John Asmuth

forgot to fix import path back

Revision history for this message
John Asmuth (jasmuth) wrote :
Download full text (4.1 KiB)

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/6348099/

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-12 15:08:31 +0000
@@ -28,11 +28,12 @@
  package bson_test

  import (
+ "bytes"
   "encoding/binary"
   "encoding/json"
   "errors"
   . "launchpad.net/gocheck"
- "labix.org/v2/mgo/bson"
+ "launchpad.net/mgo/v2/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...

Read more...

Revision history for this message
John Asmuth (jasmuth) wrote :

On 2012/07/12 15:06:00, skelterjohn wrote:
> Please take a look.

I meant to update this proposal, but it created a new one. Please ignore
this one, sorry.

https://codereview.appspot.com/6346093/

Revision history for this message
Aram Hăvărneanu (aramh) wrote :

https://codereview.appspot.com/6348099/diff/1/bson/decode.go
File bson/decode.go (right):

https://codereview.appspot.com/6348099/diff/1/bson/decode.go#newcode715
bson/decode.go:715: return
remove named return and replace the body with:

return &Decoder{r: r}

https://codereview.appspot.com/6348099/diff/1/bson/encode.go
File bson/encode.go (right):

https://codereview.appspot.com/6348099/diff/1/bson/encode.go#newcode432
bson/encode.go:432: return
Ditto as for NewDecoder.

https://codereview.appspot.com/6348099/

lp:~jasmuth/mgo/skj-mgo-bson updated
143. By John Asmuth

some people just don't appreciate named returns these days

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

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bson/bson.go'
--- bson/bson.go 2012-03-26 17:54:06 +0000
+++ bson/bson.go 2012-07-12 15:29:37 +0000
@@ -294,7 +294,7 @@
294// why this function exists. Using the time.Now function also works fine294// why this function exists. Using the time.Now function also works fine
295// otherwise.295// otherwise.
296func Now() time.Time {296func Now() time.Time {
297 return time.Unix(0, time.Now().UnixNano() / 1e6 * 1e6)297 return time.Unix(0, time.Now().UnixNano()/1e6*1e6)
298}298}
299299
300// MongoTimestamp is a special internal type used by MongoDB that for some300// MongoTimestamp is a special internal type used by MongoDB that for some
301301
=== modified file 'bson/bson_test.go'
--- bson/bson_test.go 2012-06-22 20:01:34 +0000
+++ bson/bson_test.go 2012-07-12 15:29:37 +0000
@@ -28,11 +28,12 @@
28package bson_test28package bson_test
2929
30import (30import (
31 "bytes"
31 "encoding/binary"32 "encoding/binary"
32 "encoding/json"33 "encoding/json"
33 "errors"34 "errors"
34 . "launchpad.net/gocheck"35 . "launchpad.net/gocheck"
35 "labix.org/v2/mgo/bson"36 "launchpad.net/mgo/v2/bson"
36 "net/url"37 "net/url"
37 "reflect"38 "reflect"
38 "testing"39 "testing"
@@ -1307,3 +1308,25 @@
1307 panic(err)1308 panic(err)
1308 }1309 }
1309}1310}
1311
1312func (s *S) TestEncodeSampleItems(c *C) {
1313 for i, item := range sampleItems {
1314 buf := bytes.NewBuffer([]byte{})
1315 enc := bson.NewEncoder(buf)
1316 err := enc.Encode(item.obj)
1317 data := buf.Bytes()
1318 c.Assert(err, IsNil)
1319 c.Assert(string(data), Equals, item.data, Commentf("Failed on item %d", i))
1320 }
1321}
1322
1323func (s *S) TestDecodeSampleItems(c *C) {
1324 for i, item := range sampleItems {
1325 value := bson.M{}
1326 buf := bytes.NewReader([]byte(item.data))
1327 dec := bson.NewDecoder(buf)
1328 err := dec.Decode(value)
1329 c.Assert(err, IsNil)
1330 c.Assert(value, DeepEquals, item.obj, Commentf("Failed on item %d", i))
1331 }
1332}
13101333
=== modified file 'bson/decode.go'
--- bson/decode.go 2012-04-07 19:22:56 +0000
+++ bson/decode.go 2012-07-12 15:29:37 +0000
@@ -30,9 +30,11 @@
30import (30import (
31 "fmt"31 "fmt"
32 "math"32 "math"
33 "io"
33 "net/url"34 "net/url"
34 "reflect"35 "reflect"
35 "sync"36 "sync"
37 "errors"
36 "time"38 "time"
37)39)
3840
@@ -701,3 +703,39 @@
701 }703 }
702 return d.in[start : start+int(length)]704 return d.in[start : start+int(length)]
703}705}
706
707type Decoder struct {
708 r io.Reader
709}
710
711func NewDecoder(r io.Reader) *Decoder {
712 return &Decoder{r: r}
713}
714
715func (d *Decoder) Decode(pv interface{}) (err error) {
716 var lbuf [4]byte
717 n, err := d.r.Read(lbuf[:])
718 if n != 4 {
719 err = errors.New("Corrupted BSON stream")
720 return
721 }
722 if err != nil {
723 return
724 }
725
726 length := (int(lbuf[0]) << 0) |
727 (int(lbuf[1]) << 8) |
728 (int(lbuf[2]) << 16) |
729 (int(lbuf[3]) << 24)
730
731 buf := make([]byte, length)
732 copy(buf[0:4], lbuf[:])
733 _, err = d.r.Read(buf[4:])
734 if err != nil {
735 return
736 }
737
738 err = Unmarshal(buf, pv)
739
740 return
741}
704742
=== modified file 'bson/encode.go'
--- bson/encode.go 2012-04-07 19:22:56 +0000
+++ bson/encode.go 2012-07-12 15:29:37 +0000
@@ -28,6 +28,7 @@
28package bson28package bson
2929
30import (30import (
31 "io"
31 "math"32 "math"
32 "net/url"33 "net/url"
33 "reflect"34 "reflect"
@@ -347,7 +348,7 @@
347 case time.Time:348 case time.Time:
348 // MongoDB handles timestamps as milliseconds.349 // MongoDB handles timestamps as milliseconds.
349 e.addElemName('\x09', name)350 e.addElemName('\x09', name)
350 e.addInt64(s.Unix() * 1000 + int64(s.Nanosecond() / 1e6))351 e.addInt64(s.Unix()*1000 + int64(s.Nanosecond()/1e6))
351352
352 case url.URL:353 case url.URL:
353 e.addElemName('\x02', name)354 e.addElemName('\x02', name)
@@ -419,3 +420,20 @@
419func (e *encoder) addBytes(v ...byte) {420func (e *encoder) addBytes(v ...byte) {
420 e.out = append(e.out, v...)421 e.out = append(e.out, v...)
421}422}
423
424type Encoder struct {
425 w io.Writer
426}
427
428func NewEncoder(w io.Writer) *Encoder {
429 return &Encoder{w: w}
430}
431
432func (e *Encoder) Encode(v interface{}) (err error) {
433 buf, err := Marshal(v)
434 if err != nil {
435 return
436 }
437 _, err = e.w.Write(buf)
438 return
439}

Subscribers

People subscribed via source and target branches

to all changes: