mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
cd51864d84
We initially just mimicked our old practice of using []string for module paths here, but the addrs package now gives us a pair of types that better capture the two different kinds of module addresses we are dealing with: static addresses (nodes in the configuration tree) and dynamic/instance addresses (which can represent the situation where multiple instances are created from a single module call). This distinction still remains rather artificial since we don't yet have support for count or for_each on module calls, but this is intended to lay the foundations for that to be added later, and in the mean time just gives us some handy helper functions for parsing and formatting these address types.
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package addrs
|
|
|
|
import "bytes"
|
|
|
|
// ModuleInstance is an address for a particular module instance within the
|
|
// dynamic module tree. This is an extension of the static traversals
|
|
// represented by type Module that deals with the possibility of a single
|
|
// module call producing multiple instances via the "count" and "for_each"
|
|
// arguments.
|
|
//
|
|
// Although ModuleInstance is a slice, it should be treated as immutable after
|
|
// creation.
|
|
type ModuleInstance []ModuleInstanceStep
|
|
|
|
// ModuleInstanceStep is a single traversal step through the dynamic module
|
|
// tree. It is used only as part of ModuleInstance.
|
|
type ModuleInstanceStep struct {
|
|
Name string
|
|
InstanceKey InstanceKey
|
|
}
|
|
|
|
// RootModuleInstance is the module instance address representing the root
|
|
// module, which is also the zero value of ModuleInstance.
|
|
var RootModuleInstance ModuleInstance
|
|
|
|
// Child returns the address of a child module instance of the receiver,
|
|
// identified by the given name and key.
|
|
func (m ModuleInstance) Child(name string, key InstanceKey) ModuleInstance {
|
|
ret := make(ModuleInstance, 0, len(m)+1)
|
|
ret = append(ret, m...)
|
|
return append(ret, ModuleInstanceStep{
|
|
Name: name,
|
|
InstanceKey: key,
|
|
})
|
|
}
|
|
|
|
// Parent returns the address of the parent module instance of the receiver, or
|
|
// the receiver itself if there is no parent (if it's the root module address).
|
|
func (m ModuleInstance) Parent() ModuleInstance {
|
|
if len(m) == 0 {
|
|
return m
|
|
}
|
|
return m[:len(m)-1]
|
|
}
|
|
|
|
// String returns a string representation of the receiver, in the format used
|
|
// within e.g. user-provided resource addresses.
|
|
//
|
|
// The address of the root module has the empty string as its representation.
|
|
func (m ModuleInstance) String() string {
|
|
var buf bytes.Buffer
|
|
sep := ""
|
|
for _, step := range m {
|
|
buf.WriteString(sep)
|
|
buf.WriteString("module.")
|
|
buf.WriteString(step.Name)
|
|
if step.InstanceKey != NoKey {
|
|
buf.WriteString(step.InstanceKey.String())
|
|
}
|
|
sep = "."
|
|
}
|
|
return buf.String()
|
|
}
|