From cd06572c4fda7b0771215193358d677799e26263 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 12 Jul 2021 16:30:31 -0700 Subject: [PATCH] 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. --- internal/addrs/module_instance.go | 8 ++++++++ internal/addrs/resource.go | 8 ++++++++ internal/addrs/unique_key_test.go | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/internal/addrs/module_instance.go b/internal/addrs/module_instance.go index 2eb57948d..21afcb077 100644 --- a/internal/addrs/module_instance.go +++ b/internal/addrs/module_instance.go @@ -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 { diff --git a/internal/addrs/resource.go b/internal/addrs/resource.go index f22db05cb..a7b7ee308 100644 --- a/internal/addrs/resource.go +++ b/internal/addrs/resource.go @@ -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 } diff --git a/internal/addrs/unique_key_test.go b/internal/addrs/unique_key_test.go index 416899ca4..0926a0c37 100644 --- a/internal/addrs/unique_key_test.go +++ b/internal/addrs/unique_key_test.go @@ -46,6 +46,14 @@ func TestUniqueKeyer(t *testing.T) { }, Key: IntKey(1), }, + RootModuleInstance, + RootModuleInstance.Child("foo", NoKey), + RootModuleInstance.ResourceInstance( + DataResourceMode, + "boop", + "beep", + NoKey, + ), Self, }