Skip to content

Commit

Permalink
Merge pull request #10 from AlekSi/generics
Browse files Browse the repository at this point in the history
Add generic functions
  • Loading branch information
AlekSi committed Oct 22, 2021
2 parents 2f822e1 + 974a39b commit 039be97
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
@@ -1,14 +1,15 @@
# pointer
[![GoDoc](https://godoc.org/github.com/AlekSi/pointer?status.svg)](https://godoc.org/github.com/AlekSi/pointer)
[![Build Status](https://travis-ci.org/AlekSi/pointer.svg)](https://travis-ci.org/AlekSi/pointer)

Go package pointer provides helpers to get pointers to values of built-in types.
[![Go Reference](https://pkg.go.dev/badge/github.com/AlekSi/pointer.svg)](https://pkg.go.dev/github.com/AlekSi/pointer)

Go package `pointer` provides helpers to convert between pointers and values of built-in
(and, with Go 1.18+ generics, of any) types.

```
go get github.com/AlekSi/pointer
```

API is stable. [Documentation](http://godoc.org/github.com/AlekSi/pointer).
API is stable. [Documentation](https://pkg.go.dev/github.com/AlekSi/pointer).

```go
package motivationalexample
Expand Down
36 changes: 36 additions & 0 deletions generic.go
@@ -0,0 +1,36 @@
//go:build go1.18
// +build go1.18

package pointer

// To returns a pointer to the passed value.
func To[T any](t T) *T {
return &t
}

// ToOrNil returns a pointer to the passed value, or nil, if the passed value is a zero value.
// If the passed value has `IsZero() bool` method (for example, time.Time instance),
// it is used to determine if the value is zero.
func ToOrNil[T comparable](t T) *T {
if z, ok := any(t).(interface{ IsZero() bool }); ok {
if z.IsZero() {
return nil
}
return &t
}

var zero T
if t == zero {
return nil
}
return &t
}

// Get returns the value from the passed pointer or the zero value if the pointer is nil.
func Get[T any](t *T) T {
if t == nil {
var zero T
return zero
}
return *t
}
33 changes: 33 additions & 0 deletions generic_test.go
@@ -0,0 +1,33 @@
//go:build go1.18
// +build go1.18

package pointer

import (
"testing"
"time"
)

func TestGeneric(t *testing.T) {
var x time.Time
if *To(x) != x {
t.Errorf("*To(%v)", x)
}
if ToOrNil(x) != nil {
t.Errorf("ToOrNil(%v)", x)
}
if Get((*time.Time)(nil)) != x {
t.Errorf("Time(%v)", nil)
}

x = time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)
if *To(x) != x {
t.Errorf("*To(%v)", x)
}
if *ToOrNil(x) != x {
t.Errorf("*ToOrNil(%v)", x)
}
if Get(&x) != x {
t.Errorf("Get(%v)", &x)
}
}
2 changes: 1 addition & 1 deletion go.mod
@@ -1,3 +1,3 @@
module github.com/AlekSi/pointer

go 1.11
go 1.18
2 changes: 1 addition & 1 deletion pointer.go
@@ -1,4 +1,4 @@
// Package pointer provides helpers to get pointers to values of built-in types.
// Package pointer provides helpers to convert between pointers and values of built-in (and, with generics, of any) types.
package pointer // import "github.com/AlekSi/pointer"

import (
Expand Down

0 comments on commit 039be97

Please sign in to comment.