Replace FixturePreparer.visit() with list()
The visit() method was dropped in favor of a list() method as the
latter makes it easier to treat fixtureFactory, simpleFixturePreparer
and compositeFixturePreparer as simple collections of already flattened
and deduped *simpleFixturePreparers. That will be used in a follow up
change.
Bug: 183235980
Test: m nothing
Change-Id: Ia41ae9bd4f9c39c85865ba698e0da6be00519caf
diff --git a/android/fixture.go b/android/fixture.go
index 47dd818..85e0bed 100644
--- a/android/fixture.go
+++ b/android/fixture.go
@@ -429,20 +429,10 @@
}
}
-type simpleFixturePreparerVisitor func(preparer *simpleFixturePreparer)
-
// FixturePreparer is an opaque interface that can change a fixture.
type FixturePreparer interface {
- // visit calls the supplied visitor with each *simpleFixturePreparer instances in this preparer,
- visit(simpleFixturePreparerVisitor)
-}
-
-type fixturePreparers []FixturePreparer
-
-func (f fixturePreparers) visit(visitor simpleFixturePreparerVisitor) {
- for _, p := range f {
- p.visit(visitor)
- }
+ // Return the flattened and deduped list of simpleFixturePreparer pointers.
+ list() []*simpleFixturePreparer
}
// dedupAndFlattenPreparers removes any duplicates and flattens any composite FixturePreparer
@@ -457,7 +447,7 @@
//
// Returns a deduped and flattened list of the preparers starting with the ones in base with any
// additional ones from the preparers list added afterwards.
-func dedupAndFlattenPreparers(base []*simpleFixturePreparer, preparers fixturePreparers) []*simpleFixturePreparer {
+func dedupAndFlattenPreparers(base []*simpleFixturePreparer, preparers []FixturePreparer) []*simpleFixturePreparer {
if len(preparers) == 0 {
return base
}
@@ -473,24 +463,27 @@
list[i] = s
}
- preparers.visit(func(preparer *simpleFixturePreparer) {
- if _, seen := visited[preparer]; !seen {
- visited[preparer] = struct{}{}
- list = append(list, preparer)
+ for _, p := range preparers {
+ for _, s := range p.list() {
+ if _, seen := visited[s]; !seen {
+ visited[s] = struct{}{}
+ list = append(list, s)
+ }
}
- })
+ }
+
return list
}
// compositeFixturePreparer is a FixturePreparer created from a list of fixture preparers.
type compositeFixturePreparer struct {
+ // The flattened and deduped list of simpleFixturePreparer pointers encapsulated within this
+ // composite preparer.
preparers []*simpleFixturePreparer
}
-func (c *compositeFixturePreparer) visit(visitor simpleFixturePreparerVisitor) {
- for _, p := range c.preparers {
- p.visit(visitor)
- }
+func (c *compositeFixturePreparer) list() []*simpleFixturePreparer {
+ return c.preparers
}
// simpleFixturePreparer is a FixturePreparer that applies a function to a fixture.
@@ -498,8 +491,8 @@
function func(fixture *fixture)
}
-func (s *simpleFixturePreparer) visit(visitor simpleFixturePreparerVisitor) {
- visitor(s)
+func (s *simpleFixturePreparer) list() []*simpleFixturePreparer {
+ return []*simpleFixturePreparer{s}
}
func newSimpleFixturePreparer(preparer func(fixture *fixture)) FixturePreparer {