github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/jsonformat/differ/unknown.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/computed"
    10  	"github.com/terramate-io/tf/command/jsonformat/computed/renderers"
    11  	"github.com/terramate-io/tf/command/jsonformat/structured"
    12  	"github.com/terramate-io/tf/command/jsonprovider"
    13  )
    14  
    15  func checkForUnknownType(change structured.Change, ctype cty.Type) (computed.Diff, bool) {
    16  	return change.CheckForUnknown(
    17  		false,
    18  		processUnknown,
    19  		createProcessUnknownWithBefore(func(value structured.Change) computed.Diff {
    20  			return ComputeDiffForType(value, ctype)
    21  		}))
    22  }
    23  
    24  func checkForUnknownNestedAttribute(change structured.Change, attribute *jsonprovider.NestedType) (computed.Diff, bool) {
    25  
    26  	// We want our child attributes to show up as computed instead of deleted.
    27  	// Let's populate that here.
    28  	childUnknown := make(map[string]interface{})
    29  	for key := range attribute.Attributes {
    30  		childUnknown[key] = true
    31  	}
    32  
    33  	return change.CheckForUnknown(
    34  		childUnknown,
    35  		processUnknown,
    36  		createProcessUnknownWithBefore(func(value structured.Change) computed.Diff {
    37  			return computeDiffForNestedAttribute(value, attribute)
    38  		}))
    39  }
    40  
    41  func checkForUnknownBlock(change structured.Change, block *jsonprovider.Block) (computed.Diff, bool) {
    42  
    43  	// We want our child attributes to show up as computed instead of deleted.
    44  	// Let's populate that here.
    45  	childUnknown := make(map[string]interface{})
    46  	for key := range block.Attributes {
    47  		childUnknown[key] = true
    48  	}
    49  
    50  	return change.CheckForUnknown(
    51  		childUnknown,
    52  		processUnknown,
    53  		createProcessUnknownWithBefore(func(value structured.Change) computed.Diff {
    54  			return ComputeDiffForBlock(value, block)
    55  		}))
    56  }
    57  
    58  func processUnknown(current structured.Change) computed.Diff {
    59  	return asDiff(current, renderers.Unknown(computed.Diff{}))
    60  }
    61  
    62  func createProcessUnknownWithBefore(computeDiff func(value structured.Change) computed.Diff) structured.ProcessUnknownWithBefore {
    63  	return func(current structured.Change, before structured.Change) computed.Diff {
    64  		return asDiff(current, renderers.Unknown(computeDiff(before)))
    65  	}
    66  }