-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(errorFuncF): added ErrorFunc and ErrorFuncF
- Loading branch information
1 parent
f5bbec4
commit a533090
Showing
13 changed files
with
140 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package compose | ||
|
||
// ErrorFunc represents an error-returning function. This is a specialized | ||
// form of Func, and thus is constructible from a Func. | ||
type ErrorFunc func() error | ||
|
||
// Invoke invokes the underlying error function. | ||
func (ef ErrorFunc) Invoke() error { | ||
return ef() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package compose | ||
|
||
// ErrorFuncF converts an ErrorFunc to another ErrorFunc. | ||
type ErrorFuncF func(ErrorFunc) ErrorFunc | ||
|
||
// Wrap is a convenience method that calls the underlying ErrorFuncF. | ||
func (eff ErrorFuncF) Wrap(ef ErrorFunc) ErrorFunc { | ||
return eff(ef) | ||
} | ||
|
||
// ErrorFuncF converts a FuncF to an ErrorFuncF. This method should be used at | ||
// the last stage of a chain so we do not need to redefine other higher-order | ||
// functions. | ||
func (ff FuncF) ErrorFuncF() ErrorFuncF { | ||
return func(ef ErrorFunc) ErrorFunc { | ||
var function Func = func() (interface{}, error) { | ||
return nil, ef() | ||
} | ||
|
||
wrapped := ff.Wrap(function) | ||
|
||
return func() error { | ||
_, err := wrapped.Invoke() | ||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
package compose | ||
|
||
// Function represents an operation that could return an error. | ||
type Function func() (interface{}, error) | ||
// Func represents an operation that could return an error. | ||
type Func func() (interface{}, error) | ||
|
||
// Invoke is a convenience method to call a Function. Although it is the same | ||
// as if we call the function normally, this may look nicer in a chain. | ||
func (f Function) Invoke() (interface{}, error) { | ||
// Invoke is a convenience method to call a Func. Although it is the same as if | ||
// we call the function normally, this may look nicer in a chain. | ||
func (f Func) Invoke() (interface{}, error) { | ||
return f() | ||
} | ||
|
||
// ErrorFunc converts the current function into an ErrorFunc. | ||
func (f Func) ErrorFunc() ErrorFunc { | ||
return func() error { | ||
_, err := f.Invoke() | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
package compose | ||
|
||
// FunctionF transforms a Function into another Function. | ||
type FunctionF func(Function) Function | ||
// FuncF transforms a Func into another Func. | ||
type FuncF func(Func) Func | ||
|
||
// Compose composes the functionalities of both FunctionF. We can use this to | ||
// chain enhance a base Function without exposing implementation details. | ||
func (ff FunctionF) Compose(selector FunctionF) FunctionF { | ||
return func(f Function) Function { | ||
// Compose composes the functionalities of both FuncF. We can use this to chain | ||
// enhance a base Func without exposing implementation details. | ||
func (ff FuncF) Compose(selector FuncF) FuncF { | ||
return func(f Func) Func { | ||
return ff(selector(f)) | ||
} | ||
} | ||
|
||
// ComposeFn is similar to Compose, but it is more convenient when we deal with | ||
// functions that return FunctionF. | ||
func (ff FunctionF) ComposeFn(selectorFn func() FunctionF) FunctionF { | ||
// functions that return FuncF. | ||
func (ff FuncF) ComposeFn(selectorFn func() FuncF) FuncF { | ||
return ff.Compose(selectorFn()) | ||
} | ||
|
||
// Wrap is a convenience method to invoke the wrap on a Function. This may look | ||
// nice in a function chain. | ||
func (ff FunctionF) Wrap(f Function) Function { | ||
// Wrap is a convenience method to invoke the wrap on a Func. This may look | ||
// nicer in a function chain. | ||
func (ff FuncF) Wrap(f Func) Func { | ||
return ff(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package compose | ||
|
||
import "testing" | ||
|
||
func TestConvertToErrorFunc(t *testing.T) { | ||
/// Setup | ||
var errF Func = func() (interface{}, error) { | ||
return valueOp, errOp | ||
} | ||
|
||
/// When & Then | ||
if err := errF.ErrorFunc().Invoke(); err != errOp { | ||
t.Errorf("Expected %v, got %v", errOp, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package compose | ||
|
||
// NoopF does nothing and simply returns the function. | ||
func NoopF() FunctionF { | ||
return func(f Function) Function { | ||
func NoopF() FuncF { | ||
return func(f Func) Func { | ||
return f | ||
} | ||
} | ||
|
||
// Noop is a convenience function that calls the composable NoopF under the hood. | ||
func (f Function) Noop() Function { | ||
func (f Func) Noop() Func { | ||
return NoopF().Wrap(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.