From 0f199afc6bb27dac6e7953fe808839c0aa4e8976 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Tue, 23 Feb 2021 09:55:24 +0300 Subject: [PATCH] move examples and experimentals to submodules --- docs.go | 7 +++++++ errors.go | 20 +++++++++---------- errors_test.go | 10 +++++----- compare_test.go => examples/compare_test.go | 2 +- examples/docs.go | 6 ++++++ examples/go.mod | 7 +++++++ examples/http_test.go | 1 + retry.go | 5 ----- sandbox/docs.go | 3 +++ example_test.go => sandbox/example_test.go | 18 ++++++++++------- sandbox/go.mod | 7 +++++++ exp/experimental.go => sandbox/strategy.go | 4 ++-- .../strategy_test.go | 4 ++-- strategy/strategy.go | 2 +- 14 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 docs.go rename compare_test.go => examples/compare_test.go (98%) create mode 100644 examples/docs.go create mode 100644 examples/go.mod create mode 100644 examples/http_test.go create mode 100644 sandbox/docs.go rename example_test.go => sandbox/example_test.go (79%) create mode 100644 sandbox/go.mod rename exp/experimental.go => sandbox/strategy.go (95%) rename exp/experimental_test.go => sandbox/strategy_test.go (96%) diff --git a/docs.go b/docs.go new file mode 100644 index 0000000..f0457de --- /dev/null +++ b/docs.go @@ -0,0 +1,7 @@ +// Package retry provides the most advanced interruptible mechanism +// to perform actions repetitively until successful. +// +// The retry based on https://github.com/Rican7/retry but fully reworked +// and focused on integration with the https://github.com/kamilsk/breaker +// and the built-in https://pkg.go.dev/context package. +package retry diff --git a/errors.go b/errors.go index 832edf9..05f7aa1 100644 --- a/errors.go +++ b/errors.go @@ -11,17 +11,7 @@ func (err Error) Error() string { return string(err) } // Unwrap always returns nil means that an error doesn't have other root cause. func (err Error) Unwrap() error { return nil } -// equal to go.octolab.org/errors.Unwrap func unwrap(err error) error { - // compatible with github.com/pkg/errors - type causer interface { - Cause() error - } - // compatible with built-in errors since 1.13 - type wrapper interface { - Unwrap() error - } - for err != nil { layer, is := err.(wrapper) if is { @@ -37,3 +27,13 @@ func unwrap(err error) error { } return err } + +// compatible with github.com/pkg/errors +type causer interface { + Cause() error +} + +// compatible with built-in errors since 1.13 +type wrapper interface { + Unwrap() error +} diff --git a/errors_test.go b/errors_test.go index 9f576bf..eaa2dd5 100644 --- a/errors_test.go +++ b/errors_test.go @@ -17,18 +17,18 @@ func TestError(t *testing.T) { } func TestUnwrap(t *testing.T) { - cause := errors.New("root") - core := unwrap(causer{layer{cause}}) - if !reflect.DeepEqual(core, cause) { + root := errors.New("root") + core := unwrap(cause{layer{root}}) + if !reflect.DeepEqual(core, root) { t.Error("unexpected behavior") } } // helpers -type causer struct{ error } +type cause struct{ error } -func (causer causer) Cause() error { return causer.error } +func (cause cause) Cause() error { return cause.error } type layer struct{ error } diff --git a/compare_test.go b/examples/compare_test.go similarity index 98% rename from compare_test.go rename to examples/compare_test.go index e4b4d46..7e86467 100644 --- a/compare_test.go +++ b/examples/compare_test.go @@ -1,4 +1,4 @@ -package retry_test +package examples_test import ( "context" diff --git a/examples/docs.go b/examples/docs.go new file mode 100644 index 0000000..b94fd52 --- /dev/null +++ b/examples/docs.go @@ -0,0 +1,6 @@ +// Package examples contains extended documentation +// for github.com/kamilsk/retry/v5 module. +// +// It contains examples of usage with additional +// dependencies that are not needed by the module. +package examples diff --git a/examples/go.mod b/examples/go.mod new file mode 100644 index 0000000..3f759ea --- /dev/null +++ b/examples/go.mod @@ -0,0 +1,7 @@ +module github.com/kamilsk/retry/examples + +go 1.13 + +require github.com/kamilsk/retry/v5 v5.0.0-rc8 + +replace github.com/kamilsk/retry/v5 => ../ diff --git a/examples/http_test.go b/examples/http_test.go new file mode 100644 index 0000000..8e41636 --- /dev/null +++ b/examples/http_test.go @@ -0,0 +1 @@ +package examples_test diff --git a/retry.go b/retry.go index a8ba964..ade0fe4 100644 --- a/retry.go +++ b/retry.go @@ -1,8 +1,3 @@ -// Package retry provides the most advanced interruptible mechanism -// to perform actions repetitively until successful. -// The retry based on https://github.com/Rican7/retry but fully reworked -// and focused on integration with the https://github.com/kamilsk/breaker -// and the built-in https://pkg.go.dev/context package. package retry import ( diff --git a/sandbox/docs.go b/sandbox/docs.go new file mode 100644 index 0000000..79b7492 --- /dev/null +++ b/sandbox/docs.go @@ -0,0 +1,3 @@ +// Package exp contains experimental unstable +// features. +package sandbox diff --git a/example_test.go b/sandbox/example_test.go similarity index 79% rename from example_test.go rename to sandbox/example_test.go index 51f46da..ccc6f87 100644 --- a/example_test.go +++ b/sandbox/example_test.go @@ -1,16 +1,20 @@ -package retry_test +// +build go1.13 + +package sandbox_test import ( "context" "database/sql" + "errors" "fmt" "math/rand" "net" "time" + "github.com/kamilsk/retry/sandbox" + "github.com/kamilsk/retry/v5" "github.com/kamilsk/retry/v5/backoff" - "github.com/kamilsk/retry/v5/exp" "github.com/kamilsk/retry/v5/jitter" "github.com/kamilsk/retry/v5/strategy" ) @@ -31,8 +35,8 @@ func Example() { ), // experimental - exp.CheckError( - exp.NetworkError(exp.Skip), + sandbox.CheckError( + sandbox.NetworkError(sandbox.Skip), DatabaseError(), ), } @@ -60,10 +64,10 @@ func SendRequest(ctx context.Context) error { } func DatabaseError() func(error) bool { - blacklist := []error{sql.ErrNoRows, sql.ErrConnDone, sql.ErrTxDone} + deprecated := []error{sql.ErrNoRows, sql.ErrConnDone, sql.ErrTxDone} return func(err error) bool { - for _, preset := range blacklist { - if err == preset { + for _, deprecated := range deprecated { + if errors.Is(err, deprecated) { return false } } diff --git a/sandbox/go.mod b/sandbox/go.mod new file mode 100644 index 0000000..5093fea --- /dev/null +++ b/sandbox/go.mod @@ -0,0 +1,7 @@ +module github.com/kamilsk/retry/sandbox + +go 1.11 + +require github.com/kamilsk/retry/v5 v5.0.0-rc8 + +replace github.com/kamilsk/retry/v5 => ../ diff --git a/exp/experimental.go b/sandbox/strategy.go similarity index 95% rename from exp/experimental.go rename to sandbox/strategy.go index bc00be4..d444f11 100644 --- a/exp/experimental.go +++ b/sandbox/strategy.go @@ -1,4 +1,4 @@ -package exp +package sandbox import "net" @@ -9,7 +9,7 @@ const ( // A Breaker carries a cancellation signal to interrupt an action execution. // -// It is a subset of the built-in Context and github.com/kamilsk/breaker interfaces. +// It is a subset of the built-in context and github.com/kamilsk/breaker interfaces. type Breaker = interface { // Done returns a channel that's closed when a cancellation signal occurred. Done() <-chan struct{} diff --git a/exp/experimental_test.go b/sandbox/strategy_test.go similarity index 96% rename from exp/experimental_test.go rename to sandbox/strategy_test.go index 2749f51..35660b2 100644 --- a/exp/experimental_test.go +++ b/sandbox/strategy_test.go @@ -1,4 +1,4 @@ -package exp_test +package sandbox_test import ( "context" @@ -7,7 +7,7 @@ import ( "net" "testing" - . "github.com/kamilsk/retry/v5/exp" + . "github.com/kamilsk/retry/sandbox" ) func TestCheckError(t *testing.T) { diff --git a/strategy/strategy.go b/strategy/strategy.go index 3ca7baf..a51e4ef 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -5,7 +5,7 @@ import "time" // A Breaker carries a cancellation signal to interrupt an action execution. // -// It is a subset of the built-in Context and github.com/kamilsk/breaker interfaces. +// It is a subset of the built-in context and github.com/kamilsk/breaker interfaces. type Breaker = interface { // Done returns a channel that's closed when a cancellation signal occurred. Done() <-chan struct{}