Skip to content

Commit

Permalink
go generics
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Feb 20, 2022
1 parent f9352ad commit af20de2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 183 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ func init() {
import "github.com/tada-team/swap"

func TestWithSwap(t *testing.T) {
defer swap.Bool(&myConfig.Foo, "test value")()
defer swap.Int(&myConfig.Bar, 42)()
defer swap.Value(&myConfig.Foo, "test value")()
defer swap.Value(&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...
defer swap.Chain(
swap.Value(&myConfig.Foo, "test value"),
swap.Value(&myConfig.Bar, 42),
)()
// ...test cases...
}
```
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module github.com/tada-team/swap

go 1.13

go 1.18
152 changes: 5 additions & 147 deletions swap.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package swap

import (
"sync"
"time"
)
import "sync"

var mux sync.RWMutex

Expand All @@ -23,11 +20,11 @@ func RevChain(fns ...func()) func() {
}
}

func True(p *bool) func() { return Bool(p, true) }
func True(p *bool) func() { return Value(p, true) }

func False(p *bool) func() { return Bool(p, false) }
func False(p *bool) func() { return Value(p, false) }

func Bool(p *bool, v bool) func() {
func Value[T any](p *T, v T) func() {
mux.Lock()
defer mux.Unlock()

Expand All @@ -41,146 +38,7 @@ func Bool(p *bool, v bool) func() {
}
}

func GetBool(p *bool) bool {
mux.RLock()
defer mux.RUnlock()
return *p
}

func String(p *string, v string) func() {
mux.Lock()
defer mux.Unlock()

old := *p
*p = v

return func() {
mux.Lock()
defer mux.Unlock()
*p = old
}
}

func GetString(p *string) string {
mux.RLock()
defer mux.RUnlock()
return *p
}

func Strings(p *[]string, v []string) func() {
mux.Lock()
defer mux.Unlock()

old := *p
*p = v

return func() {
mux.Lock()
defer mux.Unlock()
*p = old
}
}

func GetStrings(p *[]string) []string {
mux.RLock()
defer mux.RUnlock()
return *p
}

func Int(p *int, v int) func() {
mux.Lock()
defer mux.Unlock()

old := *p
*p = v

return func() {
mux.Lock()
defer mux.Unlock()
*p = old
}
}

func GetInt(p *int) int {
mux.RLock()
defer mux.RUnlock()
return *p
}

func Int64(p *int64, v int64) func() {
mux.Lock()
defer mux.Unlock()

old := *p
*p = v

return func() {
mux.Lock()
defer mux.Unlock()
*p = old
}
}

func GetInt64(p *int64) int64 {
mux.RLock()
defer mux.RUnlock()
return *p
}

func Int32(p *int32, v int32) func() {
mux.Lock()
defer mux.Unlock()

old := *p
*p = v

return func() {
mux.Lock()
defer mux.Unlock()
*p = old
}
}

func GetInt32(p *int32) int32 {
mux.RLock()
defer mux.RUnlock()
return *p
}

func Duration(p *time.Duration, v time.Duration) func() {
mux.Lock()
defer mux.Unlock()

old := *p
*p = v

return func() {
mux.Lock()
defer mux.Unlock()
*p = old
}
}

func GetDuration(p *time.Duration) time.Duration {
mux.RLock()
defer mux.RUnlock()
return *p
}

func Time(p *time.Time, v time.Time) func() {
mux.Lock()
defer mux.Unlock()

old := *p
*p = v
return func() {
mux.Lock()
defer mux.Unlock()
*p = old
}
}

func GetTime(p *time.Time) time.Time {
func GetValue[T any](p *T) T {
mux.RLock()
defer mux.RUnlock()
return *p
Expand Down
42 changes: 15 additions & 27 deletions swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,28 @@ package swap

import "testing"

func TestChain(t *testing.T) {
x := 2
y := 3
func TestValueAndChain(t *testing.T) {
someInt := 2
someString := "3"
someStrings := []string{"3"}
someBool := true

t.Run("defer", func(t *testing.T) {
if x != 2 {
t.Fatal("x must be 2")
if someInt != 2 {
t.Fatal("someInt must be 2")
}
defer Chain(
Int(&x, 10),
Int(&y, 20),
Value(&someInt, 10),
Value(&someString, "20"),
Value(&someBool, false),
Value(&someStrings, []string{"fff"}),
)()
if x != 10 {
t.Fatal("x must be 10")
if someInt != 10 {
t.Fatal("someInt must be 10")
}
})

if x != 2 {
t.Fatal("x must be 2")
}
}

func TestInt(t *testing.T) {
x := 2
t.Run("defer", func(t *testing.T) {
if x != 2 {
t.Fatal("x must be 2")
}
defer Int(&x, 10)()
if x != 10 {
t.Fatal("x must be 10")
}
})
if x != 2 {
t.Fatal("x must be 2")
if someInt != 2 {
t.Fatal("someInt must be 2")
}
}

0 comments on commit af20de2

Please sign in to comment.