Skip to content

Commit

Permalink
feat(helper): Added If method
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
  • Loading branch information
flc1125 committed Mar 13, 2024
1 parent 04e32d3 commit 3248673
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ func Scan(src any, dest any) error {

return json.Unmarshal(bytes, dest)
}

func If[T any](condition bool, trueVal T, falseVal T) T {
if condition {
return trueVal
}

return falseVal
}
10 changes: 10 additions & 0 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,13 @@ func TestChainWithErr(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, ctx)
}

func TestIf(t *testing.T) {
// if true
got := If(true, "foo", "bar")
assert.Equal(t, "foo", got)

// if false
got = If(false, "foo", "bar")
assert.Equal(t, "bar", got)
}

0 comments on commit 3248673

Please sign in to comment.