diff --git a/README.md b/README.md index 122be76..ce8fb83 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,13 @@ func TestWithSwap(t *testing.T) { defer swap.Int(&myConfig.Bar, 42)() // ...test cases... } + +// more sugar +func TestWithSwapChain(t *testing.T) { + defer swap.Chain( + swap.Bool(&myConfig.Foo, "test value"), + swap.Int(&myConfig.Bar, 42), + ) + // ...test cases... +} ``` diff --git a/swap.go b/swap.go index cf73834..19b13ff 100644 --- a/swap.go +++ b/swap.go @@ -2,6 +2,14 @@ package swap import "time" +func Chain(fns ...func()) func() { + return func() { + for _, fn := range fns { + fn() + } + } +} + func Bool(p *bool, v bool) func() { old := *p *p = v @@ -26,6 +34,12 @@ func Int(p *int, v int) func() { return func() { *p = old } } +func Int64(p *int64, v int64) func() { + old := *p + *p = v + return func() { *p = old } +} + func Duration(p *time.Duration, v time.Duration) func() { old := *p *p = v