From f3c48314ab7ddc501176dd7a9b196d49505cedca Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 18 Jan 2024 16:29:29 -0800 Subject: [PATCH] addrs: AbsModuleCall.StaticModule This new method calculates the static Module address corresponding to the receiver, effectively discarding any instance keys present in the containing module instance path. --- internal/addrs/module_call.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/addrs/module_call.go b/internal/addrs/module_call.go index 65474d8132..162b05a6f6 100644 --- a/internal/addrs/module_call.go +++ b/internal/addrs/module_call.go @@ -59,6 +59,23 @@ func (c AbsModuleCall) absMoveableSigil() { // AbsModuleCall is "moveable". } +// StaticModule returns the static module path for the receiver. +// +// In other words, it effectively discards all of the dynamic instance keys +// along the path to this call, while retaining the static module names. +// +// Given a representation of module.a["foo"].module.b, this would return +// the [Module]-based representation of module.a.module.b, discarding the +// first step's dynamic instance key "foo". +func (c AbsModuleCall) StaticModule() Module { + ret := make(Module, len(c.Module), len(c.Module)+1) + for i, step := range c.Module { + ret[i] = step.Name + } + ret = append(ret, c.Call.Name) + return ret +} + func (c AbsModuleCall) String() string { if len(c.Module) == 0 { return "module." + c.Call.Name