add Partition method to LabelListAttribute
It can be tedious to split a LabelListAttribute on all of the
configuration axes, so LabelListAttribute.Partition can be used to do
this based on a predicate function.
Test: go test ./bazel
Change-Id: I78ff8c24ce9c86bab5896290d8afe979d56545b6
diff --git a/bazel/properties.go b/bazel/properties.go
index 963e27b..7bbfea4 100644
--- a/bazel/properties.go
+++ b/bazel/properties.go
@@ -119,7 +119,7 @@
return dirs
}
-// Add inserts the label Label at the end of the LabelList.
+// Add inserts the label Label at the end of the LabelList.Includes.
func (ll *LabelList) Add(label *Label) {
if label == nil {
return
@@ -127,6 +127,14 @@
ll.Includes = append(ll.Includes, *label)
}
+// AddExclude inserts the label Label at the end of the LabelList.Excludes.
+func (ll *LabelList) AddExclude(label *Label) {
+ if label == nil {
+ return
+ }
+ ll.Excludes = append(ll.Excludes, *label)
+}
+
// Append appends the fields of other labelList to the corresponding fields of ll.
func (ll *LabelList) Append(other LabelList) {
if len(ll.Includes) > 0 || len(other.Includes) > 0 {
@@ -137,6 +145,30 @@
}
}
+// Partition splits a LabelList into two LabelLists depending on the return value
+// of the predicate.
+// This function preserves the Includes and Excludes, but it does not provide
+// that information to the partition function.
+func (ll *LabelList) Partition(predicate func(label Label) bool) (LabelList, LabelList) {
+ predicated := LabelList{}
+ unpredicated := LabelList{}
+ for _, include := range ll.Includes {
+ if predicate(include) {
+ predicated.Add(&include)
+ } else {
+ unpredicated.Add(&include)
+ }
+ }
+ for _, exclude := range ll.Excludes {
+ if predicate(exclude) {
+ predicated.AddExclude(&exclude)
+ } else {
+ unpredicated.AddExclude(&exclude)
+ }
+ }
+ return predicated, unpredicated
+}
+
// UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns
// the slice in a sorted order.
func UniqueSortedBazelLabels(originalLabels []Label) []Label {
@@ -822,6 +854,29 @@
}
}
+// Partition splits a LabelListAttribute into two LabelListAttributes depending
+// on the return value of the predicate.
+// This function preserves the Includes and Excludes, but it does not provide
+// that information to the partition function.
+func (lla LabelListAttribute) Partition(predicate func(label Label) bool) (LabelListAttribute, LabelListAttribute) {
+ predicated := LabelListAttribute{}
+ unpredicated := LabelListAttribute{}
+
+ valuePartitionTrue, valuePartitionFalse := lla.Value.Partition(predicate)
+ predicated.SetValue(valuePartitionTrue)
+ unpredicated.SetValue(valuePartitionFalse)
+
+ for axis, selectValueLabelLists := range lla.ConfigurableValues {
+ for config, labelList := range selectValueLabelLists {
+ configPredicated, configUnpredicated := labelList.Partition(predicate)
+ predicated.SetSelectValue(axis, config, configPredicated)
+ unpredicated.SetSelectValue(axis, config, configUnpredicated)
+ }
+ }
+
+ return predicated, unpredicated
+}
+
// OtherModuleContext is a limited context that has methods with information about other modules.
type OtherModuleContext interface {
ModuleFromName(name string) (blueprint.Module, bool)