github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/refactoring/move_statement.go (about)

     1  package refactoring
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/internal/addrs"
     7  	"github.com/hashicorp/terraform/internal/configs"
     8  	"github.com/hashicorp/terraform/internal/tfdiags"
     9  )
    10  
    11  type MoveStatement struct {
    12  	From, To  *addrs.MoveEndpointInModule
    13  	DeclRange tfdiags.SourceRange
    14  }
    15  
    16  // FindMoveStatements recurses through the modules of the given configuration
    17  // and returns a flat set of all "moved" blocks defined within, in a
    18  // deterministic but undefined order.
    19  func FindMoveStatements(rootCfg *configs.Config) []MoveStatement {
    20  	return findMoveStatements(rootCfg, nil)
    21  }
    22  
    23  func findMoveStatements(cfg *configs.Config, into []MoveStatement) []MoveStatement {
    24  	modAddr := cfg.Path
    25  	for _, mc := range cfg.Module.Moved {
    26  		fromAddr, toAddr := addrs.UnifyMoveEndpoints(modAddr, mc.From, mc.To)
    27  		if fromAddr == nil || toAddr == nil {
    28  			// Invalid combination should've been caught during original
    29  			// configuration decoding, in the configs package.
    30  			panic(fmt.Sprintf("incompatible move endpoints in %s", mc.DeclRange))
    31  		}
    32  
    33  		into = append(into, MoveStatement{
    34  			From:      fromAddr,
    35  			To:        toAddr,
    36  			DeclRange: tfdiags.SourceRangeFromHCL(mc.DeclRange),
    37  		})
    38  	}
    39  
    40  	for _, childCfg := range cfg.Children {
    41  		into = findMoveStatements(childCfg, into)
    42  	}
    43  
    44  	return into
    45  }
    46  
    47  func (s *MoveStatement) ObjectKind() addrs.MoveEndpointKind {
    48  	// addrs.UnifyMoveEndpoints guarantees that both of our addresses have
    49  	// the same kind, so we can just arbitrary use From and assume To will
    50  	// match it.
    51  	return s.From.ObjectKind()
    52  }
    53  
    54  // Name is used internally for displaying the statement graph
    55  func (s *MoveStatement) Name() string {
    56  	return fmt.Sprintf("%s->%s", s.From, s.To)
    57  }