Skip to content

Commit

Permalink
improve DefaultArg
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Nov 1, 2024
1 parent c62106f commit ea9e0ae
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion modules/testlogger/testlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (w *testLoggerWriterCloser) Reset() {
func PrintCurrentTest(t testing.TB, skip ...int) func() {
t.Helper()
start := time.Now()
actualSkip := util.DefArgZero(skip) + 1
actualSkip := util.DefaultArg(skip) + 1
_, filename, line, _ := runtime.Caller(actualSkip)

if log.CanColorStdout {
Expand Down
27 changes: 16 additions & 11 deletions modules/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,23 @@ func IfZero[T comparable](v, def T) T {
return v
}

// DefArg helps the "optional argument" in Golang: func(foo string, optionalArg ...int)
// it returns the first non-zero value from the given optional argument,
// or the default value if there is no optional argument.
func DefArg[T any](defArgs []T, def T) (ret T) {
if len(defArgs) == 1 {
return defArgs[0]
// DefaultArg helps the "optional argument" in Golang:
//
// func foo(optionalArg ...int) { return DefaultArg(optionalArg) }
// calling `foo()` gets 0, calling `foo(100)` gets 100
// func bar(optionalArg ...int) { return DefaultArg(optionalArg, 42) }
// calling `bar()` gets 42, calling `bar(100)` gets 100
//
// Passing more than 1 item to `optionalArg` or `def` is undefined behavior.
// At the moment it only returns the first argument.
func DefaultArg[T any](optionalArg []T, def ...T) (ret T) {
if len(optionalArg) >= 1 {
return optionalArg[0]
}
return def
}

func DefArgZero[T any](defArgs []T) (ret T) {
return DefArg(defArgs, ret)
if len(def) >= 1 {
return def[0]
}
return ret
}

func ReserveLineBreakForTextarea(input string) string {
Expand Down
13 changes: 13 additions & 0 deletions modules/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,16 @@ func TestReserveLineBreakForTextarea(t *testing.T) {
assert.Equal(t, ReserveLineBreakForTextarea("test\r\ndata"), "test\ndata")
assert.Equal(t, ReserveLineBreakForTextarea("test\r\ndata\r\n"), "test\ndata\n")
}

func TestDefaultArg(t *testing.T) {
foo := func(other any, optionalArg ...int) int {
return DefaultArg(optionalArg)
}
bar := func(other any, optionalArg ...int) int {
return DefaultArg(optionalArg, 42)
}
assert.Equal(t, 0, foo(nil))
assert.Equal(t, 100, foo(nil, 100))
assert.Equal(t, 42, bar(nil))
assert.Equal(t, 100, bar(nil, 100))
}
4 changes: 2 additions & 2 deletions tests/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func PrepareCleanPackageData(t testing.TB) {

func PrepareTestEnv(t testing.TB, skip ...int) func() {
t.Helper()
deferFn := PrintCurrentTest(t, util.DefArgZero(skip)+1)
deferFn := PrintCurrentTest(t, util.DefaultArg(skip)+1)

// load database fixtures
assert.NoError(t, unittest.LoadFixtures())
Expand All @@ -276,7 +276,7 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {

func PrintCurrentTest(t testing.TB, skip ...int) func() {
t.Helper()
return testlogger.PrintCurrentTest(t, util.DefArgZero(skip)+1)
return testlogger.PrintCurrentTest(t, util.DefaultArg(skip)+1)
}

// Printf takes a format and args and prints the string to os.Stdout
Expand Down

0 comments on commit ea9e0ae

Please sign in to comment.