-
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.
refactor(all): Revamped code base to extend on general Function inste…
…ad of ErrorF
- Loading branch information
1 parent
4095a24
commit f5bbec4
Showing
16 changed files
with
236 additions
and
197 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package compose | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
) | ||
|
||
const ( | ||
delayTime = time.Duration(1e8) | ||
retryCount = uint(10) | ||
|
||
// This value should be returned by test Functions. | ||
valueOp = 1 | ||
) | ||
|
||
var ( | ||
// This error should be returned by test Functions. | ||
errOp = errors.New("error") | ||
) |
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,8 +1,4 @@ | ||
// Package compose contains utility higher-order functions to wrap normal | ||
// functions. | ||
// | ||
// Naming conventions are: | ||
// - J prefix: Represents Just. | ||
// - M prefix: Represents Map. | ||
// - F prefix: Represents FlatMap. | ||
// Package compose contains higher-order functions to wrap normal functions to | ||
// provide additional capabilities. It also contains function extensions that | ||
// call these higher functions for easy chaining. | ||
package compose |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package compose | ||
|
||
// Function represents an operation that could return an error. | ||
type Function 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) { | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package compose | ||
|
||
// FunctionF transforms a Function into another Function. | ||
type FunctionF func(Function) Function | ||
|
||
// 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 { | ||
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 { | ||
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 { | ||
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,13 @@ | ||
package compose | ||
|
||
// NoopF does nothing and simply returns the function. | ||
func NoopF() FunctionF { | ||
return func(f Function) Function { | ||
return f | ||
} | ||
} | ||
|
||
// Noop is a convenience function that calls the composable NoopF under the hood. | ||
func (f Function) Noop() Function { | ||
return NoopF().Wrap(f) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package compose | ||
|
||
import "testing" | ||
|
||
func TestNoop(t *testing.T) { | ||
/// Setup | ||
var errF Function = func() (interface{}, error) { | ||
return valueOp, errOp | ||
} | ||
|
||
/// When & Then | ||
if _, err := errF.Noop().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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package compose | ||
|
||
// PublishF publishes the result of a Function for side effects. | ||
func PublishF(callback func(interface{}, error)) FunctionF { | ||
return func(f Function) Function { | ||
return func() (interface{}, error) { | ||
value, err := f() | ||
callback(value, err) | ||
return value, err | ||
} | ||
} | ||
} | ||
|
||
// Publish is a convenience function that calls the composable PublishF under | ||
// the hood. | ||
func (f Function) Publish(callback func(interface{}, error)) Function { | ||
return PublishF(callback).Wrap(f) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package compose | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPublish(t *testing.T) { | ||
/// Setup | ||
published := 0 | ||
var publishedValue interface{} | ||
var publishedErr error | ||
|
||
var errF Function = func() (interface{}, error) { | ||
return valueOp, errOp | ||
} | ||
|
||
publishF := func(value interface{}, err error) { | ||
published++ | ||
publishedValue = value | ||
publishedErr = err | ||
} | ||
|
||
/// When & Then | ||
value, err := errF.Publish(publishF).Retry(retryCount).Invoke() | ||
|
||
if err != errOp || value != nil { | ||
t.Errorf("Expected %v, got %v", errOp, err) | ||
} | ||
|
||
if publishedValue != valueOp { | ||
t.Errorf("Expected %v, got %v", valueOp, publishedValue) | ||
} | ||
|
||
if publishedErr != errOp { | ||
t.Errorf("Expected %v, got %v", errOp, publishedErr) | ||
} | ||
|
||
if uint(published) != retryCount+1 { | ||
t.Errorf("Expected %v, got %v", retryCount+1, published) | ||
} | ||
} |
Oops, something went wrong.