mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
adding support for quick pointer convertion
This commit is contained in:
parent
507e604582
commit
1e26bbee7a
2 changed files with 58 additions and 0 deletions
13
pointers.go
Normal file
13
pointers.go
Normal 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
45
pointers_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue