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

     1  package computed
     2  
     3  import (
     4  	"github.com/mitchellh/colorstring"
     5  
     6  	"github.com/hashicorp/terraform/internal/plans"
     7  )
     8  
     9  // Diff captures the computed diff for a single block, element or attribute.
    10  //
    11  // It essentially merges common functionality across all types of changes,
    12  // namely the replace logic and the action / change type. Any remaining
    13  // behaviour can be offloaded to the renderer which will be unique for the
    14  // various change types (eg. maps, objects, lists, blocks, primitives, etc.).
    15  type Diff struct {
    16  	// Renderer captures the uncommon functionality across the different kinds
    17  	// of changes. Each type of change (lists, blocks, sets, etc.) will have a
    18  	// unique renderer.
    19  	Renderer DiffRenderer
    20  
    21  	// Action is the action described by this change (such as create, delete,
    22  	// update, etc.).
    23  	Action plans.Action
    24  
    25  	// Replace tells the Change that it should add the `# forces replacement`
    26  	// suffix.
    27  	//
    28  	// Every single change could potentially add this suffix, so we embed it in
    29  	// the change as common functionality instead of in the specific renderers.
    30  	Replace bool
    31  }
    32  
    33  // NewDiff creates a new Diff object with the provided renderer, action and
    34  // replace context.
    35  func NewDiff(renderer DiffRenderer, action plans.Action, replace bool) Diff {
    36  	return Diff{
    37  		Renderer: renderer,
    38  		Action:   action,
    39  		Replace:  replace,
    40  	}
    41  }
    42  
    43  // RenderHuman prints the Change into a human-readable string referencing the
    44  // specified RenderOpts.
    45  //
    46  // If the returned string is a single line, then indent should be ignored.
    47  //
    48  // If the return string is multiple lines, then indent should be used to offset
    49  // the beginning of all lines but the first by the specified amount.
    50  func (diff Diff) RenderHuman(indent int, opts RenderHumanOpts) string {
    51  	return diff.Renderer.RenderHuman(diff, indent, opts)
    52  }
    53  
    54  // WarningsHuman returns a list of strings that should be rendered as warnings
    55  // before a given change is rendered.
    56  //
    57  // As with the RenderHuman function, the indent should only be applied on
    58  // multiline warnings and on the second and following lines.
    59  func (diff Diff) WarningsHuman(indent int, opts RenderHumanOpts) []string {
    60  	return diff.Renderer.WarningsHuman(diff, indent, opts)
    61  }
    62  
    63  type DiffRenderer interface {
    64  	RenderHuman(diff Diff, indent int, opts RenderHumanOpts) string
    65  	WarningsHuman(diff Diff, indent int, opts RenderHumanOpts) []string
    66  }
    67  
    68  // RenderHumanOpts contains options that can control how the human render
    69  // function of the DiffRenderer will function.
    70  type RenderHumanOpts struct {
    71  	Colorize *colorstring.Colorize
    72  
    73  	// OverrideNullSuffix tells the Renderer not to display the `-> null` suffix
    74  	// that is normally displayed when an element, attribute, or block is
    75  	// deleted.
    76  	OverrideNullSuffix bool
    77  
    78  	// OverrideForcesReplacement tells the Renderer to display the
    79  	// `# forces replacement` suffix, even if a diff doesn't have the Replace
    80  	// field set.
    81  	//
    82  	// Some renderers (like the Set renderer) don't display the suffix
    83  	// themselves but force their child diffs to display it instead.
    84  	OverrideForcesReplacement bool
    85  
    86  	// ShowUnchangedChildren instructs the Renderer to render all children of a
    87  	// given complex change, instead of hiding unchanged items and compressing
    88  	// them into a single line.
    89  	ShowUnchangedChildren bool
    90  
    91  	// HideDiffActionSymbols tells the renderer not to show the '+'/'-' symbols
    92  	// and to skip the places where the symbols would result in an offset.
    93  	HideDiffActionSymbols bool
    94  }
    95  
    96  // NewRenderHumanOpts creates a new RenderHumanOpts struct with the required
    97  // fields set.
    98  func NewRenderHumanOpts(colorize *colorstring.Colorize) RenderHumanOpts {
    99  	return RenderHumanOpts{
   100  		Colorize: colorize,
   101  	}
   102  }
   103  
   104  // Clone returns a new RenderOpts object, that matches the original but can be
   105  // edited without changing the original.
   106  func (opts RenderHumanOpts) Clone() RenderHumanOpts {
   107  	return RenderHumanOpts{
   108  		Colorize: opts.Colorize,
   109  
   110  		OverrideNullSuffix:    opts.OverrideNullSuffix,
   111  		ShowUnchangedChildren: opts.ShowUnchangedChildren,
   112  		HideDiffActionSymbols: opts.HideDiffActionSymbols,
   113  
   114  		// OverrideForcesReplacement is a special case in that it doesn't
   115  		// cascade. So each diff should decide independently whether it's direct
   116  		// children should override their internal Replace logic, instead of
   117  		// an ancestor making the switch and affecting the entire tree.
   118  		OverrideForcesReplacement: false,
   119  	}
   120  }