diff --git a/ptr/ptr.go b/ptr/ptr.go index 356464733..7b7d581f9 100644 --- a/ptr/ptr.go +++ b/ptr/ptr.go @@ -16,6 +16,8 @@ limitations under the License. package ptr +import "time" + // Int32 is a helper for turning integers into pointers for use in // API types that want *int32. func Int32(i int32) *int32 { @@ -39,3 +41,9 @@ func Bool(b bool) *bool { func String(s string) *string { return &s } + +// Duration is a helper for turning time.Duration into pointers for use in +// API types that want *time.Duration. +func Duration(t time.Duration) *time.Duration { + return &t +} diff --git a/ptr/ptr_test.go b/ptr/ptr_test.go index 4e7019d1e..e8c14c18a 100644 --- a/ptr/ptr_test.go +++ b/ptr/ptr_test.go @@ -16,7 +16,10 @@ limitations under the License. package ptr -import "testing" +import ( + "testing" + "time" +) func TestInt32(t *testing.T) { want := int32(55) @@ -49,3 +52,19 @@ func TestString(t *testing.T) { t.Errorf("String() = &%v, wanted %v", *gotPtr, want) } } + +func TestDuration(t *testing.T) { + want := 42 * time.Second + gotPtr := Duration(want) + if want != *gotPtr { + t.Errorf("Duration() = &%v, wanted %v", *gotPtr, want) + } +} + +func TestDurationWithConst(t *testing.T) { + const want = 42 * time.Second + gotPtr := Duration(want) + if want != *gotPtr { + t.Errorf("Duration() = &%v, wanted %v", *gotPtr, want) + } +}