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
1=== modified file 'state/state.go'
2--- state/state.go 2012-09-26 02:02:34 +0000
3+++ state/state.go 2012-09-26 20:54:43 +0000
4@@ -33,10 +33,17 @@
5 }
6
7 func (t *Tools) GetBSON() (interface{}, error) {
8+ if t == nil {
9+ return nil, nil
10+ }
11 return &toolsDoc{t.Binary, t.URL}, nil
12 }
13
14 func (t *Tools) SetBSON(raw bson.Raw) error {
15+ if raw.Kind == 10 {
16+ // Preserve the nil value in that case.
17+ return bson.SetZero
18+ }
19 var doc toolsDoc
20 if err := raw.Unmarshal(&doc); err != nil {
21 return err
22
23=== modified file 'state/tools_test.go'
24--- state/tools_test.go 2012-09-26 02:02:34 +0000
25+++ state/tools_test.go 2012-09-26 20:54:43 +0000
26@@ -91,3 +91,14 @@
27 c.Assert(err, IsNil)
28 c.Assert(t, Equals, *tools)
29 }
30+
31+func (s *ToolsSuite) TestUnmarshalNilRoundtrip(c *C) {
32+ // We have a custom unmarshaller that should keep
33+ // the field unset when it finds a nil value.
34+ var v struct{ Tools *state.Tools }
35+ data, err := bson.Marshal(&v)
36+ c.Assert(err, IsNil)
37+ err = bson.Unmarshal(data, &v)
38+ c.Assert(err, IsNil)
39+ c.Assert(v.Tools, IsNil)
40+}

Subscribers

People subscribed via source and target branches