2023-01-09 07:06:38 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
2023-01-11 02:35:36 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2023-01-09 07:06:38 -06:00
|
|
|
|
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 07:06:38 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonprovider"
|
2023-01-09 07:33:01 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2023-01-09 07:06:38 -06:00
|
|
|
)
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeAttributeDiffAsList(elementType cty.Type) computed.Diff {
|
2023-01-11 02:35:36 -06:00
|
|
|
sliceValue := change.asSlice()
|
|
|
|
|
|
|
|
processIndices := func(beforeIx, afterIx int) computed.Diff {
|
2023-01-11 03:20:24 -06:00
|
|
|
return sliceValue.getChild(beforeIx, afterIx).computeDiffForType(elementType)
|
2023-01-11 02:35:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
isObjType := func(_ interface{}) bool {
|
|
|
|
return elementType.IsObjectType()
|
|
|
|
}
|
|
|
|
|
|
|
|
elements, current := collections.TransformSlice(sliceValue.Before, sliceValue.After, processIndices, isObjType)
|
2023-01-11 03:20:24 -06:00
|
|
|
return computed.NewDiff(renderers.List(elements), current, change.ReplacePaths.ForcesReplacement())
|
2023-01-09 07:06:38 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeAttributeDiffAsNestedList(attributes map[string]*jsonprovider.Attribute) computed.Diff {
|
|
|
|
var elements []computed.Diff
|
|
|
|
current := change.getDefaultActionForIteration()
|
|
|
|
change.processNestedList(func(value Change) {
|
|
|
|
element := value.computeDiffForNestedAttribute(&jsonprovider.NestedType{
|
2023-01-09 09:49:35 -06:00
|
|
|
Attributes: attributes,
|
|
|
|
NestingMode: "single",
|
|
|
|
})
|
2023-01-09 07:06:38 -06:00
|
|
|
elements = append(elements, element)
|
2023-01-11 02:35:36 -06:00
|
|
|
current = collections.CompareActions(current, element.Action)
|
2023-01-09 07:06:38 -06:00
|
|
|
})
|
2023-01-11 03:20:24 -06:00
|
|
|
return computed.NewDiff(renderers.NestedList(elements), current, change.ReplacePaths.ForcesReplacement())
|
2023-01-09 07:06:38 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeBlockDiffsAsList(block *jsonprovider.Block) ([]computed.Diff, plans.Action) {
|
|
|
|
var elements []computed.Diff
|
|
|
|
current := change.getDefaultActionForIteration()
|
|
|
|
change.processNestedList(func(value Change) {
|
|
|
|
element := value.ComputeDiffForBlock(block)
|
2023-01-09 07:33:01 -06:00
|
|
|
elements = append(elements, element)
|
2023-01-11 02:35:36 -06:00
|
|
|
current = collections.CompareActions(current, element.Action)
|
2023-01-09 07:33:01 -06:00
|
|
|
})
|
|
|
|
return elements, current
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) processNestedList(process func(value Change)) {
|
|
|
|
sliceValue := change.asSlice()
|
2023-01-09 07:06:38 -06:00
|
|
|
for ix := 0; ix < len(sliceValue.Before) || ix < len(sliceValue.After); ix++ {
|
2023-01-11 03:20:24 -06:00
|
|
|
process(sliceValue.getChild(ix, ix))
|
2023-01-09 07:06:38 -06:00
|
|
|
}
|
|
|
|
}
|