Files
opentf/internal/command/jsonformat/differ/attribute.go
Liam Cervante 9bc5ded27a Add support for outputs in the structured renderer (#32426)
* prep for processing the structured run output

* undo unwanted change to a json key

* Add skeleton functions and API for refactored renderer

* goimports

* Fix documentation of the RenderOpts struct

* Add rendering functionality for primitives to the structured renderer

* add test case for override

* Add support for parsing and rendering sensitive values in the renderer

* Add support for unknown/computed values in the structured renderer

* delete missing unit tests

* Add support for object attributes in the structured renderer

* goimports

* Add support for the replace paths data in the structured renderer

* Add support for maps in the structured renderer

* Add support for lists in the structured renderer

* goimports

* Add support for sets in the structured renderer

* goimports

* Add support for blocks in the structured renderer

* goimports

* Add support for outputs in the structured renderer

* fix ordering of blocks

* remove unused test stub

* fix typo
2023-01-09 14:45:35 +01:00

61 lines
1.9 KiB
Go

package differ
import (
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/hashicorp/terraform/internal/command/jsonformat/change"
"github.com/hashicorp/terraform/internal/command/jsonprovider"
)
func (v Value) computeChangeForAttribute(attribute *jsonprovider.Attribute) change.Change {
if attribute.AttributeNestedType != nil {
return v.computeChangeForNestedAttribute(attribute.AttributeNestedType)
}
return v.computeChangeForType(unmarshalAttribute(attribute))
}
func (v Value) computeChangeForNestedAttribute(attribute *jsonprovider.NestedType) change.Change {
switch attribute.NestingMode {
case "single", "group":
return v.computeAttributeChangeAsNestedObject(attribute.Attributes)
case "map":
return v.computeAttributeChangeAsNestedMap(attribute.Attributes)
case "list":
return v.computeAttributeChangeAsNestedList(attribute.Attributes)
case "set":
return v.computeAttributeChangeAsNestedSet(attribute.Attributes)
default:
panic("unrecognized nesting mode: " + attribute.NestingMode)
}
}
func (v Value) computeChangeForType(ctyType cty.Type) change.Change {
if ctyType == cty.NilType {
return v.ComputeChangeForOutput()
}
switch {
case ctyType.IsPrimitiveType():
return v.computeAttributeChangeAsPrimitive(ctyType)
case ctyType.IsObjectType():
return v.computeAttributeChangeAsObject(ctyType.AttributeTypes())
case ctyType.IsMapType():
return v.computeAttributeChangeAsMap(ctyType.ElementType())
case ctyType.IsListType():
return v.computeAttributeChangeAsList(ctyType.ElementType())
case ctyType.IsSetType():
return v.computeAttributeChangeAsSet(ctyType.ElementType())
default:
panic("unrecognized type: " + ctyType.FriendlyName())
}
}
func unmarshalAttribute(attribute *jsonprovider.Attribute) cty.Type {
ctyType, err := ctyjson.UnmarshalType(attribute.AttributeType)
if err != nil {
panic("could not unmarshal attribute type: " + err.Error())
}
return ctyType
}