Skip to content

Commit

Permalink
swap.Chain()
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Mar 14, 2021
1 parent 82d046c commit 011b60f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...
}
```
14 changes: 14 additions & 0 deletions swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 011b60f

Please sign in to comment.