Skip to content

Commit

Permalink
feat: support If condition expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed Dec 3, 2024
1 parent 67e0082 commit 5604f4e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 49 deletions.
1 change: 1 addition & 0 deletions bench/decorator-bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
)

func benchmark(b *testing.B, f func(int) UserInfo) {
b.Helper()
for i := 0; i < b.N; i++ {
f(50)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/decorator-err_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestNeedCacheErrWithTTL(t *testing.T) {
if age <= 0 {
return UserInfo{}, errors.New("invalid age")
}
return UserInfo{Name: "Anonymous", Age: 9}, nil
return UserInfo{Name: "Anonymous", Age: age}, nil
}
// 1. Cacheable Function
getUserAndErrCached := gofnext.CacheFn1Err(getUserAndErr, &gofnext.Config{
Expand Down
16 changes: 8 additions & 8 deletions examples/decorator-key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ func TestCacheFuncKeyStruct(t *testing.T) {
}
}
func TestCacheFuncKeyStructUnexportedKey(t *testing.T) {
type info struct{
name string // unexported field
}
type info struct {
name string // unexported field
}

type UserInfo struct {
info struct{
name string // unexported field
}
info struct {
name string // unexported field
}
}
// Original function
getUserName := func(user UserInfo, flag *string) string {
return user.info.name
}

// Cacheable Function
getUserName2 := gofnext.CacheFn2(getUserName, )
getUserName2 := gofnext.CacheFn2(getUserName)

// Execute the function
flag := "flag"
flag := "flag"
name := getUserName2(UserInfo{info{name: "Alex"}}, &flag)
if name != "Alex" {
t.Errorf("name should be 'Alex', but get %s", name)
Expand Down
78 changes: 39 additions & 39 deletions examples/decorator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ func TestCacheFuncWith3Params(t *testing.T) {
}
}

func TestCacheCtxFuncWith3Params(t *testing.T) {
// Original function
executeCount := 0
sum := func(ctx context.Context, b, c int) int {
executeCount++
time.Sleep(10 * time.Millisecond)
return b + c
}

// Cacheable Function
sumCache := gofnext.CacheFn3(sum, &gofnext.Config{
TTL: time.Hour,
}) // accept 3 parameter

// Execute the function multi times in parallel.
parallelCall(func() {
ctx := context.Background()
score := sumCache(ctx, 3, 5)
if score != 8 {
t.Errorf("score should be 99, but get %d", score)
}
ctx = context.Background()
sumCache(ctx, 3, 5)
ctx = context.Background()
sumCache(ctx, 3, 6)
}, 5)

if executeCount != 2 {
t.Errorf("executeCount should be 2, but get %d", executeCount)
}
}

// Cache Function with more parameter(>3)
func TestCacheFuncWithMoreParams(t *testing.T) {
executeCount := 0
Expand All @@ -110,8 +142,8 @@ func TestCacheFuncWithMoreParams(t *testing.T) {
}

// Original function
fn := func(name string, age, gender, height int) int {
_ = age+ gender+ height
getUserScoreOrigin := func(name string, age, gender, height int) int {
_ = age + gender + height
executeCount++
// select score from db where name=name and age=age and gender=gender
switch name {
Expand All @@ -124,59 +156,27 @@ func TestCacheFuncWithMoreParams(t *testing.T) {

// Convert to extra parameters to a 1 parameter(2 or 3 prameters)
fnWrap := func(arg Stu) int {
return fn(arg.name, arg.age, arg.gender, arg.height)
return getUserScoreOrigin(arg.name, arg.age, arg.gender, arg.height)
}

// Cacheable Function
fnCachedInner := gofnext.CacheFn1(fnWrap)
fnCached := func(name string, age, gender, height int) int {
getUserScore := func(name string, age, gender, height int) int {
return fnCachedInner(Stu{name, age, gender, height})
}

// Execute the function multi times in parallel.
parallelCall(func() {
score := fnCached("Alex", 20, 1, 160)
score := getUserScore("Alex", 20, 1, 160)
if score != 10 {
t.Errorf("score should be 10, but get %d", score)
}
fnCached("Jhon", 21, 0, 160)
fnCached("Alex", 20, 1, 160)
getUserScore("Jhon", 21, 0, 160)
getUserScore("Alex", 20, 1, 160)
}, 10)

// Test count
if executeCount != 2 {
t.Errorf("executeCount should be 2, but get %d", executeCount)
}
}

func TestCacheCtxFuncWith3Params(t *testing.T) {
// Original function
executeCount := 0
sum := func(ctx context.Context, b, c int) int {
executeCount++
time.Sleep(10 * time.Millisecond)
return b + c
}

// Cacheable Function
sumCache := gofnext.CacheFn3(sum, &gofnext.Config{
TTL: time.Hour,
}) // accept 3 parameter

// Execute the function multi times in parallel.
parallelCall(func() {
ctx := context.Background()
score := sumCache(ctx, 3, 5)
if score != 8 {
t.Errorf("score should be 99, but get %d", score)
}
ctx = context.Background()
sumCache(ctx, 3, 5)
ctx = context.Background()
sumCache(ctx, 3, 6)
}, 5)

if executeCount != 2 {
t.Errorf("executeCount should be 2, but get %d", executeCount)
}
}
8 changes: 8 additions & 0 deletions funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gofnext

func If[T any](condition bool, trueVal, falseVal T) T {
if condition {
return trueVal
}
return falseVal
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.22
v0.0.23

0 comments on commit 5604f4e

Please sign in to comment.