2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-09 05:41:24 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
2023-01-09 07:06:38 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/collections"
|
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
|
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/computed/renderers"
|
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/structured"
|
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonprovider"
|
|
|
|
"github.com/opentofu/opentofu/internal/plans"
|
2023-01-09 05:41:24 -06:00
|
|
|
)
|
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
func computeAttributeDiffAsMap(change structured.Change, elementType cty.Type) computed.Diff {
|
|
|
|
mapValue := change.AsMap()
|
2023-04-24 03:28:21 -05:00
|
|
|
elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.AllKeys(), func(key string) computed.Diff {
|
2023-04-21 02:51:55 -05:00
|
|
|
value := mapValue.GetChild(key)
|
2023-01-16 08:18:38 -06:00
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
value = value.AsNoOp()
|
|
|
|
}
|
2023-04-21 02:51:55 -05:00
|
|
|
return ComputeDiffForType(value, elementType)
|
2023-01-09 05:41:24 -06:00
|
|
|
})
|
2023-01-16 08:18:38 -06:00
|
|
|
return computed.NewDiff(renderers.Map(elements), current, change.ReplacePaths.Matches())
|
2023-01-09 05:41:24 -06:00
|
|
|
}
|
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
func computeAttributeDiffAsNestedMap(change structured.Change, attributes map[string]*jsonprovider.Attribute) computed.Diff {
|
|
|
|
mapValue := change.AsMap()
|
2023-04-24 03:28:21 -05:00
|
|
|
elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff {
|
2023-04-21 02:51:55 -05:00
|
|
|
value := mapValue.GetChild(key)
|
2023-01-16 08:18:38 -06:00
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
value = value.AsNoOp()
|
|
|
|
}
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeDiffForNestedAttribute(value, &jsonprovider.NestedType{
|
2023-01-09 09:49:35 -06:00
|
|
|
Attributes: attributes,
|
|
|
|
NestingMode: "single",
|
|
|
|
})
|
2023-01-09 05:41:24 -06:00
|
|
|
})
|
2023-01-16 08:18:38 -06:00
|
|
|
return computed.NewDiff(renderers.NestedMap(elements), current, change.ReplacePaths.Matches())
|
2023-01-09 05:41:24 -06:00
|
|
|
}
|
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
func computeBlockDiffsAsMap(change structured.Change, block *jsonprovider.Block) (map[string]computed.Diff, plans.Action) {
|
|
|
|
mapValue := change.AsMap()
|
2023-04-24 03:28:21 -05:00
|
|
|
return collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff {
|
2023-04-21 02:51:55 -05:00
|
|
|
value := mapValue.GetChild(key)
|
2023-01-16 08:18:38 -06:00
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
value = value.AsNoOp()
|
|
|
|
}
|
2023-04-21 02:51:55 -05:00
|
|
|
return ComputeDiffForBlock(value, block)
|
2023-01-11 02:35:36 -06:00
|
|
|
})
|
2023-01-09 05:41:24 -06:00
|
|
|
}
|