Add a new util to clear a property am: 02d2b9e4cc
am: da4c2df54f
Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/2854407 Change-Id: I100b21657995d7475b2c9fc5eeb2c65d65ccd883 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
034a888f31
2 changed files with 51 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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.")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue