Skip to content

Commit

Permalink
[#15] - Return values are now []interface{} instead of []reflect.Value
Browse files Browse the repository at this point in the history
  • Loading branch information
J7mbo committed Apr 10, 2019
1 parent a45a21d commit ab927b1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
7 changes: 4 additions & 3 deletions MethodCallRetrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (r *MethodCallRetrier) ExecuteFuncWithRetry(function func() error) []error
/* ExecuteWithRetry retries the call to object.methodName(...args) with a maximum number of retries and a wait time. */
func (r *MethodCallRetrier) ExecuteWithRetry(
object interface{}, methodName string, args ...interface{},
) ([]reflect.Value, []error) {
) ([]interface{}, []error) {
defer func() {
r.resetCurrentRetries()
r.resetErrorList()
Expand Down Expand Up @@ -120,10 +120,11 @@ func (r *MethodCallRetrier) ExecuteWithRetry(
return r.ExecuteWithRetry(object, methodName, args...)
}

results := make([]reflect.Value, returnValueCount)
results := make([]interface{}, returnValueCount)

for i := range results {
results[i] = returnValues[i]
/* Convert from reflect.Value to a magical anything. */
results[i] = returnValues[i].Interface()
}

return results, nil
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Retry your method calls automatically when an `error` is returned up to a specif

Extremely useful for retrying HTTP calls in distributed systems, or anything else over the network that can error sporadically.

An example of using this library for everything from retrying connections to redis, to logging with elasticsearch, can
be found [here](https://github.com/J7mbo/palmago-streetview).

Installation
-

Expand All @@ -31,7 +34,7 @@ Call `ExecuteWithRetry` with your object and method you want to retry:
```
ExecuteWithRetry(
object interface{}, methodName string, args ...interface{},
) ([]reflect.Value, []error)
) ([]interface{}, []error)
```

Alternatively, call `ExecuteFuncWithRetry` and pass in a function that returns `error` to retry.
Expand All @@ -46,11 +49,11 @@ You can use it as follows:
results, errs := retrier.ExecuteWithRetry(yourObject, "MethodToCall", "Arg1", "Arg2", "etc")
```

The results are an array of `reflect.Value` objects, (used for the dynamic method call), and an array of all errors.
The results are an array of `interface{}` objects, (used for the dynamic method call), and an array of all errors.
To use the results, you must typecast the result to the expected type. In the case of an `int64`, for example:

```
myInt := results[0].Interface().(int64)
myInt := results[0].(int64)
```

Or, to maintain type safety in userland, you can pass a function in instead and do all your retriable work in there:
Expand Down
6 changes: 3 additions & 3 deletions Retrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func (s *RetrierTestSuite) TestRetrierWorksWithPointer() {

results, _ := s.retrier.ExecuteWithRetry(&RetryObject{}, "MethodReturningString", arg)

s.Assert().EqualValues(results[0].String(), arg)
s.Assert().EqualValues(results[0], arg)
}

func (s *RetrierTestSuite) TestRetrierWorksWithObject() {
arg := "TestArg"

results, _ := s.retrier.ExecuteWithRetry(RetryObject{}, "MethodReturningString", arg)

s.Assert().EqualValues(results[0].String(), arg)
s.Assert().EqualValues(results[0], arg)
}

func (s *RetrierTestSuite) TestRetrierReturnsErrorOnInvalidMethod() {
Expand Down Expand Up @@ -86,7 +86,7 @@ func (s *RetrierTestSuite) TestRetrierWorksWithNegativeMaxRetries() {

results, _ := New(-1, -1, 1).ExecuteWithRetry(RetryObject{}, "MethodReturningString", arg)

s.Assert().EqualValues(results[0].String(), arg)
s.Assert().EqualValues(results[0], arg)
}

func (s *RetrierTestSuite) TestRetrierDefaultsToOneRetryGivenZeroMaxRetries() {
Expand Down

0 comments on commit ab927b1

Please sign in to comment.