common: merge all *ToPtr methods to one generic ToPtr

After introducing Go 1.18 to a project, it's required by law to convert at
least one method to a generic one.

Everyone hates IntToPtr, StringToPtr, BoolToPtr and Uint64ToPtr, so let's
convert them to the ultimate generic ToPtr one.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
This commit is contained in:
Ondřej Budai 2023-01-06 12:42:02 +01:00 committed by Achilleas Koutsou
parent 0359647a82
commit b997142db0
46 changed files with 397 additions and 392 deletions

View file

@ -1,17 +1,5 @@
package common
func IntToPtr(x int) *int {
return &x
}
func Uint64ToPtr(x uint64) *uint64 {
return &x
}
func BoolToPtr(x bool) *bool {
return &x
}
func StringToPtr(x string) *string {
func ToPtr[T any](x T) *T {
return &x
}

View file

@ -6,26 +6,21 @@ import (
"github.com/stretchr/testify/assert"
)
func TestIntToPtr(t *testing.T) {
var value int = 42
got := IntToPtr(value)
assert.Equal(t, value, *got)
}
func TestToPtr(t *testing.T) {
var valueInt int = 42
gotInt := ToPtr(valueInt)
assert.Equal(t, valueInt, *gotInt)
func TestBoolToPtr(t *testing.T) {
var value bool = true
got := BoolToPtr(value)
assert.Equal(t, value, *got)
}
var valueBool bool = true
gotBool := ToPtr(valueBool)
assert.Equal(t, valueBool, *gotBool)
func TestUint64ToPtr(t *testing.T) {
var value uint64 = 1
got := Uint64ToPtr(value)
assert.Equal(t, value, *got)
}
var valueUint64 uint64 = 1
gotUint64 := ToPtr(valueUint64)
assert.Equal(t, valueUint64, *gotUint64)
var valueStr string = "the-greatest-test-value"
gotStr := ToPtr(valueStr)
assert.Equal(t, valueStr, *gotStr)
func TestStringToPtr(t *testing.T) {
var value string = "the-greatest-test-value"
got := StringToPtr(value)
assert.Equal(t, value, *got)
}