Make modules targetable

This commit is contained in:
Pam Selle 2020-03-04 17:38:39 -05:00 committed by James Bardin
parent 50077eabe9
commit 7407fee9c2
2 changed files with 64 additions and 3 deletions

View File

@ -44,6 +44,56 @@ func (m Module) Equal(other Module) bool {
return m.String() == other.String()
}
func (m Module) targetableSigil() {
// Module is targetable
}
// TargetContains implements Targetable for Module by returning true if the given other
// address either matches the receiver, is a sub-module-instance of the
// receiver, or is a targetable absolute address within a module that
// is contained within the receiver.
func (m Module) TargetContains(other Targetable) bool {
switch to := other.(type) {
case Module:
if len(to) < len(m) {
// Can't be contained if the path is shorter
return false
}
// Other is contained if its steps match for the length of our own path.
for i, ourStep := range m {
otherStep := to[i]
if ourStep != otherStep {
return false
}
}
// If we fall out here then the prefixed matched, so it's contained.
return true
case ModuleInstance:
if len(to) < len(m) {
return false
}
for i, ourStep := range m {
otherStep := to[i]
// This is where ModuleInstance differs from Module
if ourStep != otherStep.Name {
return false
}
}
return true
case AbsResource:
return m.TargetContains(to.Module)
case AbsResourceInstance:
return m.TargetContains(to.Module)
default:
return false
}
}
// Child returns the address of a child call in the receiver, identified by the
// given name.
func (m Module) Child(name string) Module {

View File

@ -383,8 +383,7 @@ func (m ModuleInstance) CallInstance() (ModuleInstance, ModuleCallInstance) {
// is contained within the reciever.
func (m ModuleInstance) TargetContains(other Targetable) bool {
switch to := other.(type) {
case ModuleInstance:
case Module:
if len(to) < len(m) {
// Can't be contained if the path is shorter
return false
@ -392,13 +391,25 @@ func (m ModuleInstance) TargetContains(other Targetable) bool {
// Other is contained if its steps match for the length of our own path.
for i, ourStep := range m {
otherStep := to[i]
if ourStep != otherStep {
if ourStep.Name != otherStep {
return false
}
}
// If we fall out here then the prefixed matched, so it's contained.
return true
case ModuleInstance:
if len(to) < len(m) {
return false
}
for i, ourStep := range m {
otherStep := to[i]
if ourStep != otherStep {
return false
}
}
return true
case AbsResource:
return m.TargetContains(to.Module)