项目作者: mauricioklein

项目描述 :
Chain function calls in Golang, with support to argument's feedback and error-handling
高级语言: Go
项目地址: git://github.com/mauricioklein/go-chainable.git
创建时间: 2018-02-28T22:27:22Z
项目社区:https://github.com/mauricioklein/go-chainable

开源协议:MIT License

下载


Go-chainable

Build Status

Maintainability
Test Coverage
GoDoc
License: MIT

Provides an easy and convenient way of chaining function calls in Golang, with support to argument’s feedback and error-handling

Requirements

  • Go 1.9 or later

Installation

  1. go get github.com/mauricioklein/go-chainable

Basics

It’s common to find Go programs similar to the one below:

  1. x, err := sum2(2)
  2. if err != nil {
  3. return 0, err
  4. }
  5. y, err = mul2(x)
  6. if err != nil {
  7. return 0, err
  8. }
  9. z, err = div2(y)
  10. if err != nil {
  11. return 0, err
  12. }
  13. return z, nil

Error handling can make the codebase messy. At the same time, chaining function calls can obfuscate
errors and cascading calls can increase the cognitive complexity.

Elixir, F# and other languages solve this problem with the support to pipes, but Golang hasn’t such
feature.

Chainable provides a clearer way to chain function calls, using the output of the previous function
as the input of the next one.

The example above could be re-written using Chainable as follow:

  1. import (chainable "github.com/mauricioklein/go-chainable")
  2. res, err := chainable.New().
  3. From(2).
  4. Chain(
  5. sum2,
  6. mul2,
  7. div2,
  8. ).
  9. Unwrap()
  10. // "Unwrap" returns the result as a slice of Argument, matching the return values
  11. // of the last function. So, we just need to cast them to the correct type
  12. z := res[0].(int)

Another advantage is that Chainable automatically handle errors in a chain.

Thus, if one of the methods returns an error as the last argument, the chain is broken
and the error is returned by the “Unwrap” method:

  1. raiseError := func () (int, error) { return 0, errors.New("generic error") }
  2. plus2 := func (x int) { return x + 2 }
  3. chainable.New().
  4. Chain(raiseError). // breakes the chain
  5. Chain(plus2). // never called
  6. Unwrap() // returns nil, "a generic error"

If automatic error handling isn’t desired (i.e. the error should be chained along with the other arguments),
the method “ChainDummy” should be used instead of “Chain”
(pay attention that the next function in the chain
must be able to receive the error generated by the previous one):

  1. raiseError := func () (int, error) { return 2, errors.New("generic error") }
  2. plus2AndError := func (x int, e error) (int) { return x + 2 }
  3. chainable.New().
  4. ChainDummy(raiseError).
  5. Chain(plus2AndError).
  6. Unwrap() // returns []Argument{4}, nil

“Chain” and “DummyChain” methods are variadics, and can be used in conjunction:

  1. chainable.New().
  2. Chain(f1). // error handling enabled for f1
  3. ChainDummy(f2). // error handling disabled for f2
  4. Chain(f3, f4). // error handling enabled for f3 and f4
  5. Unwrap()

Finally, to reset a chain and make it ready to be reused, just call the method “Reset”:

  1. chain := chainable.New()
  2. // ... use of chain
  3. chain.Reset()

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request