Merge changes from topics "replace_instead_of_append", "selects_get_and_get_default" into main

* changes:
  Rename Evaluate() to Get() and add GetDefault()
  Add tests for android:replace_instead_of_append
This commit is contained in:
Cole Faust 2024-04-08 20:45:55 +00:00 committed by Gerrit Code Review
commit 42a8b256af
3 changed files with 88 additions and 16 deletions

View file

@ -980,7 +980,7 @@ func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.S
panic(fmt.Errorf("unexpected tag format %q", field.Tag))
}
// these tags don't need to be present in the runtime generated struct type.
values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path", "replace_instead_of_append"})
if len(values) > 0 {
panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
}

View file

@ -109,9 +109,9 @@ func pathPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{})
case reflect.Struct:
intf := sv.Interface()
if configurable, ok := intf.(proptools.Configurable[string]); ok {
ret = append(ret, proptools.String(configurable.Evaluate(ctx)))
ret = append(ret, configurable.GetOrDefault(ctx, ""))
} else if configurable, ok := intf.(proptools.Configurable[[]string]); ok {
ret = append(ret, proptools.Slice(configurable.Evaluate(ctx))...)
ret = append(ret, configurable.GetOrDefault(ctx, nil)...)
} else {
panic(fmt.Errorf(`field %s in type %s has tag android:"path" but is not a string or slice of strings, it is a %s`,
v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))

View file

@ -368,14 +368,62 @@ func TestSelects(t *testing.T) {
my_bool: proptools.BoolPtr(true),
},
},
{
name: "defaults with lists are appended",
bp: `
my_module_type {
name: "foo",
defaults: ["bar"],
my_string_list: select(soong_config_variable("my_namespace", "my_variable"), {
"a": ["a1"],
default: ["b1"],
}),
}
my_defaults {
name: "bar",
my_string_list: select(soong_config_variable("my_namespace", "my_variable2"), {
"a": ["a2"],
default: ["b2"],
}),
}
`,
provider: selectsTestProvider{
my_string_list: &[]string{"b2", "b1"},
},
},
{
name: "Replacing string list",
bp: `
my_module_type {
name: "foo",
defaults: ["bar"],
replacing_string_list: select(soong_config_variable("my_namespace", "my_variable"), {
"a": ["a1"],
default: ["b1"],
}),
}
my_defaults {
name: "bar",
replacing_string_list: select(soong_config_variable("my_namespace", "my_variable2"), {
"a": ["a2"],
default: ["b2"],
}),
}
`,
provider: selectsTestProvider{
replacing_string_list: &[]string{"b1"},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fixtures := GroupFixturePreparers(
PrepareForTestWithDefaults,
PrepareForTestWithArchMutator,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("my_module_type", newSelectsMockModule)
ctx.RegisterModuleType("my_defaults", newSelectsMockModuleDefaults)
}),
FixtureModifyProductVariables(func(variables FixtureProductVariables) {
variables.VendorVars = tc.vendorVars
@ -398,10 +446,11 @@ func TestSelects(t *testing.T) {
}
type selectsTestProvider struct {
my_bool *bool
my_string *string
my_string_list *[]string
my_paths *[]string
my_bool *bool
my_string *string
my_string_list *[]string
my_paths *[]string
replacing_string_list *[]string
}
func (p *selectsTestProvider) String() string {
@ -418,16 +467,18 @@ func (p *selectsTestProvider) String() string {
my_string: %s,
my_string_list: %s,
my_paths: %s,
}`, myBoolStr, myStringStr, p.my_string_list, p.my_paths)
replacing_string_list %s,
}`, myBoolStr, myStringStr, p.my_string_list, p.my_paths, p.replacing_string_list)
}
var selectsTestProviderKey = blueprint.NewProvider[selectsTestProvider]()
type selectsMockModuleProperties struct {
My_bool proptools.Configurable[bool]
My_string proptools.Configurable[string]
My_string_list proptools.Configurable[[]string]
My_paths proptools.Configurable[[]string] `android:"path"`
My_bool proptools.Configurable[bool]
My_string proptools.Configurable[string]
My_string_list proptools.Configurable[[]string]
My_paths proptools.Configurable[[]string] `android:"path"`
Replacing_string_list proptools.Configurable[[]string] `android:"replace_instead_of_append,arch_variant"`
}
type selectsMockModule struct {
@ -438,10 +489,11 @@ type selectsMockModule struct {
func (p *selectsMockModule) GenerateAndroidBuildActions(ctx ModuleContext) {
SetProvider(ctx, selectsTestProviderKey, selectsTestProvider{
my_bool: p.properties.My_bool.Evaluate(ctx),
my_string: p.properties.My_string.Evaluate(ctx),
my_string_list: p.properties.My_string_list.Evaluate(ctx),
my_paths: p.properties.My_paths.Evaluate(ctx),
my_bool: p.properties.My_bool.Get(ctx),
my_string: p.properties.My_string.Get(ctx),
my_string_list: p.properties.My_string_list.Get(ctx),
my_paths: p.properties.My_paths.Get(ctx),
replacing_string_list: p.properties.Replacing_string_list.Get(ctx),
})
}
@ -452,3 +504,23 @@ func newSelectsMockModule() Module {
InitDefaultableModule(m)
return m
}
type selectsMockModuleDefaults struct {
ModuleBase
DefaultsModuleBase
}
func (d *selectsMockModuleDefaults) GenerateAndroidBuildActions(ctx ModuleContext) {
}
func newSelectsMockModuleDefaults() Module {
module := &selectsMockModuleDefaults{}
module.AddProperties(
&selectsMockModuleProperties{},
)
InitDefaultsModule(module)
return module
}