github.com/opentofu/opentofu@v1.7.1/internal/command/jsonformat/differ/map.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package differ
     7  
     8  import (
     9  	"github.com/zclconf/go-cty/cty"
    10  
    11  	"github.com/opentofu/opentofu/internal/command/jsonformat/collections"
    12  	"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
    13  	"github.com/opentofu/opentofu/internal/command/jsonformat/computed/renderers"
    14  	"github.com/opentofu/opentofu/internal/command/jsonformat/structured"
    15  	"github.com/opentofu/opentofu/internal/command/jsonprovider"
    16  	"github.com/opentofu/opentofu/internal/plans"
    17  )
    18  
    19  func computeAttributeDiffAsMap(change structured.Change, elementType cty.Type) computed.Diff {
    20  	mapValue := change.AsMap()
    21  	elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.AllKeys(), func(key string) computed.Diff {
    22  		value := mapValue.GetChild(key)
    23  		if !value.RelevantAttributes.MatchesPartial() {
    24  			// Mark non-relevant attributes as unchanged.
    25  			value = value.AsNoOp()
    26  		}
    27  		return ComputeDiffForType(value, elementType)
    28  	})
    29  	return computed.NewDiff(renderers.Map(elements), current, change.ReplacePaths.Matches())
    30  }
    31  
    32  func computeAttributeDiffAsNestedMap(change structured.Change, attributes map[string]*jsonprovider.Attribute) computed.Diff {
    33  	mapValue := change.AsMap()
    34  	elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff {
    35  		value := mapValue.GetChild(key)
    36  		if !value.RelevantAttributes.MatchesPartial() {
    37  			// Mark non-relevant attributes as unchanged.
    38  			value = value.AsNoOp()
    39  		}
    40  		return computeDiffForNestedAttribute(value, &jsonprovider.NestedType{
    41  			Attributes:  attributes,
    42  			NestingMode: "single",
    43  		})
    44  	})
    45  	return computed.NewDiff(renderers.NestedMap(elements), current, change.ReplacePaths.Matches())
    46  }
    47  
    48  func computeBlockDiffsAsMap(change structured.Change, block *jsonprovider.Block) (map[string]computed.Diff, plans.Action) {
    49  	mapValue := change.AsMap()
    50  	return collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff {
    51  		value := mapValue.GetChild(key)
    52  		if !value.RelevantAttributes.MatchesPartial() {
    53  			// Mark non-relevant attributes as unchanged.
    54  			value = value.AsNoOp()
    55  		}
    56  		return ComputeDiffForBlock(value, block)
    57  	})
    58  }