Files
opentf/internal/command/jsonformat/differ/attribute.go
Liam Cervante 1eebcf875f Add support for object attributes in the structured renderer (#32391)
* 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
2023-01-09 12:15:38 +01:00

45 lines
1.3 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)
default:
panic("unrecognized nesting mode: " + attribute.NestingMode)
}
}
func (v Value) computeChangeForType(ctyType cty.Type) change.Change {
switch {
case ctyType.IsPrimitiveType():
return v.computeAttributeChangeAsPrimitive(ctyType)
case ctyType.IsObjectType():
return v.computeAttributeChangeAsObject(ctyType.AttributeTypes())
default:
panic("not implemented")
}
}
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
}