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