2023-01-09 04:05:25 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
2023-01-09 04:24:01 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
2023-01-09 04:05:25 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/change"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonprovider"
|
|
|
|
)
|
|
|
|
|
2023-01-09 09:49:35 -06:00
|
|
|
func (v Value) ComputeChangeForAttribute(attribute *jsonprovider.Attribute) change.Change {
|
2023-01-09 05:15:38 -06:00
|
|
|
if attribute.AttributeNestedType != nil {
|
|
|
|
return v.computeChangeForNestedAttribute(attribute.AttributeNestedType)
|
2023-01-09 04:40:47 -06:00
|
|
|
}
|
2023-01-09 05:15:38 -06:00
|
|
|
return v.computeChangeForType(unmarshalAttribute(attribute))
|
|
|
|
}
|
2023-01-09 04:40:47 -06:00
|
|
|
|
2023-01-09 09:49:35 -06:00
|
|
|
func (v Value) computeChangeForNestedAttribute(nested *jsonprovider.NestedType) change.Change {
|
2023-01-09 13:38:25 -06:00
|
|
|
if sensitive, ok := v.checkForSensitiveNestedAttribute(nested); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return sensitive
|
|
|
|
}
|
|
|
|
|
|
|
|
if computed, ok := v.checkForComputedNestedAttribute(nested); ok {
|
|
|
|
return computed
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:15:17 -06:00
|
|
|
switch NestingMode(nested.NestingMode) {
|
|
|
|
case nestingModeSingle, nestingModeGroup:
|
2023-01-09 09:49:35 -06:00
|
|
|
return v.computeAttributeChangeAsNestedObject(nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeMap:
|
2023-01-09 09:49:35 -06:00
|
|
|
return v.computeAttributeChangeAsNestedMap(nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeList:
|
2023-01-09 09:49:35 -06:00
|
|
|
return v.computeAttributeChangeAsNestedList(nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeSet:
|
2023-01-09 09:49:35 -06:00
|
|
|
return v.computeAttributeChangeAsNestedSet(nested.Attributes)
|
2023-01-09 05:15:38 -06:00
|
|
|
default:
|
2023-01-09 09:49:35 -06:00
|
|
|
panic("unrecognized nesting mode: " + nested.NestingMode)
|
2023-01-09 04:55:55 -06:00
|
|
|
}
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|
2023-01-09 04:55:55 -06:00
|
|
|
|
2023-01-09 09:49:35 -06:00
|
|
|
func (v Value) computeChangeForType(ctype cty.Type) change.Change {
|
2023-01-09 13:38:25 -06:00
|
|
|
if sensitive, ok := v.checkForSensitiveType(ctype); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return sensitive
|
|
|
|
}
|
|
|
|
|
|
|
|
if computed, ok := v.checkForComputedType(ctype); ok {
|
|
|
|
return computed
|
2023-01-09 07:45:35 -06:00
|
|
|
}
|
|
|
|
|
2023-01-09 04:24:01 -06:00
|
|
|
switch {
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype == cty.NilType, ctype == cty.DynamicPseudoType:
|
2023-01-09 13:08:08 -06:00
|
|
|
// Forward nil or dynamic types over to be processed as outputs.
|
|
|
|
// There is nothing particularly special about the way outputs are
|
|
|
|
// processed that make this unsafe, we could just as easily call this
|
|
|
|
// function computeChangeForDynamicValues(), but external callers will
|
|
|
|
// only be in this situation when processing outputs so this function
|
|
|
|
// is named for their benefit.
|
2023-01-09 09:49:35 -06:00
|
|
|
return v.ComputeChangeForOutput()
|
|
|
|
case ctype.IsPrimitiveType():
|
|
|
|
return v.computeAttributeChangeAsPrimitive(ctype)
|
|
|
|
case ctype.IsObjectType():
|
|
|
|
return v.computeAttributeChangeAsObject(ctype.AttributeTypes())
|
|
|
|
case ctype.IsMapType():
|
|
|
|
return v.computeAttributeChangeAsMap(ctype.ElementType())
|
|
|
|
case ctype.IsListType():
|
|
|
|
return v.computeAttributeChangeAsList(ctype.ElementType())
|
2023-01-09 10:39:13 -06:00
|
|
|
case ctype.IsTupleType():
|
|
|
|
return v.computeAttributeChangeAsTuple(ctype.TupleElementTypes())
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsSetType():
|
|
|
|
return v.computeAttributeChangeAsSet(ctype.ElementType())
|
2023-01-09 04:24:01 -06:00
|
|
|
default:
|
2023-01-09 09:49:35 -06:00
|
|
|
panic("unrecognized type: " + ctype.FriendlyName())
|
2023-01-09 04:24:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-01-09 04:05:25 -06:00
|
|
|
}
|