Adding Duration ptr helper. (#519)

* Adding Time and Duration ptr helpers.

* use do duration.

* nit picky reviwers get nit picky comments.
This commit is contained in:
Scott Nichols 2019-07-18 19:10:28 -04:00 committed by Knative Prow Robot
parent a7dbe91220
commit 540853ba6b
2 changed files with 28 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
}
}