Skip to content

Commit

Permalink
core: no longer return slices of errors, instead use multierror
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Nov 7, 2016
1 parent a93bc5f commit 08874e4
Show file tree
Hide file tree
Showing 42 changed files with 213 additions and 237 deletions.
4 changes: 2 additions & 2 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ type Adaptor interface {
// SetName sets the label for the Adaptor
SetName(n string)
// Connect initiates the Adaptor
Connect() []error
Connect() error
// Finalize terminates the Adaptor
Finalize() []error
Finalize() error
}

// Porter is the interface that describes an adaptor's port
Expand Down
18 changes: 9 additions & 9 deletions api/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type testDriver struct {
gobot.Eventer
}

func (t *testDriver) Start() (errs []error) { return }
func (t *testDriver) Halt() (errs []error) { return }
func (t *testDriver) Start() (err error) { return }
func (t *testDriver) Halt() (err error) { return }
func (t *testDriver) Name() string { return t.name }
func (t *testDriver) SetName(n string) { t.name = n }
func (t *testDriver) Pin() string { return t.pin }
Expand Down Expand Up @@ -64,14 +64,14 @@ type testAdaptor struct {
port string
}

var testAdaptorConnect = func() (errs []error) { return }
var testAdaptorFinalize = func() (errs []error) { return }
var testAdaptorConnect = func() (err error) { return }
var testAdaptorFinalize = func() (err error) { return }

func (t *testAdaptor) Finalize() (errs []error) { return testAdaptorFinalize() }
func (t *testAdaptor) Connect() (errs []error) { return testAdaptorConnect() }
func (t *testAdaptor) Name() string { return t.name }
func (t *testAdaptor) SetName(n string) { t.name = n }
func (t *testAdaptor) Port() string { return t.port }
func (t *testAdaptor) Finalize() (err error) { return testAdaptorFinalize() }
func (t *testAdaptor) Connect() (err error) { return testAdaptorConnect() }
func (t *testAdaptor) Name() string { return t.name }
func (t *testAdaptor) SetName(n string) { t.name = n }
func (t *testAdaptor) Port() string { return t.port }

func newTestAdaptor(name string, port string) *testAdaptor {
return &testAdaptor{
Expand Down
8 changes: 4 additions & 4 deletions cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Name() string { return {{.FirstLe
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) SetName(name string) { {{.FirstLetter}}.name = name }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Connect() []error { return nil }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Connect() error { return nil }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Finalize() []error { return nil }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Finalize() error { return nil }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Ping() string { return "pong" }
`
Expand Down Expand Up @@ -242,7 +242,7 @@ func ({{.FirstLetter}} *{{.UpperName}}Driver) Ping() string {
return {{.FirstLetter}}.adaptor().Ping()
}
func ({{.FirstLetter}} *{{.UpperName}}Driver) Start() []error {
func ({{.FirstLetter}} *{{.UpperName}}Driver) Start() error {
go func() {
for {
{{.FirstLetter}}.Publish({{.FirstLetter}}.Event(Hello), {{.FirstLetter}}.Hello())
Expand All @@ -257,7 +257,7 @@ func ({{.FirstLetter}} *{{.UpperName}}Driver) Start() []error {
return nil
}
func ({{.FirstLetter}} *{{.UpperName}}Driver) Halt() []error {
func ({{.FirstLetter}} *{{.UpperName}}Driver) Halt() error {
{{.FirstLetter}}.halt <- true
return nil
}
Expand Down
25 changes: 10 additions & 15 deletions connection.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package gobot

import (
"fmt"
"log"
"reflect"

multierror "github.com/hashicorp/go-multierror"
)

// JSONConnection is a JSON representation of a Connection.
Expand Down Expand Up @@ -39,7 +40,7 @@ func (c *Connections) Each(f func(Connection)) {
}

// Start calls Connect on each Connection in c
func (c *Connections) Start() (errs []error) {
func (c *Connections) Start() (err error) {
log.Println("Starting connections...")
for _, connection := range *c {
info := "Starting connection " + connection.Name()
Expand All @@ -50,25 +51,19 @@ func (c *Connections) Start() (errs []error) {

log.Println(info + "...")

if errs = connection.Connect(); len(errs) > 0 {
for i, err := range errs {
errs[i] = fmt.Errorf("Connection %q: %v", connection.Name(), err)
}
return
if cerr := connection.Connect(); cerr != nil {
err = multierror.Append(err, cerr)
}
}
return
return err
}

// Finalize calls Finalize on each Connection in c
func (c *Connections) Finalize() (errs []error) {
func (c *Connections) Finalize() (err error) {
for _, connection := range *c {
if cerrs := connection.Finalize(); cerrs != nil {
for i, err := range cerrs {
cerrs[i] = fmt.Errorf("Connection %q: %v", connection.Name(), err)
}
errs = append(errs, cerrs...)
if cerr := connection.Finalize(); cerr != nil {
err = multierror.Append(err, cerr)
}
}
return errs
return err
}
25 changes: 10 additions & 15 deletions device.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package gobot

import (
"fmt"
"log"
"reflect"

multierror "github.com/hashicorp/go-multierror"
)

// JSONDevice is a JSON representation of a Device.
Expand Down Expand Up @@ -52,7 +53,7 @@ func (d *Devices) Each(f func(Device)) {
}

// Start calls Start on each Device in d
func (d *Devices) Start() (errs []error) {
func (d *Devices) Start() (err error) {
log.Println("Starting devices...")
for _, device := range *d {
info := "Starting device " + device.Name()
Expand All @@ -62,25 +63,19 @@ func (d *Devices) Start() (errs []error) {
}

log.Println(info + "...")
if errs = device.Start(); len(errs) > 0 {
for i, err := range errs {
errs[i] = fmt.Errorf("Device %q: %v", device.Name(), err)
}
return
if derr := device.Start(); derr != nil {
err = multierror.Append(err, derr)
}
}
return
return err
}

// Halt calls Halt on each Device in d
func (d *Devices) Halt() (errs []error) {
func (d *Devices) Halt() (err error) {
for _, device := range *d {
if derrs := device.Halt(); len(derrs) > 0 {
for i, err := range derrs {
derrs[i] = fmt.Errorf("Device %q: %v", device.Name(), err)
}
errs = append(errs, derrs...)
if derr := device.Halt(); derr != nil {
err = multierror.Append(err, derr)
}
}
return
return err
}
4 changes: 2 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ type Driver interface {
// SetName sets the label for the Driver
SetName(s string)
// Start initiates the Driver
Start() []error
Start() error
// Halt terminates the Driver
Halt() []error
Halt() error
// Connection returns the Connection assiciated with the Driver
Connection() Connection
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewAnalogSensorDriver(a AnalogReader, pin string, v ...time.Duration) *Anal
// Emits the Events:
// Data int - Event is emitted on change and represents the current reading from the sensor.
// Error error - Event is emitted on error reading from the sensor.
func (a *AnalogSensorDriver) Start() (errs []error) {
func (a *AnalogSensorDriver) Start() (err error) {
value := 0
go func() {
timer := time.NewTimer(a.interval)
Expand All @@ -82,7 +82,7 @@ func (a *AnalogSensorDriver) Start() (errs []error) {
}

// Halt stops polling the analog sensor for new information
func (a *AnalogSensorDriver) Halt() (errs []error) {
func (a *AnalogSensorDriver) Halt() (err error) {
a.halt <- true
return
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewButtonDriver(a DigitalReader, pin string, v ...time.Duration) *ButtonDri
// Push int - On button push
// Release int - On button release
// Error error - On button error
func (b *ButtonDriver) Start() (errs []error) {
func (b *ButtonDriver) Start() (err error) {
state := 0
go func() {
for {
Expand All @@ -72,7 +72,7 @@ func (b *ButtonDriver) Start() (errs []error) {
}

// Halt stops polling the button for new information
func (b *ButtonDriver) Halt() (errs []error) {
func (b *ButtonDriver) Halt() (err error) {
b.halt <- true
return
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/buzzer_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ func NewBuzzerDriver(a DigitalWriter, pin string) *BuzzerDriver {
}

// Start implements the Driver interface
func (l *BuzzerDriver) Start() (errs []error) { return }
func (l *BuzzerDriver) Start() (err error) { return }

// Halt implements the Driver interface
func (l *BuzzerDriver) Halt() (errs []error) { return }
func (l *BuzzerDriver) Halt() (err error) { return }

// Name returns the BuzzerDrivers name
func (l *BuzzerDriver) Name() string { return l.name }
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/direct_pin_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func (d *DirectPinDriver) Pin() string { return d.pin }
func (d *DirectPinDriver) Connection() gobot.Connection { return d.connection }

// Start implements the Driver interface
func (d *DirectPinDriver) Start() (errs []error) { return }
func (d *DirectPinDriver) Start() (err error) { return }

// Halt implements the Driver interface
func (d *DirectPinDriver) Halt() (errs []error) { return }
func (d *DirectPinDriver) Halt() (err error) { return }

// Turn Off pin
func (d *DirectPinDriver) Off() (err error) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/grove_temperature_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewGroveTemperatureSensorDriver(a AnalogReader, pin string, v ...time.Durat
// Emits the Events:
// Data int - Event is emitted on change and represents the current temperature in celsius from the sensor.
// Error error - Event is emitted on error reading from the sensor.
func (a *GroveTemperatureSensorDriver) Start() (errs []error) {
func (a *GroveTemperatureSensorDriver) Start() (err error) {
thermistor := 3975.0
a.temperature = 0

Expand Down Expand Up @@ -80,7 +80,7 @@ func (a *GroveTemperatureSensorDriver) Start() (errs []error) {
}

// Halt stops polling the analog sensor for new information
func (a *GroveTemperatureSensorDriver) Halt() (errs []error) {
func (a *GroveTemperatureSensorDriver) Halt() (err error) {
a.halt <- true
return
}
Expand Down
18 changes: 9 additions & 9 deletions drivers/gpio/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package gpio

type gpioTestBareAdaptor struct{}

func (t *gpioTestBareAdaptor) Connect() (errs []error) { return }
func (t *gpioTestBareAdaptor) Finalize() (errs []error) { return }
func (t *gpioTestBareAdaptor) Name() string { return "" }
func (t *gpioTestBareAdaptor) SetName(n string) {}
func (t *gpioTestBareAdaptor) Connect() (err error) { return }
func (t *gpioTestBareAdaptor) Finalize() (err error) { return }
func (t *gpioTestBareAdaptor) Name() string { return "" }
func (t *gpioTestBareAdaptor) SetName(n string) {}

type gpioTestDigitalWriter struct {
gpioTestBareAdaptor
Expand Down Expand Up @@ -49,11 +49,11 @@ func (t *gpioTestAdaptor) AnalogRead(string) (val int, err error) {
func (t *gpioTestAdaptor) DigitalRead(string) (val int, err error) {
return testAdaptorDigitalRead()
}
func (t *gpioTestAdaptor) Connect() (errs []error) { return }
func (t *gpioTestAdaptor) Finalize() (errs []error) { return }
func (t *gpioTestAdaptor) Name() string { return t.name }
func (t *gpioTestAdaptor) SetName(n string) { t.name = n }
func (t *gpioTestAdaptor) Port() string { return t.port }
func (t *gpioTestAdaptor) Connect() (err error) { return }
func (t *gpioTestAdaptor) Finalize() (err error) { return }
func (t *gpioTestAdaptor) Name() string { return t.name }
func (t *gpioTestAdaptor) SetName(n string) { t.name = n }
func (t *gpioTestAdaptor) Port() string { return t.port }

func newGpioTestAdaptor() *gpioTestAdaptor {
return &gpioTestAdaptor{
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/led_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func NewLedDriver(a DigitalWriter, pin string) *LedDriver {
}

// Start implements the Driver interface
func (l *LedDriver) Start() (errs []error) { return }
func (l *LedDriver) Start() (err error) { return }

// Halt implements the Driver interface
func (l *LedDriver) Halt() (errs []error) { return }
func (l *LedDriver) Halt() (err error) { return }

// Name returns the LedDrivers name
func (l *LedDriver) Name() string { return l.name }
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/makey_button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (b *MakeyButtonDriver) Connection() gobot.Connection { return b.connection.
// Push int - On button push
// Release int - On button release
// Error error - On button error
func (b *MakeyButtonDriver) Start() (errs []error) {
func (b *MakeyButtonDriver) Start() (err error) {
state := 1
go func() {
timer := time.NewTimer(b.interval)
Expand Down Expand Up @@ -93,7 +93,7 @@ func (b *MakeyButtonDriver) Start() (errs []error) {
}

// Halt stops polling the makey button for new information
func (b *MakeyButtonDriver) Halt() (errs []error) {
func (b *MakeyButtonDriver) Halt() (err error) {
b.halt <- true
return
}
4 changes: 2 additions & 2 deletions drivers/gpio/motor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func (m *MotorDriver) SetName(n string) { m.name = n }
func (m *MotorDriver) Connection() gobot.Connection { return m.connection.(gobot.Connection) }

// Start implements the Driver interface
func (m *MotorDriver) Start() (errs []error) { return }
func (m *MotorDriver) Start() (err error) { return }

// Halt implements the Driver interface
func (m *MotorDriver) Halt() (errs []error) { return }
func (m *MotorDriver) Halt() (err error) { return }

// Off turns the motor off or sets the motor to a 0 speed
func (m *MotorDriver) Off() (err error) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/pir_motion_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewPIRMotionDriver(a DigitalReader, pin string, v ...time.Duration) *PIRMot
// just as long as motion is still being detected.
// It will only send the MotionStopped event once, however, until
// motion starts being detected again
func (p *PIRMotionDriver) Start() (errs []error) {
func (p *PIRMotionDriver) Start() (err error) {
go func() {
for {
newValue, err := p.connection.DigitalRead(p.Pin())
Expand Down Expand Up @@ -86,7 +86,7 @@ func (p *PIRMotionDriver) Start() (errs []error) {
}

// Halt stops polling the button for new information
func (p *PIRMotionDriver) Halt() (errs []error) {
func (p *PIRMotionDriver) Halt() (err error) {
p.halt <- true
return
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/relay_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func NewRelayDriver(a DigitalWriter, pin string) *RelayDriver {
}

// Start implements the Driver interface
func (l *RelayDriver) Start() (errs []error) { return }
func (l *RelayDriver) Start() (err error) { return }

// Halt implements the Driver interface
func (l *RelayDriver) Halt() (errs []error) { return }
func (l *RelayDriver) Halt() (err error) { return }

// Name returns the RelayDrivers name
func (l *RelayDriver) Name() string { return l.name }
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/rgb_led_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func NewRgbLedDriver(a DigitalWriter, redPin string, greenPin string, bluePin st
}

// Start implements the Driver interface
func (l *RgbLedDriver) Start() (errs []error) { return }
func (l *RgbLedDriver) Start() (err error) { return }

// Halt implements the Driver interface
func (l *RgbLedDriver) Halt() (errs []error) { return }
func (l *RgbLedDriver) Halt() (err error) { return }

// Name returns the RGBLEDDrivers name
func (l *RgbLedDriver) Name() string { return l.name }
Expand Down
Loading

0 comments on commit 08874e4

Please sign in to comment.