Skip to content

Commit

Permalink
chore(helpers): Enhance the Default and DefaultFunc functionality…
Browse files Browse the repository at this point in the history
… and add `DefaultFunc`

Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 committed Apr 25, 2024
1 parent 29c3a64 commit fc4ac52
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
44 changes: 35 additions & 9 deletions helpers/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,54 @@ func When[T any](condition bool, callbacks func() T, defaults ...T) T {
return zero
}

// Default returns defaultValue if value is zero, otherwise value.
// Default returns the first non-zero value.
// If all values are zero, return the zero value.
//
// Default("", "foo") // "foo"
// Default("bar", "foo") // "bar"
func Default[T comparable](value T, defaultValue T) T {
// Default("", "", "foo") // "foo"
func Default[T comparable](values ...T) T {
var zero T
if value == zero {
return defaultValue
for _, value := range values {
if value != zero {
return value
}
}
return value
return zero

Check warning on line 133 in helpers/values.go

View check run for this annotation

Codecov / codecov/patch

helpers/values.go#L133

Added line #L133 was not covered by tests
}

func DefaultFunc[T comparable](callbacks ...func() T) T {
var zero, value T
for _, callback := range callbacks {
if callback != nil {
value = callback()
if value != zero {
return value
}
}
}
return zero

Check warning on line 146 in helpers/values.go

View check run for this annotation

Codecov / codecov/patch

helpers/values.go#L146

Added line #L146 was not covered by tests
}

// DefaultWithFunc returns defaultValue if value is zero, otherwise value.
//
// DefaultWithFunc("", func() string { return "foo" }) // "foo"
// DefaultWithFunc("bar", func() string { return "foo" }) // "bar"
func DefaultWithFunc[T comparable](value T, defaultValue func() T) T {
// DefaultWithFunc("", func() string { return "" }, func() string { return "foo" }) // "foo"
func DefaultWithFunc[T comparable](value T, callbacks ...func() T) T {
var zero T
if value == zero {
return defaultValue()
if value != zero {
return value

Check warning on line 157 in helpers/values.go

View check run for this annotation

Codecov / codecov/patch

helpers/values.go#L157

Added line #L157 was not covered by tests
}
return value
for _, callback := range callbacks {
if callback != nil {
value = callback()
if value != zero {
return value
}
}
}
return zero

Check warning on line 167 in helpers/values.go

View check run for this annotation

Codecov / codecov/patch

helpers/values.go#L167

Added line #L167 was not covered by tests
}

// Ptr returns a pointer to the value.
Expand Down
61 changes: 60 additions & 1 deletion helpers/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,60 @@ func TestDefault(t *testing.T) {
// ptr
got4 := Default(nil, &foo{Name: "bar"})
assert.Equal(t, "bar", got4.Name)

// more values
got5 := Default(0, 10, 20, 30)
assert.Equal(t, 10, got5)

got6 := Default(0, 0, 20)
assert.Equal(t, 20, got6)
}

func TestDefaultWith(t *testing.T) {
func TestDefaultFunc(t *testing.T) {
// string
got := DefaultFunc(func() string {
return ""
}, func() string {
return "foo"
})
assert.Equal(t, "foo", got)

// int
got2 := DefaultFunc(func() int {
return 0
}, func() int {
return 10
})
assert.Equal(t, 10, got2)

// struct
got3 := DefaultFunc(func() foo {
return foo{}
}, func() foo {
return foo{Name: "bar"}
})
assert.Equal(t, "bar", got3.Name)

// ptr
got4 := DefaultFunc(func() *foo {
return nil
}, func() *foo {
return &foo{Name: "bar"}
})
assert.Equal(t, "bar", got4.Name)

// more values
got5 := DefaultFunc(func() int {
return 0
}, func() int {
return 0
}, func() int {
return 10
})
assert.Equal(t, 10, got5)
}

func TestDefaultWithFunc(t *testing.T) {
// string
got := DefaultWithFunc("", func() string {
return "foo"
Expand All @@ -269,6 +320,14 @@ func TestDefaultWith(t *testing.T) {
return &foo{Name: "bar"}
})
assert.Equal(t, "bar", got4.Name)

// more values
got5 := DefaultWithFunc(0, func() int {
return 0
}, func() int {
return 10
})
assert.Equal(t, 10, got5)
}

func TestPtrAndVal(t *testing.T) {
Expand Down

0 comments on commit fc4ac52

Please sign in to comment.