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

     1  package renderers
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
     8  	"github.com/hashicorp/terraform/internal/plans"
     9  )
    10  
    11  var _ computed.DiffRenderer = (*setRenderer)(nil)
    12  
    13  func Set(elements []computed.Diff) computed.DiffRenderer {
    14  	return &setRenderer{
    15  		elements: elements,
    16  	}
    17  }
    18  
    19  func NestedSet(elements []computed.Diff) computed.DiffRenderer {
    20  	return &setRenderer{
    21  		elements:                  elements,
    22  		overrideForcesReplacement: true,
    23  	}
    24  }
    25  
    26  type setRenderer struct {
    27  	NoWarningsRenderer
    28  
    29  	elements []computed.Diff
    30  
    31  	overrideForcesReplacement bool
    32  }
    33  
    34  func (renderer setRenderer) RenderHuman(diff computed.Diff, indent int, opts computed.RenderHumanOpts) string {
    35  	// Sets are a bit finicky, nested sets don't render the forces replacement
    36  	// suffix themselves, but push it onto their children. So if we are
    37  	// overriding the forces replacement setting, we set it to true for children
    38  	// and false for ourselves.
    39  	displayForcesReplacementInSelf := diff.Replace && !renderer.overrideForcesReplacement
    40  	displayForcesReplacementInChildren := diff.Replace && renderer.overrideForcesReplacement
    41  
    42  	if len(renderer.elements) == 0 {
    43  		return fmt.Sprintf("[]%s%s", nullSuffix(diff.Action, opts), forcesReplacement(displayForcesReplacementInSelf, opts))
    44  	}
    45  
    46  	elementOpts := opts.Clone()
    47  	elementOpts.OverrideNullSuffix = true
    48  	elementOpts.OverrideForcesReplacement = displayForcesReplacementInChildren
    49  
    50  	unchangedElements := 0
    51  
    52  	var buf bytes.Buffer
    53  	buf.WriteString(fmt.Sprintf("[%s\n", forcesReplacement(displayForcesReplacementInSelf, opts)))
    54  	for _, element := range renderer.elements {
    55  		if element.Action == plans.NoOp && !opts.ShowUnchangedChildren {
    56  			unchangedElements++
    57  			continue
    58  		}
    59  
    60  		for _, warning := range element.WarningsHuman(indent+1, opts) {
    61  			buf.WriteString(fmt.Sprintf("%s%s\n", formatIndent(indent+1), warning))
    62  		}
    63  		buf.WriteString(fmt.Sprintf("%s%s%s,\n", formatIndent(indent+1), writeDiffActionSymbol(element.Action, elementOpts), element.RenderHuman(indent+1, elementOpts)))
    64  	}
    65  
    66  	if unchangedElements > 0 {
    67  		buf.WriteString(fmt.Sprintf("%s%s%s\n", formatIndent(indent+1), writeDiffActionSymbol(plans.NoOp, opts), unchanged("element", unchangedElements, opts)))
    68  	}
    69  
    70  	buf.WriteString(fmt.Sprintf("%s%s]%s", formatIndent(indent), writeDiffActionSymbol(plans.NoOp, opts), nullSuffix(diff.Action, opts)))
    71  	return buf.String()
    72  }