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:
Tomas Hozza 2021-07-01 12:00:22 +02:00 committed by Ondřej Budai
parent 78ef247042
commit 9386a02984
2 changed files with 28 additions and 0 deletions

View file

@ -0,0 +1,9 @@
package common
func IntToPtr(x int) *int {
return &x
}
func BoolToPtr(x bool) *bool {
return &x
}

View 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)
}