Skip to content

Commit

Permalink
Rename chainable error to be more go idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Nov 4, 2024
1 parent 22bb4a7 commit a4868f9
Show file tree
Hide file tree
Showing 21 changed files with 278 additions and 275 deletions.
11 changes: 7 additions & 4 deletions common/chainable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ type Chainable struct {
err error
}

// NewChainable initialises new chainable
func NewChainable() *Chainable {
return &Chainable{}
}

func (c *Chainable) SetChainError(err error) {
// SetErr sets chainable error
func (c *Chainable) SetErr(err error) {
c.err = err
}

func (c *Chainable) HasChainError() bool {
// HasErr returns true when chainable has error
func (c *Chainable) HasErr() bool {
return c.err != nil
}

// @TODO: rename to Err()
func (c *Chainable) ChainError() error {
// Err returns chainable error
func (c *Chainable) Err() error {
return c.err
}
6 changes: 3 additions & 3 deletions component/activation_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ func WantsToKeepInputs(activationResult *ActivationResult) bool {
return activationResult.Code() == ActivationCodeWaitingForInputsKeep
}

// WithChainError returns activation result with chain error
func (ar *ActivationResult) WithChainError(err error) *ActivationResult {
ar.SetChainError(err)
// WithErr returns activation result with chain error
func (ar *ActivationResult) WithErr(err error) *ActivationResult {
ar.SetErr(err)
return ar
}
22 changes: 11 additions & 11 deletions component/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func NewCollection() *Collection {

// ByName returns a component by its name
func (c *Collection) ByName(name string) *Component {
if c.HasChainError() {
return New("").WithChainError(c.ChainError())
if c.HasErr() {
return New("").WithErr(c.Err())
}

component, ok := c.components[name]

if !ok {
c.SetChainError(errors.New("component not found"))
c.SetErr(errors.New("component not found"))
return nil
}

Expand All @@ -40,24 +40,24 @@ func (c *Collection) ByName(name string) *Component {

// With adds components and returns the collection
func (c *Collection) With(components ...*Component) *Collection {
if c.HasChainError() {
if c.HasErr() {
return c
}

for _, component := range components {
c.components[component.Name()] = component

if component.HasChainError() {
return c.WithChainError(component.ChainError())
if component.HasErr() {
return c.WithErr(component.Err())
}
}

return c
}

// WithChainError returns group with error
func (c *Collection) WithChainError(err error) *Collection {
c.SetChainError(err)
// WithErr returns group with error
func (c *Collection) WithErr(err error) *Collection {
c.SetErr(err)
return c
}

Expand All @@ -67,8 +67,8 @@ func (c *Collection) Len() int {
}

func (c *Collection) Components() (ComponentsMap, error) {
if c.HasChainError() {
return nil, c.ChainError()
if c.HasErr() {
return nil, c.Err()
}
return c.components, nil
}
102 changes: 51 additions & 51 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func New(name string) *Component {

// WithDescription sets a description
func (c *Component) WithDescription(description string) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}

Expand All @@ -48,23 +48,23 @@ func (c *Component) WithDescription(description string) *Component {

// withInputPorts sets input ports collection
func (c *Component) withInputPorts(collection *port.Collection) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}
if collection.HasChainError() {
return c.WithChainError(collection.ChainError())
if collection.HasErr() {
return c.WithErr(collection.Err())
}
c.inputs = collection
return c
}

// withOutputPorts sets input ports collection
func (c *Component) withOutputPorts(collection *port.Collection) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}
if collection.HasChainError() {
return c.WithChainError(collection.ChainError())
if collection.HasErr() {
return c.WithErr(collection.Err())
}

c.outputs = collection
Expand All @@ -73,36 +73,36 @@ func (c *Component) withOutputPorts(collection *port.Collection) *Component {

// WithInputs ads input ports
func (c *Component) WithInputs(portNames ...string) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}

ports, err := port.NewGroup(portNames...).Ports()
if err != nil {
c.SetChainError(err)
return New("").WithChainError(c.ChainError())
c.SetErr(err)
return New("").WithErr(c.Err())
}

return c.withInputPorts(c.Inputs().With(ports...))
}

// WithOutputs adds output ports
func (c *Component) WithOutputs(portNames ...string) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}

ports, err := port.NewGroup(portNames...).Ports()
if err != nil {
c.SetChainError(err)
return New("").WithChainError(c.ChainError())
c.SetErr(err)
return New("").WithErr(c.Err())
}
return c.withOutputPorts(c.Outputs().With(ports...))
}

// WithInputsIndexed creates multiple prefixed ports
func (c *Component) WithInputsIndexed(prefix string, startIndex int, endIndex int) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}

Expand All @@ -111,7 +111,7 @@ func (c *Component) WithInputsIndexed(prefix string, startIndex int, endIndex in

// WithOutputsIndexed creates multiple prefixed ports
func (c *Component) WithOutputsIndexed(prefix string, startIndex int, endIndex int) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}

Expand All @@ -120,7 +120,7 @@ func (c *Component) WithOutputsIndexed(prefix string, startIndex int, endIndex i

// WithActivationFunc sets activation function
func (c *Component) WithActivationFunc(f ActivationFunc) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}

Expand All @@ -130,7 +130,7 @@ func (c *Component) WithActivationFunc(f ActivationFunc) *Component {

// WithLabels sets labels and returns the component
func (c *Component) WithLabels(labels common.LabelsCollection) *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}
c.LabeledEntity.SetLabels(labels)
Expand All @@ -139,51 +139,51 @@ func (c *Component) WithLabels(labels common.LabelsCollection) *Component {

// Inputs getter
func (c *Component) Inputs() *port.Collection {
if c.HasChainError() {
return port.NewCollection().WithChainError(c.ChainError())
if c.HasErr() {
return port.NewCollection().WithErr(c.Err())
}

return c.inputs
}

// Outputs getter
func (c *Component) Outputs() *port.Collection {
if c.HasChainError() {
return port.NewCollection().WithChainError(c.ChainError())
if c.HasErr() {
return port.NewCollection().WithErr(c.Err())
}

return c.outputs
}

// OutputByName is shortcut method
func (c *Component) OutputByName(name string) *port.Port {
if c.HasChainError() {
return port.New("").WithChainError(c.ChainError())
if c.HasErr() {
return port.New("").WithErr(c.Err())
}
outputPort := c.Outputs().ByName(name)
if outputPort.HasChainError() {
c.SetChainError(outputPort.ChainError())
return port.New("").WithChainError(c.ChainError())
if outputPort.HasErr() {
c.SetErr(outputPort.Err())
return port.New("").WithErr(c.Err())
}
return outputPort
}

// InputByName is shortcut method
func (c *Component) InputByName(name string) *port.Port {
if c.HasChainError() {
return port.New("").WithChainError(c.ChainError())
if c.HasErr() {
return port.New("").WithErr(c.Err())
}
inputPort := c.Inputs().ByName(name)
if inputPort.HasChainError() {
c.SetChainError(inputPort.ChainError())
return port.New("").WithChainError(c.ChainError())
if inputPort.HasErr() {
c.SetErr(inputPort.Err())
return port.New("").WithErr(c.Err())
}
return inputPort
}

// hasActivationFunction checks when activation function is set
func (c *Component) hasActivationFunction() bool {
if c.HasChainError() {
if c.HasErr() {
return false
}

Expand All @@ -192,26 +192,26 @@ func (c *Component) hasActivationFunction() bool {

// propagateChainErrors propagates up all chain errors that might have not been propagated yet
func (c *Component) propagateChainErrors() {
if c.Inputs().HasChainError() {
c.SetChainError(c.Inputs().ChainError())
if c.Inputs().HasErr() {
c.SetErr(c.Inputs().Err())
return
}

if c.Outputs().HasChainError() {
c.SetChainError(c.Outputs().ChainError())
if c.Outputs().HasErr() {
c.SetErr(c.Outputs().Err())
return
}

for _, p := range c.Inputs().PortsOrNil() {
if p.HasChainError() {
c.SetChainError(p.ChainError())
if p.HasErr() {
c.SetErr(p.Err())
return
}
}

for _, p := range c.Outputs().PortsOrNil() {
if p.HasChainError() {
c.SetChainError(p.ChainError())
if p.HasErr() {
c.SetErr(p.Err())
return
}
}
Expand All @@ -223,8 +223,8 @@ func (c *Component) propagateChainErrors() {
func (c *Component) MaybeActivate() (activationResult *ActivationResult) {
c.propagateChainErrors()

if c.HasChainError() {
activationResult = NewActivationResult(c.Name()).WithChainError(c.ChainError())
if c.HasErr() {
activationResult = NewActivationResult(c.Name()).WithErr(c.Err())
return
}

Expand Down Expand Up @@ -265,35 +265,35 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) {

// FlushOutputs pushed signals out of the component outputs to pipes and clears outputs
func (c *Component) FlushOutputs() *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}

ports, err := c.Outputs().Ports()
if err != nil {
c.SetChainError(err)
return New("").WithChainError(c.ChainError())
c.SetErr(err)
return New("").WithErr(c.Err())
}
for _, out := range ports {
out = out.Flush()
if out.HasChainError() {
return c.WithChainError(out.ChainError())
if out.HasErr() {
return c.WithErr(out.Err())
}
}
return c
}

// ClearInputs clears all input ports
func (c *Component) ClearInputs() *Component {
if c.HasChainError() {
if c.HasErr() {
return c
}
c.Inputs().Clear()
return c
}

// WithChainError returns component with error
func (c *Component) WithChainError(err error) *Component {
c.SetChainError(err)
// WithErr returns component with error
func (c *Component) WithErr(err error) *Component {
c.SetErr(err)
return c
}
Loading

0 comments on commit a4868f9

Please sign in to comment.