Files
opentf/internal/command/jsonformat/differ/primitive.go
Liam Cervante 71daef058f Add rendering functionality for primitives to the structured renderer (#32373)
* 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

* goimports
2023-01-09 11:24:01 +01:00

38 lines
831 B
Go

package differ
import (
"fmt"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/command/jsonformat/change"
)
func strptr(str string) *string {
return &str
}
func (v Value) computeAttributeChangeAsPrimitive(ctyType cty.Type) change.Change {
return v.AsChange(change.Primitive(formatAsPrimitive(v.Before, ctyType), formatAsPrimitive(v.After, ctyType)))
}
func formatAsPrimitive(value interface{}, ctyType cty.Type) *string {
if value == nil {
return nil
}
switch {
case ctyType == cty.String:
return strptr(fmt.Sprintf("\"%s\"", value))
case ctyType == cty.Bool:
if value.(bool) {
return strptr("true")
}
return strptr("false")
case ctyType == cty.Number:
return strptr(fmt.Sprintf("%g", value))
default:
panic("unrecognized primitive type: " + ctyType.FriendlyName())
}
}