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

     1  package renderers
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"sort"
     7  
     8  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
     9  	"github.com/hashicorp/terraform/internal/plans"
    10  )
    11  
    12  var _ computed.DiffRenderer = (*objectRenderer)(nil)
    13  
    14  func Object(attributes map[string]computed.Diff) computed.DiffRenderer {
    15  	return &objectRenderer{
    16  		attributes:         attributes,
    17  		overrideNullSuffix: true,
    18  	}
    19  }
    20  
    21  func NestedObject(attributes map[string]computed.Diff) computed.DiffRenderer {
    22  	return &objectRenderer{
    23  		attributes:         attributes,
    24  		overrideNullSuffix: false,
    25  	}
    26  }
    27  
    28  type objectRenderer struct {
    29  	NoWarningsRenderer
    30  
    31  	attributes         map[string]computed.Diff
    32  	overrideNullSuffix bool
    33  }
    34  
    35  func (renderer objectRenderer) RenderHuman(diff computed.Diff, indent int, opts computed.RenderHumanOpts) string {
    36  	if len(renderer.attributes) == 0 {
    37  		return fmt.Sprintf("{}%s%s", nullSuffix(diff.Action, opts), forcesReplacement(diff.Replace, opts))
    38  	}
    39  
    40  	attributeOpts := opts.Clone()
    41  	attributeOpts.OverrideNullSuffix = renderer.overrideNullSuffix
    42  
    43  	// We need to keep track of our keys in two ways. The first is the order in
    44  	// which we will display them. The second is a mapping to their safely
    45  	// escaped equivalent.
    46  
    47  	maximumKeyLen := 0
    48  	var keys []string
    49  	escapedKeys := make(map[string]string)
    50  	for key := range renderer.attributes {
    51  		keys = append(keys, key)
    52  		escapedKey := EnsureValidAttributeName(key)
    53  		escapedKeys[key] = escapedKey
    54  		if maximumKeyLen < len(escapedKey) {
    55  			maximumKeyLen = len(escapedKey)
    56  		}
    57  	}
    58  	sort.Strings(keys)
    59  
    60  	unchangedAttributes := 0
    61  	var buf bytes.Buffer
    62  	buf.WriteString(fmt.Sprintf("{%s\n", forcesReplacement(diff.Replace, opts)))
    63  	for _, key := range keys {
    64  		attribute := renderer.attributes[key]
    65  
    66  		if importantAttribute(key) {
    67  			importantAttributeOpts := attributeOpts.Clone()
    68  			importantAttributeOpts.ShowUnchangedChildren = true
    69  
    70  			for _, warning := range attribute.WarningsHuman(indent+1, importantAttributeOpts) {
    71  				buf.WriteString(fmt.Sprintf("%s%s\n", formatIndent(indent+1), warning))
    72  			}
    73  			buf.WriteString(fmt.Sprintf("%s%s%-*s = %s\n", formatIndent(indent+1), writeDiffActionSymbol(attribute.Action, importantAttributeOpts), maximumKeyLen, escapedKeys[key], attribute.RenderHuman(indent+1, importantAttributeOpts)))
    74  			continue
    75  		}
    76  
    77  		if attribute.Action == plans.NoOp && !opts.ShowUnchangedChildren {
    78  			// Don't render NoOp operations when we are compact display.
    79  			unchangedAttributes++
    80  			continue
    81  		}
    82  
    83  		for _, warning := range attribute.WarningsHuman(indent+1, opts) {
    84  			buf.WriteString(fmt.Sprintf("%s%s\n", formatIndent(indent+1), warning))
    85  		}
    86  		buf.WriteString(fmt.Sprintf("%s%s%-*s = %s\n", formatIndent(indent+1), writeDiffActionSymbol(attribute.Action, attributeOpts), maximumKeyLen, escapedKeys[key], attribute.RenderHuman(indent+1, attributeOpts)))
    87  	}
    88  
    89  	if unchangedAttributes > 0 {
    90  		buf.WriteString(fmt.Sprintf("%s%s%s\n", formatIndent(indent+1), writeDiffActionSymbol(plans.NoOp, opts), unchanged("attribute", unchangedAttributes, opts)))
    91  	}
    92  
    93  	buf.WriteString(fmt.Sprintf("%s%s}%s", formatIndent(indent), writeDiffActionSymbol(plans.NoOp, opts), nullSuffix(diff.Action, opts)))
    94  	return buf.String()
    95  }