internal/common: add helper functions to get pointer to a basic type value
Add two new helper functions `IntToPtr()` and `BoolToPtr()` to the `internal/common` package, which can be used to conveniently set basic type literals in a struct literal, in which pointer to the basic type is expected. Signed-off-by: Tomas Hozza <thozza@redhat.com>
This commit is contained in:
parent
78ef247042
commit
9386a02984
2 changed files with 28 additions and 0 deletions
9
internal/common/pointers.go
Normal file
9
internal/common/pointers.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package common
|
||||
|
||||
func IntToPtr(x int) *int {
|
||||
return &x
|
||||
}
|
||||
|
||||
func BoolToPtr(x bool) *bool {
|
||||
return &x
|
||||
}
|
||||
19
internal/common/pointers_test.go
Normal file
19
internal/common/pointers_test.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIntToPtr(t *testing.T) {
|
||||
var value int = 42
|
||||
got := IntToPtr(value)
|
||||
assert.Equal(t, value, *got)
|
||||
}
|
||||
|
||||
func TestBoolToPtr(t *testing.T) {
|
||||
var value bool = true
|
||||
got := BoolToPtr(value)
|
||||
assert.Equal(t, value, *got)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue