github.com/opentofu/opentofu@v1.7.1/internal/command/jsonformat/differ/tuple.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  )
    16  
    17  func computeAttributeDiffAsTuple(change structured.Change, elementTypes []cty.Type) computed.Diff {
    18  	var elements []computed.Diff
    19  	current := change.GetDefaultActionForIteration()
    20  	sliceValue := change.AsSlice()
    21  	for ix, elementType := range elementTypes {
    22  		childValue := sliceValue.GetChild(ix, ix)
    23  		if !childValue.RelevantAttributes.MatchesPartial() {
    24  			// Mark non-relevant attributes as unchanged.
    25  			childValue = childValue.AsNoOp()
    26  		}
    27  		element := ComputeDiffForType(childValue, elementType)
    28  		elements = append(elements, element)
    29  		current = collections.CompareActions(current, element.Action)
    30  	}
    31  	return computed.NewDiff(renderers.List(elements), current, change.ReplacePaths.Matches())
    32  }