aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-12-06 00:02:10 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-12-06 00:02:10 +0000
commit0762d21e7975b26eb24fe748ed9995202a955150 (patch)
treea8e7d8f19178a884fd7f5efc1ed424d9320daa5c
parent09924b93ec719a7d8c425796013ab5a93f3e947e (diff)
parent9570d1bbedf6b79324cabf57a69a1c4dea66d6a5 (diff)
downloadblueprint-0762d21e7975b26eb24fe748ed9995202a955150.tar.gz
Snap for 11180250 from 9570d1bbedf6b79324cabf57a69a1c4dea66d6a5 to 24Q1-release
Change-Id: I707077b0eb8581d70b82bcc3fc316575807485e5
-rw-r--r--proptools/proptools.go6
-rw-r--r--proptools/proptools_test.go46
2 files changed, 51 insertions, 1 deletions
diff --git a/proptools/proptools.go b/proptools/proptools.go
index 6946d7e..4580d01 100644
--- a/proptools/proptools.go
+++ b/proptools/proptools.go
@@ -53,6 +53,12 @@ func FieldNameForProperty(propertyName string) string {
return fieldName
}
+// Clear takes a pointer to a field and clears the value pointed to by the pointer with zero value.
+func Clear[T any](ptr *T) {
+ var zeroValue T
+ *ptr = zeroValue
+}
+
// BoolPtr returns a pointer to a new bool containing the given value.
func BoolPtr(b bool) *bool {
return &b
diff --git a/proptools/proptools_test.go b/proptools/proptools_test.go
index 207ee1b..0fa0507 100644
--- a/proptools/proptools_test.go
+++ b/proptools/proptools_test.go
@@ -14,7 +14,9 @@
package proptools
-import "testing"
+import (
+ "testing"
+)
func TestPropertyNameForField(t *testing.T) {
tests := []struct {
@@ -112,3 +114,45 @@ func TestFieldNameForProperty(t *testing.T) {
})
}
}
+
+func TestClearField(t *testing.T) {
+ props := struct {
+ i int
+ s string
+ ps *string
+ ss []string
+ c struct {
+ n int
+ }
+ }{}
+
+ props.i = 42
+ Clear(&props.i)
+ if props.i != 0 {
+ t.Error("int field is not cleared to zero.")
+ }
+
+ props.s = "foo"
+ Clear(&props.s)
+ if props.s != "" {
+ t.Error("string field is not cleared to zero.")
+ }
+
+ props.ps = StringPtr("foo")
+ Clear(&props.ps)
+ if props.ps != nil {
+ t.Error("string pointer field is not cleared to zero.")
+ }
+
+ props.ss = []string{"foo"}
+ Clear(&props.ss)
+ if props.ss != nil {
+ t.Error("string array field is not cleared to zero.")
+ }
+
+ props.c.n = 42
+ Clear(&props.c)
+ if props.c.n != 0 {
+ t.Error("struct field is not cleared to zero.")
+ }
+}