Fix postProcessors having value semantics when unset

Currently, if postProcessors was empty, it would be set to a new
pointer value. But that would only apply for the current Configurable,
so if you passed a Configurable by value to a function and then
that function added a postProcessor, the caller wouldn't see that.

Configurable properties in general have a confusing mix of pointer
and value semantics (thanks go...), this fixes one part of it but in
followup cls I'll try to make it consistent.

Bug: 362579941
Test: m nothing --no-skip-soong-tests
Change-Id: Ib33497768be3af45233cf1a682adffb28ee64bfd
This commit is contained in:
Cole Faust 2024-09-05 11:09:10 -07:00 committed by Bartłomiej Rudecki
parent c472e38ec1
commit 0a002f5985
Signed by: przekichane
GPG key ID: 751F23C6F014EF76
2 changed files with 24 additions and 2 deletions

View file

@ -502,8 +502,12 @@ func (c *Configurable[T]) AddPostProcessor(p func(T) T) {
// See Configurable.evaluate for more details on the postProcessors algorithm
// and data structure.
num_links := c.inner.numLinks()
if c.postProcessors == nil || len(*c.postProcessors) == 0 {
c.postProcessors = &[][]postProcessor[T]{{{
if c.postProcessors == nil {
var nilCases []ConfigurableCase[T]
c.initialize(nil, "", nil, nilCases)
}
if len(*c.postProcessors) == 0 {
*c.postProcessors = [][]postProcessor[T]{{{
f: p,
start: 0,
end: num_links,

View file

@ -43,6 +43,24 @@ func TestPostProcessor(t *testing.T) {
}
}
func TestPostProcessorWhenPassedToHelperFunction(t *testing.T) {
prop := NewConfigurable[[]string](nil, nil)
prop.AppendSimpleValue([]string{"a"})
prop.AppendSimpleValue([]string{"b"})
helper := func(p Configurable[[]string]) {
p.AddPostProcessor(addToElements("1"))
}
helper(prop)
expected := []string{"a1", "b1"}
x := prop.Get(&configurableEvalutorForTesting{})
if !reflect.DeepEqual(x.Get(), expected) {
t.Fatalf("Expected %v, got %v", expected, x.Get())
}
}
func addToElements(s string) func([]string) []string {
return func(arr []string) []string {
for i := range arr {