Skip to content

Commit

Permalink
use OptionalArg
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Nov 1, 2024
1 parent ea9e0ae commit e2ac317
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 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.DefaultArg(skip) + 1
actualSkip := util.OptionalArg(skip) + 1
_, filename, line, _ := runtime.Caller(actualSkip)

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

// DefaultArg helps the "optional argument" in Golang:
// OptionalArg 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
// func foo(optArg ...int) { return OptionalArg(optArg) }
// calling `foo()` gets zero value 0, calling `foo(100)` gets 100
// func bar(optArg ...int) { return OptionalArg(optArg, 42) }
// calling `bar()` gets default value 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]
// Passing more than 1 item to `optArg` or `defaultValue` is undefined behavior.
// At the moment only the first item is used.
func OptionalArg[T any](optArg []T, defaultValue ...T) (ret T) {
if len(optArg) >= 1 {
return optArg[0]
}
if len(def) >= 1 {
return def[0]
if len(defaultValue) >= 1 {
return defaultValue[0]
}
return ret
}
Expand Down
8 changes: 4 additions & 4 deletions modules/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ func TestReserveLineBreakForTextarea(t *testing.T) {
}

func TestDefaultArg(t *testing.T) {
foo := func(other any, optionalArg ...int) int {
return DefaultArg(optionalArg)
foo := func(other any, optArg ...int) int {
return OptionalArg(optArg)
}
bar := func(other any, optionalArg ...int) int {
return DefaultArg(optionalArg, 42)
bar := func(other any, optArg ...int) int {
return OptionalArg(optArg, 42)
}
assert.Equal(t, 0, foo(nil))
assert.Equal(t, 100, foo(nil, 100))
Expand Down
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.DefaultArg(skip)+1)
deferFn := PrintCurrentTest(t, util.OptionalArg(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.DefaultArg(skip)+1)
return testlogger.PrintCurrentTest(t, util.OptionalArg(skip)+1)
}

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

0 comments on commit e2ac317

Please sign in to comment.