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

     1  package renderers
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
     7  )
     8  
     9  // Blocks is a helper struct for collating the different kinds of blocks in a
    10  // simple way for rendering.
    11  type Blocks struct {
    12  	SingleBlocks map[string]computed.Diff
    13  	ListBlocks   map[string][]computed.Diff
    14  	SetBlocks    map[string][]computed.Diff
    15  	MapBlocks    map[string]map[string]computed.Diff
    16  
    17  	// ReplaceBlocks and Before/AfterSensitiveBlocks carry forward the
    18  	// information about an entire group of blocks (eg. if all the blocks for a
    19  	// given list block are sensitive that isn't captured in the individual
    20  	// blocks as they are processed independently). These maps allow the
    21  	// renderer to check the metadata on the overall groups and respond
    22  	// accordingly.
    23  
    24  	ReplaceBlocks         map[string]bool
    25  	BeforeSensitiveBlocks map[string]bool
    26  	AfterSensitiveBlocks  map[string]bool
    27  }
    28  
    29  func (blocks *Blocks) GetAllKeys() []string {
    30  	var keys []string
    31  	for key := range blocks.SingleBlocks {
    32  		keys = append(keys, key)
    33  	}
    34  	for key := range blocks.ListBlocks {
    35  		keys = append(keys, key)
    36  	}
    37  	for key := range blocks.SetBlocks {
    38  		keys = append(keys, key)
    39  	}
    40  	for key := range blocks.MapBlocks {
    41  		keys = append(keys, key)
    42  	}
    43  	sort.Strings(keys)
    44  	return keys
    45  }
    46  
    47  func (blocks *Blocks) IsSingleBlock(key string) bool {
    48  	_, ok := blocks.SingleBlocks[key]
    49  	return ok
    50  }
    51  
    52  func (blocks *Blocks) IsListBlock(key string) bool {
    53  	_, ok := blocks.ListBlocks[key]
    54  	return ok
    55  }
    56  
    57  func (blocks *Blocks) IsMapBlock(key string) bool {
    58  	_, ok := blocks.MapBlocks[key]
    59  	return ok
    60  }
    61  
    62  func (blocks *Blocks) IsSetBlock(key string) bool {
    63  	_, ok := blocks.SetBlocks[key]
    64  	return ok
    65  }
    66  
    67  func (blocks *Blocks) AddSingleBlock(key string, diff computed.Diff, replace, beforeSensitive, afterSensitive bool) {
    68  	blocks.SingleBlocks[key] = diff
    69  	blocks.ReplaceBlocks[key] = replace
    70  	blocks.BeforeSensitiveBlocks[key] = beforeSensitive
    71  	blocks.AfterSensitiveBlocks[key] = afterSensitive
    72  }
    73  
    74  func (blocks *Blocks) AddAllListBlock(key string, diffs []computed.Diff, replace, beforeSensitive, afterSensitive bool) {
    75  	blocks.ListBlocks[key] = diffs
    76  	blocks.ReplaceBlocks[key] = replace
    77  	blocks.BeforeSensitiveBlocks[key] = beforeSensitive
    78  	blocks.AfterSensitiveBlocks[key] = afterSensitive
    79  }
    80  
    81  func (blocks *Blocks) AddAllSetBlock(key string, diffs []computed.Diff, replace, beforeSensitive, afterSensitive bool) {
    82  	blocks.SetBlocks[key] = diffs
    83  	blocks.ReplaceBlocks[key] = replace
    84  	blocks.BeforeSensitiveBlocks[key] = beforeSensitive
    85  	blocks.AfterSensitiveBlocks[key] = afterSensitive
    86  }
    87  
    88  func (blocks *Blocks) AddAllMapBlocks(key string, diffs map[string]computed.Diff, replace, beforeSensitive, afterSensitive bool) {
    89  	blocks.MapBlocks[key] = diffs
    90  	blocks.ReplaceBlocks[key] = replace
    91  	blocks.BeforeSensitiveBlocks[key] = beforeSensitive
    92  	blocks.AfterSensitiveBlocks[key] = afterSensitive
    93  }