Skip to content

Commit e2ac317

Browse files
committed
use OptionalArg
1 parent ea9e0ae commit e2ac317

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

modules/testlogger/testlogger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (w *testLoggerWriterCloser) Reset() {
9393
func PrintCurrentTest(t testing.TB, skip ...int) func() {
9494
t.Helper()
9595
start := time.Now()
96-
actualSkip := util.DefaultArg(skip) + 1
96+
actualSkip := util.OptionalArg(skip) + 1
9797
_, filename, line, _ := runtime.Caller(actualSkip)
9898

9999
if log.CanColorStdout {

modules/util/util.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,21 @@ func IfZero[T comparable](v, def T) T {
230230
return v
231231
}
232232

233-
// DefaultArg helps the "optional argument" in Golang:
233+
// OptionalArg helps the "optional argument" in Golang:
234234
//
235-
// func foo(optionalArg ...int) { return DefaultArg(optionalArg) }
236-
// calling `foo()` gets 0, calling `foo(100)` gets 100
237-
// func bar(optionalArg ...int) { return DefaultArg(optionalArg, 42) }
238-
// calling `bar()` gets 42, calling `bar(100)` gets 100
235+
// func foo(optArg ...int) { return OptionalArg(optArg) }
236+
// calling `foo()` gets zero value 0, calling `foo(100)` gets 100
237+
// func bar(optArg ...int) { return OptionalArg(optArg, 42) }
238+
// calling `bar()` gets default value 42, calling `bar(100)` gets 100
239239
//
240-
// Passing more than 1 item to `optionalArg` or `def` is undefined behavior.
241-
// At the moment it only returns the first argument.
242-
func DefaultArg[T any](optionalArg []T, def ...T) (ret T) {
243-
if len(optionalArg) >= 1 {
244-
return optionalArg[0]
240+
// Passing more than 1 item to `optArg` or `defaultValue` is undefined behavior.
241+
// At the moment only the first item is used.
242+
func OptionalArg[T any](optArg []T, defaultValue ...T) (ret T) {
243+
if len(optArg) >= 1 {
244+
return optArg[0]
245245
}
246-
if len(def) >= 1 {
247-
return def[0]
246+
if len(defaultValue) >= 1 {
247+
return defaultValue[0]
248248
}
249249
return ret
250250
}

modules/util/util_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ func TestReserveLineBreakForTextarea(t *testing.T) {
242242
}
243243

244244
func TestDefaultArg(t *testing.T) {
245-
foo := func(other any, optionalArg ...int) int {
246-
return DefaultArg(optionalArg)
245+
foo := func(other any, optArg ...int) int {
246+
return OptionalArg(optArg)
247247
}
248-
bar := func(other any, optionalArg ...int) int {
249-
return DefaultArg(optionalArg, 42)
248+
bar := func(other any, optArg ...int) int {
249+
return OptionalArg(optArg, 42)
250250
}
251251
assert.Equal(t, 0, foo(nil))
252252
assert.Equal(t, 100, foo(nil, 100))

tests/test_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func PrepareCleanPackageData(t testing.TB) {
262262

263263
func PrepareTestEnv(t testing.TB, skip ...int) func() {
264264
t.Helper()
265-
deferFn := PrintCurrentTest(t, util.DefaultArg(skip)+1)
265+
deferFn := PrintCurrentTest(t, util.OptionalArg(skip)+1)
266266

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

277277
func PrintCurrentTest(t testing.TB, skip ...int) func() {
278278
t.Helper()
279-
return testlogger.PrintCurrentTest(t, util.DefaultArg(skip)+1)
279+
return testlogger.PrintCurrentTest(t, util.OptionalArg(skip)+1)
280280
}
281281

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

0 commit comments

Comments
 (0)