Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't return typed nil interface values #46

Merged
merged 1 commit into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (d *device) IPAddresses() []string {

// Zone implements Device.
func (d *device) Zone() Zone {
if d.zone == nil {
return nil
}
return d.zone
}

Expand Down
5 changes: 5 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type deviceSuite struct {

var _ = gc.Suite(&deviceSuite{})

func (*deviceSuite) TestNilZone(c *gc.C) {
var empty device
c.Check(empty.Zone() == nil, jc.IsTrue)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternatively:

var zeroDevice device
c.Check(zeroDevice.Zone(), gc.IsNil)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that first, but it uses reflection so didn't fail before I fixed the problem.

}

func (*deviceSuite) TestReadDevicesBadSchema(c *gc.C) {
_, err := readDevices(twoDotOh, "wat?")
c.Check(err, jc.Satisfies, IsDeserializationError)
Expand Down
3 changes: 3 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (i *interface_) Tags() []string {

// VLAN implements Interface.
func (i *interface_) VLAN() VLAN {
if i.vlan == nil {
return nil
}
return i.vlan
}

Expand Down
5 changes: 5 additions & 0 deletions interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type interfaceSuite struct {

var _ = gc.Suite(&interfaceSuite{})

func (*interfaceSuite) TestNilVLAN(c *gc.C) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

var empty interface_
c.Check(empty.VLAN() == nil, jc.IsTrue)
}

func (*interfaceSuite) TestReadInterfacesBadSchema(c *gc.C) {
_, err := readInterfaces(twoDotOh, "wat?")
c.Check(err, jc.Satisfies, IsDeserializationError)
Expand Down
3 changes: 3 additions & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (k *link) Mode() string {

// Subnet implements Link.
func (k *link) Subnet() Subnet {
if k.subnet == nil {
return nil
}
return k.subnet
}

Expand Down
5 changes: 5 additions & 0 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type linkSuite struct{}

var _ = gc.Suite(&linkSuite{})

func (*linkSuite) TestNilSubnet(c *gc.C) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

var empty link
c.Check(empty.Subnet() == nil, jc.IsTrue)
}

func (*linkSuite) TestReadLinksBadSchema(c *gc.C) {
_, err := readLinks(twoDotOh, "wat?")
c.Check(err, jc.Satisfies, IsDeserializationError)
Expand Down
6 changes: 6 additions & 0 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,17 @@ func (m *machine) PowerState() string {

// Zone implements Machine.
func (m *machine) Zone() Zone {
if m.zone == nil {
return nil
}
return m.zone
}

// BootInterface implements Machine.
func (m *machine) BootInterface() Interface {
if m.bootInterface == nil {
return nil
}
m.bootInterface.controller = m.controller
return m.bootInterface
}
Expand Down
8 changes: 8 additions & 0 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ type machineSuite struct {

var _ = gc.Suite(&machineSuite{})

func (*machineSuite) TestNilGetters(c *gc.C) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise here

var empty machine
c.Check(empty.Zone() == nil, jc.IsTrue)
c.Check(empty.PhysicalBlockDevice(0) == nil, jc.IsTrue)
c.Check(empty.Interface(0) == nil, jc.IsTrue)
c.Check(empty.BootInterface() == nil, jc.IsTrue)
}

func (*machineSuite) TestReadMachinesBadSchema(c *gc.C) {
_, err := readMachines(twoDotOh, "wat?")
c.Check(err, jc.Satisfies, IsDeserializationError)
Expand Down
3 changes: 3 additions & 0 deletions partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (p *partition) Path() string {

// FileSystem implements Partition.
func (p *partition) FileSystem() FileSystem {
if p.filesystem == nil {
return nil
}
return p.filesystem
}

Expand Down
5 changes: 5 additions & 0 deletions partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type partitionSuite struct{}

var _ = gc.Suite(&partitionSuite{})

func (*partitionSuite) TestNilFileSystem(c *gc.C) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

var empty partition
c.Assert(empty.FileSystem() == nil, jc.IsTrue)
}

func (*partitionSuite) TestReadPartitionsBadSchema(c *gc.C) {
_, err := readPartitions(twoDotOh, "wat?")
c.Check(err, jc.Satisfies, IsDeserializationError)
Expand Down
3 changes: 3 additions & 0 deletions subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (s *subnet) Space() string {

// VLAN implements Subnet.
func (s *subnet) VLAN() VLAN {
if s.vlan == nil {
return nil
}
return s.vlan
}

Expand Down
5 changes: 5 additions & 0 deletions subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type subnetSuite struct{}

var _ = gc.Suite(&subnetSuite{})

func (*subnetSuite) TestNilVLAN(c *gc.C) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and here

var empty subnet
c.Check(empty.VLAN() == nil, jc.IsTrue)
}

func (*subnetSuite) TestReadSubnetsBadSchema(c *gc.C) {
_, err := readSubnets(twoDotOh, "wat?")
c.Assert(err.Error(), gc.Equals, `subnet base schema check failed: expected list, got string("wat?")`)
Expand Down