From 8e02f5dd21238640efa1a3166c2ff9cce14f2467 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 2 Feb 2024 15:41:22 -0800 Subject: [PATCH] addrs: PartialExpandedResource module address accessors This is kinda awkward because this address type represents both resources whose own instances are not expanded yet and resources belonging to whole modules whose instance keys haven't been expanded yet, and those two cases have different address types. However, in return for this awkward API only for the rare case where we need to isolate the module instance address, the rest of the system gets to not worry very much about this distinction: it can share most code between the two cases, since they both need similar evaluation treatment anyway. --- internal/addrs/partial_expanded.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/internal/addrs/partial_expanded.go b/internal/addrs/partial_expanded.go index 6bffedac55..6b2cac149b 100644 --- a/internal/addrs/partial_expanded.go +++ b/internal/addrs/partial_expanded.go @@ -323,6 +323,34 @@ func (per PartialExpandedResource) KnownModuleInstancePrefix() ModuleInstance { return per.module.KnownPrefix() } +// ModuleInstance returns the fully-qualified [ModuleInstance] that this +// partial-expanded resource belongs to, but only if its module instance +// address is fully known. +// +// The second return value is false if the module instance address is not +// fully expanded, in which case the first return value is invalid. Use +// [PartialExpandedResource.PartialExpandedModule] instead in that case. +func (per PartialExpandedResource) ModuleInstance() (ModuleInstance, bool) { + if len(per.module.unexpandedSuffix) != 0 { + return nil, false + } + return per.module.expandedPrefix, true +} + +// PartialExpandedModule returns a [PartialExpandedModule] address describing +// the partially-unknown module instance address that the resource belongs to, +// but only if the module instance address is not fully known. +// +// The second return value is false if the module instance address is actually +// fully expanded, in which case the first return value is invalid. Use +// [PartialExpandedResource.ModuleInstance] instead in that case. +func (per PartialExpandedResource) PartialExpandedModule() (PartialExpandedModule, bool) { + if len(per.module.unexpandedSuffix) == 0 { + return PartialExpandedModule{}, false + } + return per.module, true +} + // String returns a string representation of the pattern which uses the special // placeholder "[*]" to represent positions where instance keys are not yet // known.