github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonformat/differ/block.go (about)

     1  package differ
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/internal/command/jsonformat/collections"
     5  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
     6  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed/renderers"
     7  	"github.com/hashicorp/terraform/internal/command/jsonprovider"
     8  	"github.com/hashicorp/terraform/internal/plans"
     9  )
    10  
    11  func (change Change) ComputeDiffForBlock(block *jsonprovider.Block) computed.Diff {
    12  	if sensitive, ok := change.checkForSensitiveBlock(block); ok {
    13  		return sensitive
    14  	}
    15  
    16  	if unknown, ok := change.checkForUnknownBlock(block); ok {
    17  		return unknown
    18  	}
    19  
    20  	current := change.getDefaultActionForIteration()
    21  
    22  	blockValue := change.asMap()
    23  
    24  	attributes := make(map[string]computed.Diff)
    25  	for key, attr := range block.Attributes {
    26  		childValue := blockValue.getChild(key)
    27  
    28  		if !childValue.RelevantAttributes.MatchesPartial() {
    29  			// Mark non-relevant attributes as unchanged.
    30  			childValue = childValue.AsNoOp()
    31  		}
    32  
    33  		// Empty strings in blocks should be considered null for legacy reasons.
    34  		// The SDK doesn't support null strings yet, so we work around this now.
    35  		if before, ok := childValue.Before.(string); ok && len(before) == 0 {
    36  			childValue.Before = nil
    37  		}
    38  		if after, ok := childValue.After.(string); ok && len(after) == 0 {
    39  			childValue.After = nil
    40  		}
    41  
    42  		// Always treat changes to blocks as implicit.
    43  		childValue.BeforeExplicit = false
    44  		childValue.AfterExplicit = false
    45  
    46  		childChange := childValue.ComputeDiffForAttribute(attr)
    47  		if childChange.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
    48  			// Don't record nil values at all in blocks.
    49  			continue
    50  		}
    51  
    52  		attributes[key] = childChange
    53  		current = collections.CompareActions(current, childChange.Action)
    54  	}
    55  
    56  	blocks := renderers.Blocks{
    57  		ReplaceBlocks:         make(map[string]bool),
    58  		BeforeSensitiveBlocks: make(map[string]bool),
    59  		AfterSensitiveBlocks:  make(map[string]bool),
    60  		SingleBlocks:          make(map[string]computed.Diff),
    61  		ListBlocks:            make(map[string][]computed.Diff),
    62  		SetBlocks:             make(map[string][]computed.Diff),
    63  		MapBlocks:             make(map[string]map[string]computed.Diff),
    64  	}
    65  
    66  	for key, blockType := range block.BlockTypes {
    67  		childValue := blockValue.getChild(key)
    68  
    69  		if !childValue.RelevantAttributes.MatchesPartial() {
    70  			// Mark non-relevant attributes as unchanged.
    71  			childValue = childValue.AsNoOp()
    72  		}
    73  
    74  		beforeSensitive := childValue.isBeforeSensitive()
    75  		afterSensitive := childValue.isAfterSensitive()
    76  		forcesReplacement := childValue.ReplacePaths.Matches()
    77  
    78  		switch NestingMode(blockType.NestingMode) {
    79  		case nestingModeSet:
    80  			diffs, action := childValue.computeBlockDiffsAsSet(blockType.Block)
    81  			if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
    82  				// Don't record nil values in blocks.
    83  				continue
    84  			}
    85  			blocks.AddAllSetBlock(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
    86  			current = collections.CompareActions(current, action)
    87  		case nestingModeList:
    88  			diffs, action := childValue.computeBlockDiffsAsList(blockType.Block)
    89  			if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
    90  				// Don't record nil values in blocks.
    91  				continue
    92  			}
    93  			blocks.AddAllListBlock(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
    94  			current = collections.CompareActions(current, action)
    95  		case nestingModeMap:
    96  			diffs, action := childValue.computeBlockDiffsAsMap(blockType.Block)
    97  			if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
    98  				// Don't record nil values in blocks.
    99  				continue
   100  			}
   101  			blocks.AddAllMapBlocks(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
   102  			current = collections.CompareActions(current, action)
   103  		case nestingModeSingle, nestingModeGroup:
   104  			diff := childValue.ComputeDiffForBlock(blockType.Block)
   105  			if diff.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
   106  				// Don't record nil values in blocks.
   107  				continue
   108  			}
   109  			blocks.AddSingleBlock(key, diff, forcesReplacement, beforeSensitive, afterSensitive)
   110  			current = collections.CompareActions(current, diff.Action)
   111  		default:
   112  			panic("unrecognized nesting mode: " + blockType.NestingMode)
   113  		}
   114  	}
   115  
   116  	return computed.NewDiff(renderers.Block(attributes, blocks), current, change.ReplacePaths.Matches())
   117  }