adding support for quick pointer convertion

This commit is contained in:
0syntrax0 2024-10-21 15:58:57 -05:00
parent 507e604582
commit 1e26bbee7a
2 changed files with 58 additions and 0 deletions

13
pointers.go Normal file
View file

@ -0,0 +1,13 @@
package underscore
// Convert values to pointers
//
// Instead of:
// v = "value"
// MyPointerVar = &v
//
// Use:
// MyPointerVar = ToPointer("value")
func ToPointer[T any](in T) *T {
return &in
}

45
pointers_test.go Normal file
View file

@ -0,0 +1,45 @@
package underscore_test
import (
"reflect"
"testing"
u "github.com/rjNemo/underscore"
"github.com/stretchr/testify/assert"
)
func TestPointers(t *testing.T) {
variable := 123
var object struct{}
cases := []struct {
value any
expected bool
}{
{
value: u.ToPointer("myValue"),
expected: true,
},
{
value: u.ToPointer(variable),
expected: true,
},
{
value: &variable,
expected: true,
},
{
value: nil,
expected: false,
},
{
value: u.ToPointer(object),
expected: true,
},
}
for _, c := range cases {
got := (reflect.ValueOf(c.value).Kind() == reflect.Ptr)
assert.Equal(t, c.expected, got)
}
}