-
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(types): Added SupplyFunc and CallbackFunc
- Loading branch information
1 parent
a533090
commit 9a89262
Showing
17 changed files
with
284 additions
and
101 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,38 @@ | ||
package compose | ||
|
||
// CallbackFunc represents an function that accepts a value and perform side. | ||
// effects. This is a specialized form of Func, and thus is constructible from | ||
// a Func. | ||
type CallbackFunc func(interface{}) error | ||
|
||
// CallbackFuncConvertible represents an object that can be converted to a | ||
// CallbackFunc. | ||
type CallbackFuncConvertible interface { | ||
ToCallbackFunc() CallbackFunc | ||
} | ||
|
||
// ToCallbackFunc returns the current CallbackFunc. | ||
func (cf CallbackFunc) ToCallbackFunc() CallbackFunc { | ||
return cf | ||
} | ||
|
||
// Invoke invokes the underlying function. | ||
func (cf CallbackFunc) Invoke(value interface{}) error { | ||
return cf(value) | ||
} | ||
|
||
// ToFunc converts the current CallbackFunc into a ToFunc. | ||
func (cf CallbackFunc) ToFunc() Func { | ||
return func(value interface{}) (interface{}, error) { | ||
err := cf(value) | ||
return nil, err | ||
} | ||
} | ||
|
||
// ToCallbackFunc converts the current Func into an ToCallbackFunc. | ||
func (f Func) ToCallbackFunc() CallbackFunc { | ||
return func(value interface{}) error { | ||
_, err := f.Invoke(value) | ||
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package compose | ||
|
||
// CallbackFuncF converts an CallbackFunc to another CallbackFunc. | ||
type CallbackFuncF func(CallbackFunc) CallbackFunc | ||
|
||
// CallbackFuncFConvertible represents an object that can be converted to a | ||
// CallbackFuncF. | ||
type CallbackFuncFConvertible interface { | ||
ToCallbackFuncF() CallbackFuncF | ||
} | ||
|
||
// ToCallbackFuncF returns the current CallbackFuncF. | ||
func (cff CallbackFuncF) ToCallbackFuncF() CallbackFuncF { | ||
return cff | ||
} | ||
|
||
// Wrap is a convenience method that calls the underlying CallbackFuncF. | ||
func (cff CallbackFuncF) Wrap(ef CallbackFunc) CallbackFunc { | ||
return cff(ef) | ||
} | ||
|
||
// ToFuncF converts the current CallbackF into a ToFuncF. | ||
func (cff CallbackFuncF) ToFuncF() FuncF { | ||
return func(f Func) Func { | ||
callback := func(value interface{}) error { | ||
_, err := f.Invoke(value) | ||
return err | ||
} | ||
|
||
return cff.Wrap(callback).ToFunc() | ||
} | ||
} | ||
|
||
// ToCallbackFuncF converts a FuncF to an ToCallbackFuncF. 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) ToCallbackFuncF() CallbackFuncF { | ||
return func(cf CallbackFunc) CallbackFunc { | ||
var function Func = func(value interface{}) (interface{}, error) { | ||
return nil, cf(value) | ||
} | ||
|
||
return ff.Wrap(function).ToCallbackFunc() | ||
} | ||
} |
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,52 @@ | ||
package compose | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestConvertBetweenFuncTypes(t *testing.T) { | ||
/// Setup | ||
var errF Func = func(value interface{}) (interface{}, error) { | ||
return valueOp, errOp | ||
} | ||
|
||
/// When | ||
value, err := errF. | ||
ToCallbackFunc(). | ||
ToCallbackFunc(). | ||
ToFunc(). | ||
ToFunc(). | ||
ToSupplyFunc(). | ||
ToSupplyFunc(). | ||
ToFunc(). | ||
Invoke(0) | ||
|
||
/// Then | ||
if err != errOp || value != nil { | ||
t.Errorf("Expected %v, got %v", errOp, err) | ||
} | ||
} | ||
|
||
func TestCovertBetweenFuncF(t *testing.T) { | ||
/// Setup | ||
var errF Func = func(value interface{}) (interface{}, error) { | ||
return valueOp, errOp | ||
} | ||
|
||
/// When | ||
value, err := RetryF(retries). | ||
ToCallbackFuncF(). | ||
ToCallbackFuncF(). | ||
ToFuncF(). | ||
ToFuncF(). | ||
ToSupplyFuncF(). | ||
ToSupplyFuncF(). | ||
ToFuncF(). | ||
Wrap(errF). | ||
Invoke(0) | ||
|
||
/// Then | ||
if err != errOp || value != nil { | ||
t.Errorf("Expected %v, got %v", errOp, err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,18 +1,20 @@ | ||
package compose | ||
|
||
// Func represents an operation that could return an error. | ||
type Func func() (interface{}, error) | ||
type Func func(interface{}) (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() | ||
// FuncConvertible represents an object that can be converted to a Func. | ||
type FuncConvertible interface { | ||
ToFunc() Func | ||
} | ||
|
||
// ToFunc returns the current Func. | ||
func (f Func) ToFunc() Func { | ||
return f | ||
} | ||
|
||
// ErrorFunc converts the current function into an ErrorFunc. | ||
func (f Func) ErrorFunc() ErrorFunc { | ||
return func() error { | ||
_, err := f.Invoke() | ||
return err | ||
} | ||
// 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(value interface{}) (interface{}, error) { | ||
return f(value) | ||
} |
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
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.