Add ModuleContext method to get dependency by name

Instead of forcing every mutator or module to use VisitDirectDeps
to iterate through its deps, allow it to request a dependency by
name to return the Module.

Change-Id: I18b23bffa324bb9d93a7743b6e2a19c07058c775
This commit is contained in:
Colin Cross 2016-12-13 16:45:06 -08:00
parent 92844f0da8
commit dfb4c9f7af

View file

@ -147,6 +147,9 @@ type ModuleContext interface {
OtherModuleErrorf(m Module, fmt string, args ...interface{}) OtherModuleErrorf(m Module, fmt string, args ...interface{})
OtherModuleDependencyTag(m Module) DependencyTag OtherModuleDependencyTag(m Module) DependencyTag
GetDirectDepWithTag(name string, tag DependencyTag) Module
GetDirectDep(name string) (Module, DependencyTag)
VisitDirectDeps(visit func(Module)) VisitDirectDeps(visit func(Module))
VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
VisitDepsDepthFirst(visit func(Module)) VisitDepsDepthFirst(visit func(Module))
@ -300,6 +303,34 @@ func (m *baseModuleContext) OtherModuleDependencyTag(logicModule Module) Depende
return nil return nil
} }
// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
// name, or nil if none exists.
func (m *baseModuleContext) GetDirectDep(name string) (Module, DependencyTag) {
for _, dep := range m.module.directDeps {
if dep.module.Name() == name {
return dep.module.logicModule, dep.tag
}
}
return nil, nil
}
// GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
// none exists. It panics if the dependency does not have the specified tag.
func (m *baseModuleContext) GetDirectDepWithTag(name string, tag DependencyTag) Module {
for _, dep := range m.module.directDeps {
if dep.module.Name() == name {
if dep.tag != tag {
panic(fmt.Errorf("found dependency %q with tag %#v, expected tag %#v",
dep.module, dep.tag, tag))
}
return dep.module.logicModule
}
}
return nil
}
func (m *baseModuleContext) VisitDirectDeps(visit func(Module)) { func (m *baseModuleContext) VisitDirectDeps(visit func(Module)) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@ -488,6 +519,9 @@ type TopDownMutatorContext interface {
OtherModuleErrorf(m Module, fmt string, args ...interface{}) OtherModuleErrorf(m Module, fmt string, args ...interface{})
OtherModuleDependencyTag(m Module) DependencyTag OtherModuleDependencyTag(m Module) DependencyTag
GetDirectDepWithTag(name string, tag DependencyTag) Module
GetDirectDep(name string) (Module, DependencyTag)
VisitDirectDeps(visit func(Module)) VisitDirectDeps(visit func(Module))
VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
VisitDepsDepthFirst(visit func(Module)) VisitDepsDepthFirst(visit func(Module))