Repository: knative/pkg
Pull request #2172 (2021-06-28 21:03:36)

Added inverse functions for the ptr package
<!-- Thanks for sending a pull request! -->

# Changes

<!-- 
Describe your changes here- ideally you can get that description straight from
your descriptive commit message(s)! 

- :gift: Add new feature
- :bug: Fix bug
- :broom: Update or clean up current behavior
- :wastebasket: Remove feature or internal logic
-->

- :gift: Added corresponding Value functions for each ptr function. 

<!--
In addition, categorize the changes you're making using the "/kind" Prow command, example:

/kind <kind>

Supported kinds are: api-change, bug, cleanup, deprecation, removal, documentation, enhancement, performance

-->
/kind enhancement

<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*

Fixes #
-->


No issue, I just find myself needing these functions to avoid NPE errors in defaulting scenarios. This also exists within the [aws sdk](https://github.com/aws/aws-sdk-go/blob/e2d6cb448883e4f4fcc5246650f89bde349041ec/aws/convert_types.go), but I don't want to import *that* kitchen sink.

<!-- Please include the 'why' behind your changes if no issue exists -->

**Release Note**

<!--
:page_facing_up: If this change has user-visible impact, write a release note in the block
below. Include the string "action required" if additional action is required of
users switching to the new release, for example in case of a breaking change.

Write as if you are speaking to users, not other Knative contributors. If this
change has no user-visible impact, no release-note is needed.
-->
```release-note
Added corresponding Value functions for each ptr function.
```

**Docs**

<!--
:book: If this change has user-visible impact, link to an issue or PR in
https://github.com/knative/docs.

See here for guidance on getting permanent links to files: https://help.github.com/en/articles/getting-permanent-links-to-files

Please use the following format for linking documentation:
- [knative/docs]: <issue or pr link>
- [Feature Track]: <link>
- [Usage]: <link>
- [Other doc]: <link>

```docs

```
-->

knative-prow-robot: Hi @ellistarn. Thanks for your PR.

I'm waiting for a [knative](https://github.com/orgs/knative/people) member to verify that this patch is reasonable to test. If it is, they should reply with `/ok-to-test` on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should [join the org](https://github.com/orgs/knative/people) to skip this step.

Once the patch is verified, the new status will be reflected by the `ok-to-test` label.

I understand the commands that are listed [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>

knative-prow-robot: Welcome @ellistarn! It looks like this is your first PR to knative/pkg 🎉
knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2172#" title="Author self-approved">ellistarn</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **vagababov** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @vagababov` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/knative/pkg/blob/main/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["vagababov"]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/knative/pkg/pull/2172?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) Report
> Merging [#2172](https://codecov.io/gh/knative/pkg/pull/2172?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (42ff51b) into [main](https://codecov.io/gh/knative/pkg/commit/cf1994e9cc24db63cbc8a21e1a84152b732ad3f0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (cf1994e) will **decrease** coverage by `0.08%`.
> The diff coverage is `33.33%`.

[![Impacted file tree graph](https://codecov.io/gh/knative/pkg/pull/2172/graphs/tree.svg?width=650&height=150&src=pr&token=rlqpDnfxbO&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)](https://codecov.io/gh/knative/pkg/pull/2172?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)

```diff
@@            Coverage Diff             @@
##             main    #2172      +/-   ##
==========================================
- Coverage   65.96%   65.88%   -0.09%     
==========================================
  Files         223      224       +1     
  Lines        9402     9426      +24     
==========================================
+ Hits         6202     6210       +8     
- Misses       2920     2928       +8     
- Partials      280      288       +8     
```


| [Impacted Files](https://codecov.io/gh/knative/pkg/pull/2172?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) | Coverage Δ | |
|---|---|---|
| [ptr/value.go](https://codecov.io/gh/knative/pkg/pull/2172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-cHRyL3ZhbHVlLmdv) | `33.33% <33.33%> (ø)` | |
| [test/gcs/mock/mock.go](https://codecov.io/gh/knative/pkg/pull/2172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-dGVzdC9nY3MvbW9jay9tb2NrLmdv) | `91.39% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative/pkg/pull/2172?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative/pkg/pull/2172?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Last update [cf1994e...42ff51b](https://codecov.io/gh/knative/pkg/pull/2172?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).

knative-prow-robot: @ellistarn: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-knative-pkg-unit-tests | 667fe2f906a59303e9bdbb657cac3406b9fbf929 | [link](https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_pkg/2172/pull-knative-pkg-unit-tests/1409634941444034560) | `/test pull-knative-pkg-unit-tests`

[Full PR test history](https://gubernator.knative.dev/pr/knative_pkg/2172). [Your PR dashboard](https://gubernator.knative.dev/pr/ellistarn).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
ellistarn: Hang on -- I clobbered some of my tests =/ 
knative-prow-robot: @ellistarn: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-knative-pkg-unit-tests | ba21fdf8536f500e54d85550f5a4a3fe9cd414f1 | [link](https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_pkg/2172/pull-knative-pkg-unit-tests/1409639449960124416) | `/test pull-knative-pkg-unit-tests`

[Full PR test history](https://gubernator.knative.dev/pr/knative_pkg/2172). [Your PR dashboard](https://gubernator.knative.dev/pr/ellistarn).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
knative-prow-robot: @ellistarn: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-knative-pkg-unit-tests | aa897eb96efc04bb8f524684e4c6b74cd4254729 | [link](https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_pkg/2172/pull-knative-pkg-unit-tests/1409640970680537088) | `/test pull-knative-pkg-unit-tests`

[Full PR test history](https://gubernator.knative.dev/pr/knative_pkg/2172). [Your PR dashboard](https://gubernator.knative.dev/pr/ellistarn).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
ellistarn: /test pull-knative-pkg-unit-tests
knative-prow-robot: @ellistarn: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-knative-pkg-unit-tests | aa897eb96efc04bb8f524684e4c6b74cd4254729 | [link](https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_pkg/2172/pull-knative-pkg-unit-tests/1409642566579654656) | `/test pull-knative-pkg-unit-tests`

[Full PR test history](https://gubernator.knative.dev/pr/knative_pkg/2172). [Your PR dashboard](https://gubernator.knative.dev/pr/ellistarn).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
n3wscott: well... come now flakes!

/test pull-knative-pkg-unit-tests
ellistarn: ![image](https://user-images.githubusercontent.com/2585992/123713402-2e7af180-d829-11eb-9ff2-956344b3e305.png)

n3wscott: /lgtm
/approve
knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2172#" title="Author self-approved">ellistarn</a>*, *<a href="https://github.com/knative/pkg/pull/2172#issuecomment-870098921" title="Approved">n3wscott</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative/pkg/blob/main/OWNERS)~~ [n3wscott]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/ptr/value.go b/ptr/value.go
new file mode 100644
index 000000000..148bf2105
--- /dev/null
+++ b/ptr/value.go
@@ -0,0 +1,91 @@
+/*
+Copyright 2021 The Knative Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package ptr
+
+import "time"
+
+// Int32Value is a helper for turning pointers to integers into values for use
+// in API types that want int32.
+func Int32Value(i *int32) int32 {
+	if i == nil {
+		return 0
+	}
+	return *i
+}
+
+// Int64Value is a helper for turning pointers to integers into values for use
+// in API types that want int64.
+func Int64Value(i *int64) int64 {
+	if i == nil {
+		return 0
+	}
+	return *i
+}
+
+// Float32Value is a helper for turning pointers to floats into values for use
+// in API types that want float32.
+func Float32Value(f *float32) float32 {
+	if f == nil {
+		return 0
+	}
+	return *f
+}
+
+// Float64Value is a helper for turning pointers to floats into values for use
+// in API types that want float64.
+func Float64Value(f *float64) float64 {
+	if f == nil {
+		return 0
+	}
+	return *f
+}
+
+// BoolValue is a helper for turning pointers to bools into values for use in
+// API types that want bool.
+func BoolValue(b *bool) bool {
+	if b == nil {
+		return false
+	}
+	return *b
+}
+
+// StringValue is a helper for turning pointers to strings into values for use
+// in API types that want string.
+func StringValue(s *string) string {
+	if s == nil {
+		return ""
+	}
+	return *s
+}
+
+// DurationValue is a helper for turning *time.Duration into values for use in
+// API types that want time.Duration.
+func DurationValue(t *time.Duration) time.Duration {
+	if t == nil {
+		return 0
+	}
+	return *t
+}
+
+// TimeValue is a helper for turning *time.Time into values for use in API
+// types that want API types that want time.Time.
+func TimeValue(t *time.Time) time.Time {
+	if t == nil {
+		return time.Time{}
+	}
+	return *t
+}


Test patch:
diff --git a/ptr/value_test.go b/ptr/value_test.go
new file mode 100644
index 000000000..6b4ded41f
--- /dev/null
+++ b/ptr/value_test.go
@@ -0,0 +1,148 @@
+/*
+Copyright 2021 The Knative Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package ptr
+
+import (
+	"testing"
+	"time"
+)
+
+func TestInt32Value(t *testing.T) {
+	want := int32(55)
+	gotValue := Int32Value(&want)
+	if want != gotValue {
+		t.Errorf("Int32() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestInt32Value_Nil(t *testing.T) {
+	want := int32(0)
+	gotValue := Int32Value(nil)
+	if want != gotValue {
+		t.Errorf("Int32() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestInt64Value(t *testing.T) {
+	want := int64(55)
+	gotValue := Int64Value(&want)
+	if want != gotValue {
+		t.Errorf("Int64() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestInt64Value_Nil(t *testing.T) {
+	want := int64(0)
+	gotValue := Int64Value(nil)
+	if want != gotValue {
+		t.Errorf("Int64() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestFloat32Value(t *testing.T) {
+	want := float32(1.25)
+	gotValue := Float32Value(&want)
+	if want != gotValue {
+		t.Errorf("Float32() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestFloat32Value_Nil(t *testing.T) {
+	want := float32(0)
+	gotValue := Float32Value(nil)
+	if want != gotValue {
+		t.Errorf("Float32() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestFloat64Value(t *testing.T) {
+	want := 1.25
+	gotValue := Float64Value(&want)
+	if want != gotValue {
+		t.Errorf("Float64() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestFloat64Value_Nil(t *testing.T) {
+	want := float64(0)
+	gotValue := Float64Value(nil)
+	if want != gotValue {
+		t.Errorf("Float64() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestBoolValue(t *testing.T) {
+	want := true
+	gotValue := BoolValue(&want)
+	if want != gotValue {
+		t.Errorf("Bool() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestBoolValue_Nil(t *testing.T) {
+	want := false
+	gotValue := BoolValue(nil)
+	if want != gotValue {
+		t.Errorf("Bool() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestStringValue(t *testing.T) {
+	want := "should be a pointer"
+	gotValue := StringValue(&want)
+	if want != gotValue {
+		t.Errorf("String() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestStringValue_Nil(t *testing.T) {
+	want := ""
+	gotValue := StringValue(nil)
+	if want != gotValue {
+		t.Errorf("String() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestTimeValue(t *testing.T) {
+	want := time.Now().Add(time.Minute)
+	if got, want := TimeValue(&want), want; got != want {
+		t.Errorf("got = %v, want: %v", got, want)
+	}
+}
+
+func TestTimeValue_Nil(t *testing.T) {
+	want := time.Time{}
+	if got, want := TimeValue(nil), want; got != want {
+		t.Errorf("got = %v, want: %v", got, want)
+	}
+}
+
+func TestDurationValue(t *testing.T) {
+	want := 42 * time.Second
+	gotValue := DurationValue(&want)
+	if want != gotValue {
+		t.Errorf("Duration() = &%v, wanted %v", gotValue, want)
+	}
+}
+
+func TestDurationValue_Nil(t *testing.T) {
+	want := time.Duration(0)
+	gotValue := DurationValue(nil)
+	if want != gotValue {
+		t.Errorf("Duration() = &%v, wanted %v", gotValue, want)
+	}
+}