2023-01-09 05:15:38 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2023-01-11 02:35:36 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/collections"
|
2023-01-10 10:24:48 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed/renderers"
|
2023-01-09 05:15:38 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonprovider"
|
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
|
|
|
)
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeAttributeDiffAsObject(attributes map[string]cty.Type) computed.Diff {
|
|
|
|
attributeDiffs, action := processObject(change, attributes, func(value Change, ctype cty.Type) computed.Diff {
|
2023-02-07 02:14:14 -06:00
|
|
|
return value.ComputeDiffForType(ctype)
|
2023-01-09 05:15:38 -06:00
|
|
|
})
|
2023-01-16 08:18:38 -06:00
|
|
|
return computed.NewDiff(renderers.Object(attributeDiffs), action, change.ReplacePaths.Matches())
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeAttributeDiffAsNestedObject(attributes map[string]*jsonprovider.Attribute) computed.Diff {
|
|
|
|
attributeDiffs, action := processObject(change, attributes, func(value Change, attribute *jsonprovider.Attribute) computed.Diff {
|
|
|
|
return value.ComputeDiffForAttribute(attribute)
|
2023-01-09 05:15:38 -06:00
|
|
|
})
|
2023-01-16 08:18:38 -06:00
|
|
|
return computed.NewDiff(renderers.NestedObject(attributeDiffs), action, change.ReplacePaths.Matches())
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|
|
|
|
|
2023-01-09 09:49:35 -06:00
|
|
|
// processObject steps through the children of value as if it is an object and
|
2023-01-10 10:24:48 -06:00
|
|
|
// calls out to the provided computeDiff function once it has collated the
|
2023-01-09 09:49:35 -06:00
|
|
|
// diffs for each child attribute.
|
|
|
|
//
|
|
|
|
// We have to make this generic as attributes and nested objects process either
|
|
|
|
// cty.Type or jsonprovider.Attribute children respectively. And we want to
|
|
|
|
// reuse as much code as possible.
|
|
|
|
//
|
2023-01-10 10:24:48 -06:00
|
|
|
// Also, as it generic we cannot make this function a method on Change as you
|
2023-01-09 09:49:35 -06:00
|
|
|
// can't create generic methods on structs. Instead, we make this a generic
|
|
|
|
// function that receives the value as an argument.
|
2023-01-10 10:24:48 -06:00
|
|
|
func processObject[T any](v Change, attributes map[string]T, computeDiff func(Change, T) computed.Diff) (map[string]computed.Diff, plans.Action) {
|
|
|
|
attributeDiffs := make(map[string]computed.Diff)
|
2023-01-09 05:15:38 -06:00
|
|
|
mapValue := v.asMap()
|
|
|
|
|
|
|
|
currentAction := v.getDefaultActionForIteration()
|
2023-01-09 09:49:35 -06:00
|
|
|
for key, attribute := range attributes {
|
2023-01-09 05:15:38 -06:00
|
|
|
attributeValue := mapValue.getChild(key)
|
|
|
|
|
2023-01-16 08:18:38 -06:00
|
|
|
if !attributeValue.RelevantAttributes.MatchesPartial() {
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
attributeValue = attributeValue.AsNoOp()
|
|
|
|
}
|
|
|
|
|
2023-01-09 05:15:38 -06:00
|
|
|
// We always assume changes to object are implicit.
|
|
|
|
attributeValue.BeforeExplicit = false
|
|
|
|
attributeValue.AfterExplicit = false
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
attributeDiff := computeDiff(attributeValue, attribute)
|
|
|
|
if attributeDiff.Action == plans.NoOp && attributeValue.Before == nil && attributeValue.After == nil {
|
2023-01-09 05:15:38 -06:00
|
|
|
// We skip attributes of objects that are null both before and
|
|
|
|
// after. We don't even count these as unchanged attributes.
|
|
|
|
continue
|
|
|
|
}
|
2023-01-10 10:24:48 -06:00
|
|
|
attributeDiffs[key] = attributeDiff
|
2023-01-11 02:35:36 -06:00
|
|
|
currentAction = collections.CompareActions(currentAction, attributeDiff.Action)
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
return attributeDiffs, currentAction
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|