github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonformat/differ/attribute.go (about)

     1  package differ
     2  
     3  import (
     4  	"github.com/zclconf/go-cty/cty"
     5  	ctyjson "github.com/zclconf/go-cty/cty/json"
     6  
     7  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
     8  
     9  	"github.com/hashicorp/terraform/internal/command/jsonprovider"
    10  )
    11  
    12  func (change Change) ComputeDiffForAttribute(attribute *jsonprovider.Attribute) computed.Diff {
    13  	if attribute.AttributeNestedType != nil {
    14  		return change.computeDiffForNestedAttribute(attribute.AttributeNestedType)
    15  	}
    16  	return change.ComputeDiffForType(unmarshalAttribute(attribute))
    17  }
    18  
    19  func (change Change) computeDiffForNestedAttribute(nested *jsonprovider.NestedType) computed.Diff {
    20  	if sensitive, ok := change.checkForSensitiveNestedAttribute(nested); ok {
    21  		return sensitive
    22  	}
    23  
    24  	if computed, ok := change.checkForUnknownNestedAttribute(nested); ok {
    25  		return computed
    26  	}
    27  
    28  	switch NestingMode(nested.NestingMode) {
    29  	case nestingModeSingle, nestingModeGroup:
    30  		return change.computeAttributeDiffAsNestedObject(nested.Attributes)
    31  	case nestingModeMap:
    32  		return change.computeAttributeDiffAsNestedMap(nested.Attributes)
    33  	case nestingModeList:
    34  		return change.computeAttributeDiffAsNestedList(nested.Attributes)
    35  	case nestingModeSet:
    36  		return change.computeAttributeDiffAsNestedSet(nested.Attributes)
    37  	default:
    38  		panic("unrecognized nesting mode: " + nested.NestingMode)
    39  	}
    40  }
    41  
    42  func (change Change) ComputeDiffForType(ctype cty.Type) computed.Diff {
    43  	if sensitive, ok := change.checkForSensitiveType(ctype); ok {
    44  		return sensitive
    45  	}
    46  
    47  	if computed, ok := change.checkForUnknownType(ctype); ok {
    48  		return computed
    49  	}
    50  
    51  	switch {
    52  	case ctype == cty.NilType, ctype == cty.DynamicPseudoType:
    53  		// Forward nil or dynamic types over to be processed as outputs.
    54  		// There is nothing particularly special about the way outputs are
    55  		// processed that make this unsafe, we could just as easily call this
    56  		// function computeChangeForDynamicValues(), but external callers will
    57  		// only be in this situation when processing outputs so this function
    58  		// is named for their benefit.
    59  		return change.ComputeDiffForOutput()
    60  	case ctype.IsPrimitiveType():
    61  		return change.computeAttributeDiffAsPrimitive(ctype)
    62  	case ctype.IsObjectType():
    63  		return change.computeAttributeDiffAsObject(ctype.AttributeTypes())
    64  	case ctype.IsMapType():
    65  		return change.computeAttributeDiffAsMap(ctype.ElementType())
    66  	case ctype.IsListType():
    67  		return change.computeAttributeDiffAsList(ctype.ElementType())
    68  	case ctype.IsTupleType():
    69  		return change.computeAttributeDiffAsTuple(ctype.TupleElementTypes())
    70  	case ctype.IsSetType():
    71  		return change.computeAttributeDiffAsSet(ctype.ElementType())
    72  	default:
    73  		panic("unrecognized type: " + ctype.FriendlyName())
    74  	}
    75  }
    76  
    77  func unmarshalAttribute(attribute *jsonprovider.Attribute) cty.Type {
    78  	ctyType, err := ctyjson.UnmarshalType(attribute.AttributeType)
    79  	if err != nil {
    80  		panic("could not unmarshal attribute type: " + err.Error())
    81  	}
    82  	return ctyType
    83  }