addrs: ModuleInstance and AbsResourceInstance are UniqueKeyers

Since these address types are not directly comparable themselves, we use
an unexported named type around the string representation, whereby the
special type can avoid any ambiguity between string representations of
different types and thus each type only needs to worry about possible
ambiguity of its _own_ string representation.
This commit is contained in:
Martin Atkins 2021-07-12 16:30:31 -07:00
parent f3a57db293
commit cd06572c4f
3 changed files with 24 additions and 0 deletions

View File

@ -275,6 +275,14 @@ func (m ModuleInstance) String() string {
return buf.String()
}
type moduleInstanceKey string
func (m ModuleInstance) UniqueKey() UniqueKey {
return moduleInstanceKey(m.String())
}
func (mk moduleInstanceKey) uniqueKeySigil() {}
// Equal returns true if the receiver and the given other value
// contains the exact same parts.
func (m ModuleInstance) Equal(o ModuleInstance) bool {

View File

@ -292,6 +292,14 @@ func (r AbsResourceInstance) Less(o AbsResourceInstance) bool {
}
}
type absResourceInstanceKey string
func (r AbsResourceInstance) UniqueKey() UniqueKey {
return absResourceInstanceKey(r.String())
}
func (r absResourceInstanceKey) uniqueKeySigil() {}
func (r AbsResourceInstance) absMoveableSigil() {
// AbsResourceInstance is moveable
}

View File

@ -46,6 +46,14 @@ func TestUniqueKeyer(t *testing.T) {
},
Key: IntKey(1),
},
RootModuleInstance,
RootModuleInstance.Child("foo", NoKey),
RootModuleInstance.ResourceInstance(
DataResourceMode,
"boop",
"beep",
NoKey,
),
Self,
}