github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/addrs/module.go (about)

     1  package addrs
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // Module is an address for a module call within configuration. This is
     8  // the static counterpart of ModuleInstance, representing a traversal through
     9  // the static module call tree in configuration and does not take into account
    10  // the potentially-multiple instances of a module that might be created by
    11  // "count" and "for_each" arguments within those calls.
    12  //
    13  // This type should be used only in very specialized cases when working with
    14  // the static module call tree. Type ModuleInstance is appropriate in more cases.
    15  //
    16  // Although Module is a slice, it should be treated as immutable after creation.
    17  type Module []string
    18  
    19  // IsRoot returns true if the receiver is the address of the root module,
    20  // or false otherwise.
    21  func (m Module) IsRoot() bool {
    22  	return len(m) == 0
    23  }
    24  
    25  func (m Module) String() string {
    26  	if len(m) == 0 {
    27  		return ""
    28  	}
    29  	return strings.Join([]string(m), ".")
    30  }
    31  
    32  // Call returns the module call address that corresponds to the given module
    33  // instance, along with the address of the module that contains it.
    34  //
    35  // There is no call for the root module, so this method will panic if called
    36  // on the root module address.
    37  //
    38  // In practice, this just turns the last element of the receiver into a
    39  // ModuleCall and then returns a slice of the receiever that excludes that
    40  // last part. This is just a convenience for situations where a call address
    41  // is required, such as when dealing with *Reference and Referencable values.
    42  func (m Module) Call() (Module, ModuleCall) {
    43  	if len(m) == 0 {
    44  		panic("cannot produce ModuleCall for root module")
    45  	}
    46  
    47  	caller, callName := m[:len(m)-1], m[len(m)-1]
    48  	return caller, ModuleCall{
    49  		Name: callName,
    50  	}
    51  }