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 authored 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/AlekSi/pointer

go 1.11
go 1.18
2 changes: 1 addition & 1 deletion pointer.go
Original file line number Diff line number Diff line change
@@ -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.