Merge lp:~niemeyer/juju-core/fix-tools-nil-roundtrip into lp:~juju/juju-core/trunk

Proposed by Gustavo Niemeyer
Status: Merged
Merged at revision: 586
Proposed branch: lp:~niemeyer/juju-core/fix-tools-nil-roundtrip
Merge into: lp:~juju/juju-core/trunk
Diff against target: 40 lines (+18/-0)
2 files modified
state/state.go (+7/-0)
state/tools_test.go (+11/-0)
To merge this branch: bzr merge lp:~niemeyer/juju-core/fix-tools-nil-roundtrip
Reviewer Review Type Date Requested Status
The Go Language Gophers Pending
Review via email: mp+126546@code.launchpad.net

Description of the change

state: fix Tools nil marshal roundtrip

https://codereview.appspot.com/6574051/

To post a comment you must log in.
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Reviewers: mp+126546_code.launchpad.net,

Message:
Please take a look.

Description:
state: fix Tools nil marshal roundtrip

https://code.launchpad.net/~niemeyer/juju-core/fix-tools-nil-roundtrip/+merge/126546

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/6574051/

Affected files:
   A [revision details]
   M state/state.go
   M state/tools_test.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: state/state.go
=== modified file 'state/state.go'
--- state/state.go 2012-09-26 02:02:34 +0000
+++ state/state.go 2012-09-26 20:48:54 +0000
@@ -33,10 +33,17 @@
  }

  func (t *Tools) GetBSON() (interface{}, error) {
+ if t == nil {
+ return nil, nil
+ }
   return &toolsDoc{t.Binary, t.URL}, nil
  }

  func (t *Tools) SetBSON(raw bson.Raw) error {
+ if raw.Kind == 10 {
+ // Preserve the nil value in that case.
+ return bson.SetZero
+ }
   var doc toolsDoc
   if err := raw.Unmarshal(&doc); err != nil {
    return err

Index: state/tools_test.go
=== modified file 'state/tools_test.go'
--- state/tools_test.go 2012-09-26 02:02:34 +0000
+++ state/tools_test.go 2012-09-26 20:52:06 +0000
@@ -91,3 +91,14 @@
   c.Assert(err, IsNil)
   c.Assert(t, Equals, *tools)
  }
+
+func (s *ToolsSuite) TestUnmarshalNilRoundtrip(c *C) {
+ // We have a custom unmarshaller that should keep
+ // the field unset when it finds a nil value.
+ var v struct{ Tools *state.Tools }
+ data, err := bson.Marshal(&v)
+ c.Assert(err, IsNil)
+ err = bson.Unmarshal(data, &v)
+ c.Assert(err, IsNil)
+ c.Assert(v.Tools, IsNil)
+}

Revision history for this message
William Reade (fwereade) wrote :
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

*** Submitted:

state: fix Tools nil marshal roundtrip

R=fwereade
CC=
https://codereview.appspot.com/6574051

https://codereview.appspot.com/6574051/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'state/state.go'
--- state/state.go 2012-09-26 02:02:34 +0000
+++ state/state.go 2012-09-26 20:54:43 +0000
@@ -33,10 +33,17 @@
33}33}
3434
35func (t *Tools) GetBSON() (interface{}, error) {35func (t *Tools) GetBSON() (interface{}, error) {
36 if t == nil {
37 return nil, nil
38 }
36 return &toolsDoc{t.Binary, t.URL}, nil39 return &toolsDoc{t.Binary, t.URL}, nil
37}40}
3841
39func (t *Tools) SetBSON(raw bson.Raw) error {42func (t *Tools) SetBSON(raw bson.Raw) error {
43 if raw.Kind == 10 {
44 // Preserve the nil value in that case.
45 return bson.SetZero
46 }
40 var doc toolsDoc47 var doc toolsDoc
41 if err := raw.Unmarshal(&doc); err != nil {48 if err := raw.Unmarshal(&doc); err != nil {
42 return err49 return err
4350
=== modified file 'state/tools_test.go'
--- state/tools_test.go 2012-09-26 02:02:34 +0000
+++ state/tools_test.go 2012-09-26 20:54:43 +0000
@@ -91,3 +91,14 @@
91 c.Assert(err, IsNil)91 c.Assert(err, IsNil)
92 c.Assert(t, Equals, *tools)92 c.Assert(t, Equals, *tools)
93}93}
94
95func (s *ToolsSuite) TestUnmarshalNilRoundtrip(c *C) {
96 // We have a custom unmarshaller that should keep
97 // the field unset when it finds a nil value.
98 var v struct{ Tools *state.Tools }
99 data, err := bson.Marshal(&v)
100 c.Assert(err, IsNil)
101 err = bson.Unmarshal(data, &v)
102 c.Assert(err, IsNil)
103 c.Assert(v.Tools, IsNil)
104}

Subscribers

People subscribed via source and target branches