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 05:15:38 -06:00
|
|
|
func (v Value) computeChangeForAttribute(attribute *jsonprovider.Attribute) change.Change {
|
|
|
|
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 05:15:38 -06:00
|
|
|
func (v Value) computeChangeForNestedAttribute(attribute *jsonprovider.NestedType) change.Change {
|
|
|
|
switch attribute.NestingMode {
|
|
|
|
case "single", "group":
|
|
|
|
return v.computeAttributeChangeAsNestedObject(attribute.Attributes)
|
2023-01-09 05:41:24 -06:00
|
|
|
case "map":
|
|
|
|
return v.computeAttributeChangeAsNestedMap(attribute.Attributes)
|
2023-01-09 05:15:38 -06:00
|
|
|
default:
|
|
|
|
panic("unrecognized nesting mode: " + attribute.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 05:15:38 -06:00
|
|
|
func (v Value) computeChangeForType(ctyType cty.Type) change.Change {
|
2023-01-09 04:24:01 -06:00
|
|
|
switch {
|
|
|
|
case ctyType.IsPrimitiveType():
|
|
|
|
return v.computeAttributeChangeAsPrimitive(ctyType)
|
2023-01-09 05:15:38 -06:00
|
|
|
case ctyType.IsObjectType():
|
|
|
|
return v.computeAttributeChangeAsObject(ctyType.AttributeTypes())
|
2023-01-09 05:41:24 -06:00
|
|
|
case ctyType.IsMapType():
|
|
|
|
return v.computeAttributeChangeAsMap(ctyType.ElementType())
|
2023-01-09 04:24:01 -06:00
|
|
|
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
|
2023-01-09 04:05:25 -06:00
|
|
|
}
|