github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonformat/differ/change_map.go (about)

     1  package differ
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path"
     5  )
     6  
     7  // ChangeMap is a Change that represents a Map or an Object type, and has
     8  // converted the relevant interfaces into maps for easier access.
     9  type ChangeMap struct {
    10  	// Before contains the value before the proposed change.
    11  	Before map[string]interface{}
    12  
    13  	// After contains the value after the proposed change.
    14  	After map[string]interface{}
    15  
    16  	// Unknown contains the unknown status of any elements/attributes of this
    17  	// map/object.
    18  	Unknown map[string]interface{}
    19  
    20  	// BeforeSensitive contains the before sensitive status of any
    21  	// elements/attributes of this map/object.
    22  	BeforeSensitive map[string]interface{}
    23  
    24  	// AfterSensitive contains the after sensitive status of any
    25  	// elements/attributes of this map/object.
    26  	AfterSensitive map[string]interface{}
    27  
    28  	// ReplacePaths matches the same attributes in Change exactly.
    29  	ReplacePaths attribute_path.Matcher
    30  
    31  	// RelevantAttributes matches the same attributes in Change exactly.
    32  	RelevantAttributes attribute_path.Matcher
    33  }
    34  
    35  func (change Change) asMap() ChangeMap {
    36  	return ChangeMap{
    37  		Before:             genericToMap(change.Before),
    38  		After:              genericToMap(change.After),
    39  		Unknown:            genericToMap(change.Unknown),
    40  		BeforeSensitive:    genericToMap(change.BeforeSensitive),
    41  		AfterSensitive:     genericToMap(change.AfterSensitive),
    42  		ReplacePaths:       change.ReplacePaths,
    43  		RelevantAttributes: change.RelevantAttributes,
    44  	}
    45  }
    46  
    47  func (m ChangeMap) getChild(key string) Change {
    48  	before, beforeExplicit := getFromGenericMap(m.Before, key)
    49  	after, afterExplicit := getFromGenericMap(m.After, key)
    50  	unknown, _ := getFromGenericMap(m.Unknown, key)
    51  	beforeSensitive, _ := getFromGenericMap(m.BeforeSensitive, key)
    52  	afterSensitive, _ := getFromGenericMap(m.AfterSensitive, key)
    53  
    54  	return Change{
    55  		BeforeExplicit:     beforeExplicit,
    56  		AfterExplicit:      afterExplicit,
    57  		Before:             before,
    58  		After:              after,
    59  		Unknown:            unknown,
    60  		BeforeSensitive:    beforeSensitive,
    61  		AfterSensitive:     afterSensitive,
    62  		ReplacePaths:       m.ReplacePaths.GetChildWithKey(key),
    63  		RelevantAttributes: m.RelevantAttributes.GetChildWithKey(key),
    64  	}
    65  }
    66  
    67  func getFromGenericMap(generic map[string]interface{}, key string) (interface{}, bool) {
    68  	if generic == nil {
    69  		return nil, false
    70  	}
    71  
    72  	if child, ok := generic[key]; ok {
    73  		return child, ok
    74  	}
    75  	return nil, false
    76  }
    77  
    78  func genericToMap(generic interface{}) map[string]interface{} {
    79  	if concrete, ok := generic.(map[string]interface{}); ok {
    80  		return concrete
    81  	}
    82  	return nil
    83  }