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