github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/jsonformat/differ/map.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package differ 5 6 import ( 7 "github.com/zclconf/go-cty/cty" 8 9 "github.com/terramate-io/tf/command/jsonformat/collections" 10 "github.com/terramate-io/tf/command/jsonformat/computed" 11 "github.com/terramate-io/tf/command/jsonformat/computed/renderers" 12 "github.com/terramate-io/tf/command/jsonformat/structured" 13 "github.com/terramate-io/tf/command/jsonprovider" 14 "github.com/terramate-io/tf/plans" 15 ) 16 17 func computeAttributeDiffAsMap(change structured.Change, elementType cty.Type) computed.Diff { 18 mapValue := change.AsMap() 19 elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.AllKeys(), func(key string) computed.Diff { 20 value := mapValue.GetChild(key) 21 if !value.RelevantAttributes.MatchesPartial() { 22 // Mark non-relevant attributes as unchanged. 23 value = value.AsNoOp() 24 } 25 return ComputeDiffForType(value, elementType) 26 }) 27 return computed.NewDiff(renderers.Map(elements), current, change.ReplacePaths.Matches()) 28 } 29 30 func computeAttributeDiffAsNestedMap(change structured.Change, attributes map[string]*jsonprovider.Attribute) computed.Diff { 31 mapValue := change.AsMap() 32 elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff { 33 value := mapValue.GetChild(key) 34 if !value.RelevantAttributes.MatchesPartial() { 35 // Mark non-relevant attributes as unchanged. 36 value = value.AsNoOp() 37 } 38 return computeDiffForNestedAttribute(value, &jsonprovider.NestedType{ 39 Attributes: attributes, 40 NestingMode: "single", 41 }) 42 }) 43 return computed.NewDiff(renderers.NestedMap(elements), current, change.ReplacePaths.Matches()) 44 } 45 46 func computeBlockDiffsAsMap(change structured.Change, block *jsonprovider.Block) (map[string]computed.Diff, plans.Action) { 47 mapValue := change.AsMap() 48 return collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff { 49 value := mapValue.GetChild(key) 50 if !value.RelevantAttributes.MatchesPartial() { 51 // Mark non-relevant attributes as unchanged. 52 value = value.AsNoOp() 53 } 54 return ComputeDiffForBlock(value, block) 55 }) 56 }