Files
opentf/internal/command/jsonformat/differ/map.go
Liam Cervante 8330b7295b Structured plan renderer: Add support for map blocks and sensitive blocks. (#32491)
* change -> diff, value -> change

* also update readme#

* pause

* Update internal/command/jsonformat/computed/diff.go

Co-authored-by: Alisdair McDiarmid <alisdair@users.noreply.github.com>

* add interface assertions for diff renderers

* Add support for different kinds of blocks, and for sensitive blocks

Co-authored-by: Alisdair McDiarmid <alisdair@users.noreply.github.com>
2023-01-11 09:04:26 +01:00

65 lines
2.1 KiB
Go

package differ
import (
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
"github.com/hashicorp/terraform/internal/command/jsonformat/computed/renderers"
"github.com/hashicorp/terraform/internal/command/jsonprovider"
"github.com/hashicorp/terraform/internal/plans"
)
func (change Change) computeAttributeDiffAsMap(elementType cty.Type) computed.Diff {
current := change.getDefaultActionForIteration()
elements := make(map[string]computed.Diff)
change.processMap(func(key string, value Change) {
element := value.computeDiffForType(elementType)
elements[key] = element
current = compareActions(current, element.Action)
})
return computed.NewDiff(renderers.Map(elements), current, change.replacePath())
}
func (change Change) computeAttributeDiffAsNestedMap(attributes map[string]*jsonprovider.Attribute) computed.Diff {
current := change.getDefaultActionForIteration()
elements := make(map[string]computed.Diff)
change.processMap(func(key string, value Change) {
element := value.computeDiffForNestedAttribute(&jsonprovider.NestedType{
Attributes: attributes,
NestingMode: "single",
})
elements[key] = element
current = compareActions(current, element.Action)
})
return computed.NewDiff(renderers.Map(elements), current, change.replacePath())
}
func (change Change) computeBlockDiffsAsMap(block *jsonprovider.Block) (map[string]computed.Diff, plans.Action) {
current := change.getDefaultActionForIteration()
elements := make(map[string]computed.Diff)
change.processMap(func(key string, value Change) {
element := value.ComputeDiffForBlock(block)
elements[key] = element
current = compareActions(current, element.Action)
})
return elements, current
}
func (change Change) processMap(process func(key string, value Change)) {
mapValue := change.asMap()
handled := make(map[string]bool)
for key := range mapValue.Before {
handled[key] = true
process(key, mapValue.getChild(key))
}
for key := range mapValue.After {
if _, ok := handled[key]; ok {
continue
}
process(key, mapValue.getChild(key))
}
}