github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/jsonformat/collections/map.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package collections
     5  
     6  import (
     7  	"github.com/terramate-io/tf/command/jsonformat/computed"
     8  	"github.com/terramate-io/tf/plans"
     9  )
    10  
    11  type ProcessKey func(key string) computed.Diff
    12  
    13  func TransformMap[Input any](before, after map[string]Input, keys []string, process ProcessKey) (map[string]computed.Diff, plans.Action) {
    14  	current := plans.NoOp
    15  	if before != nil && after == nil {
    16  		current = plans.Delete
    17  	}
    18  	if before == nil && after != nil {
    19  		current = plans.Create
    20  	}
    21  
    22  	elements := make(map[string]computed.Diff)
    23  	for _, key := range keys {
    24  		elements[key] = process(key)
    25  		current = CompareActions(current, elements[key].Action)
    26  	}
    27  
    28  	return elements, current
    29  }