github.com/opentofu/opentofu@v1.7.1/internal/command/jsonformat/collections/map.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 collections
     7  
     8  import (
     9  	"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
    10  	"github.com/opentofu/opentofu/internal/plans"
    11  )
    12  
    13  type ProcessKey func(key string) computed.Diff
    14  
    15  func TransformMap[Input any](before, after map[string]Input, keys []string, process ProcessKey) (map[string]computed.Diff, plans.Action) {
    16  	current := plans.NoOp
    17  	if before != nil && after == nil {
    18  		current = plans.Delete
    19  	}
    20  	if before == nil && after != nil {
    21  		current = plans.Create
    22  	}
    23  
    24  	elements := make(map[string]computed.Diff)
    25  	for _, key := range keys {
    26  		elements[key] = process(key)
    27  		current = CompareActions(current, elements[key].Action)
    28  	}
    29  
    30  	return elements, current
    31  }