-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from AlekSi/generics
Add generic functions
- Loading branch information
Showing
5 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters