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
1=== modified file 'bson/bson.go'
2--- bson/bson.go 2012-03-26 17:54:06 +0000
3+++ bson/bson.go 2012-07-12 15:29:37 +0000
4@@ -294,7 +294,7 @@
5 // why this function exists. Using the time.Now function also works fine
6 // otherwise.
7 func Now() time.Time {
8- return time.Unix(0, time.Now().UnixNano() / 1e6 * 1e6)
9+ return time.Unix(0, time.Now().UnixNano()/1e6*1e6)
10 }
11
12 // MongoTimestamp is a special internal type used by MongoDB that for some
13
14=== modified file 'bson/bson_test.go'
15--- bson/bson_test.go 2012-06-22 20:01:34 +0000
16+++ bson/bson_test.go 2012-07-12 15:29:37 +0000
17@@ -28,11 +28,12 @@
18 package bson_test
19
20 import (
21+ "bytes"
22 "encoding/binary"
23 "encoding/json"
24 "errors"
25 . "launchpad.net/gocheck"
26- "labix.org/v2/mgo/bson"
27+ "launchpad.net/mgo/v2/bson"
28 "net/url"
29 "reflect"
30 "testing"
31@@ -1307,3 +1308,25 @@
32 panic(err)
33 }
34 }
35+
36+func (s *S) TestEncodeSampleItems(c *C) {
37+ for i, item := range sampleItems {
38+ buf := bytes.NewBuffer([]byte{})
39+ enc := bson.NewEncoder(buf)
40+ err := enc.Encode(item.obj)
41+ data := buf.Bytes()
42+ c.Assert(err, IsNil)
43+ c.Assert(string(data), Equals, item.data, Commentf("Failed on item %d", i))
44+ }
45+}
46+
47+func (s *S) TestDecodeSampleItems(c *C) {
48+ for i, item := range sampleItems {
49+ value := bson.M{}
50+ buf := bytes.NewReader([]byte(item.data))
51+ dec := bson.NewDecoder(buf)
52+ err := dec.Decode(value)
53+ c.Assert(err, IsNil)
54+ c.Assert(value, DeepEquals, item.obj, Commentf("Failed on item %d", i))
55+ }
56+}
57
58=== modified file 'bson/decode.go'
59--- bson/decode.go 2012-04-07 19:22:56 +0000
60+++ bson/decode.go 2012-07-12 15:29:37 +0000
61@@ -30,9 +30,11 @@
62 import (
63 "fmt"
64 "math"
65+ "io"
66 "net/url"
67 "reflect"
68 "sync"
69+ "errors"
70 "time"
71 )
72
73@@ -701,3 +703,39 @@
74 }
75 return d.in[start : start+int(length)]
76 }
77+
78+type Decoder struct {
79+ r io.Reader
80+}
81+
82+func NewDecoder(r io.Reader) *Decoder {
83+ return &Decoder{r: r}
84+}
85+
86+func (d *Decoder) Decode(pv interface{}) (err error) {
87+ var lbuf [4]byte
88+ n, err := d.r.Read(lbuf[:])
89+ if n != 4 {
90+ err = errors.New("Corrupted BSON stream")
91+ return
92+ }
93+ if err != nil {
94+ return
95+ }
96+
97+ length := (int(lbuf[0]) << 0) |
98+ (int(lbuf[1]) << 8) |
99+ (int(lbuf[2]) << 16) |
100+ (int(lbuf[3]) << 24)
101+
102+ buf := make([]byte, length)
103+ copy(buf[0:4], lbuf[:])
104+ _, err = d.r.Read(buf[4:])
105+ if err != nil {
106+ return
107+ }
108+
109+ err = Unmarshal(buf, pv)
110+
111+ return
112+}
113
114=== modified file 'bson/encode.go'
115--- bson/encode.go 2012-04-07 19:22:56 +0000
116+++ bson/encode.go 2012-07-12 15:29:37 +0000
117@@ -28,6 +28,7 @@
118 package bson
119
120 import (
121+ "io"
122 "math"
123 "net/url"
124 "reflect"
125@@ -347,7 +348,7 @@
126 case time.Time:
127 // MongoDB handles timestamps as milliseconds.
128 e.addElemName('\x09', name)
129- e.addInt64(s.Unix() * 1000 + int64(s.Nanosecond() / 1e6))
130+ e.addInt64(s.Unix()*1000 + int64(s.Nanosecond()/1e6))
131
132 case url.URL:
133 e.addElemName('\x02', name)
134@@ -419,3 +420,20 @@
135 func (e *encoder) addBytes(v ...byte) {
136 e.out = append(e.out, v...)
137 }
138+
139+type Encoder struct {
140+ w io.Writer
141+}
142+
143+func NewEncoder(w io.Writer) *Encoder {
144+ return &Encoder{w: w}
145+}
146+
147+func (e *Encoder) Encode(v interface{}) (err error) {
148+ buf, err := Marshal(v)
149+ if err != nil {
150+ return
151+ }
152+ _, err = e.w.Write(buf)
153+ return
154+}

Subscribers

People subscribed via source and target branches

to all changes: