-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
map.go
35 lines (29 loc) · 897 Bytes
/
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
package collections
import (
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
"github.com/hashicorp/terraform/internal/plans"
)
type ProcessKey func(key string) computed.Diff
func TransformMap[Input any](before, after map[string]Input, process ProcessKey) (map[string]computed.Diff, plans.Action) {
current := plans.NoOp
if before != nil && after == nil {
current = plans.Delete
}
if before == nil && after != nil {
current = plans.Create
}
elements := make(map[string]computed.Diff)
for key := range before {
elements[key] = process(key)
current = CompareActions(current, elements[key].Action)
}
for key := range after {
if _, ok := elements[key]; ok {
// Then we've already processed this key in the before.
continue
}
elements[key] = process(key)
current = CompareActions(current, elements[key].Action)
}
return elements, current
}