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