diff --git a/module_ctx.go b/module_ctx.go index a074e37..0235beb 100644 --- a/module_ctx.go +++ b/module_ctx.go @@ -294,6 +294,14 @@ type BaseModuleContext interface { // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called. OtherModuleExists(name string) bool + // ModuleFromName returns (module, true) if a module exists by the given name and same context namespace, + // or (nil, false) if it does not exist. It panics if there is either more than one + // module of the given name, or if the given name refers to an alias instead of a module. + // There are no guarantees about which variant of the module will be returned. + // Prefer retrieving the module using GetDirectDep or a visit function, when possible, as + // this will guarantee the appropriate module-variant dependency is returned. + ModuleFromName(name string) (Module, bool) + // OtherModuleDependencyVariantExists returns true if a module with the // specified name and variant exists. The variant must match the given // variations. It must also match all the non-local variations of the current @@ -532,6 +540,23 @@ func (m *baseModuleContext) OtherModuleDependencyTag(logicModule Module) Depende return nil } +func (m *baseModuleContext) ModuleFromName(name string) (Module, bool) { + moduleGroup, exists := m.context.nameInterface.ModuleFromName(name, m.module.namespace()) + if exists { + if len(moduleGroup.modules) != 1 { + panic(fmt.Errorf("Expected exactly one module named %q, but got %d", name, len(moduleGroup.modules))) + } + moduleInfo := moduleGroup.modules[0].module() + if moduleInfo != nil { + return moduleInfo.logicModule, true + } else { + panic(fmt.Errorf(`Expected actual module named %q, but group did not contain a module. + There may instead be an alias by that name.`, name)) + } + } + return nil, exists +} + func (m *baseModuleContext) OtherModuleExists(name string) bool { _, exists := m.context.nameInterface.ModuleFromName(name, m.module.namespace()) return exists