From 0a002f59852048337a2185d7d964c7b994bef1c9 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Thu, 5 Sep 2024 11:09:10 -0700 Subject: [PATCH] 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 --- proptools/configurable.go | 8 ++++++-- proptools/configurable_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/proptools/configurable.go b/proptools/configurable.go index 4dfe007..55e4fb7 100644 --- a/proptools/configurable.go +++ b/proptools/configurable.go @@ -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, diff --git a/proptools/configurable_test.go b/proptools/configurable_test.go index f3ea417..f608a45 100644 --- a/proptools/configurable_test.go +++ b/proptools/configurable_test.go @@ -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 {