2023-01-09 07:06:38 -06:00
|
|
|
package differ
|
|
|
|
|
2023-01-16 08:18:38 -06:00
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path"
|
|
|
|
)
|
2023-01-11 03:20:24 -06:00
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
// ChangeSlice is a Change that represents a Tuple, Set, or List type, and has
|
|
|
|
// converted the relevant interfaces into slices for easier access.
|
|
|
|
type ChangeSlice struct {
|
2023-01-09 07:06:38 -06:00
|
|
|
// Before contains the value before the proposed change.
|
|
|
|
Before []interface{}
|
|
|
|
|
|
|
|
// After contains the value after the proposed change.
|
|
|
|
After []interface{}
|
|
|
|
|
|
|
|
// Unknown contains the unknown status of any elements of this list/set.
|
|
|
|
Unknown []interface{}
|
|
|
|
|
|
|
|
// BeforeSensitive contains the before sensitive status of any elements of
|
|
|
|
//this list/set.
|
|
|
|
BeforeSensitive []interface{}
|
|
|
|
|
|
|
|
// AfterSensitive contains the after sensitive status of any elements of
|
|
|
|
//this list/set.
|
|
|
|
AfterSensitive []interface{}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
// ReplacePaths matches the same attributes in Change exactly.
|
2023-01-16 08:18:38 -06:00
|
|
|
ReplacePaths attribute_path.Matcher
|
|
|
|
|
|
|
|
// RelevantAttributes matches the same attributes in Change exactly.
|
|
|
|
RelevantAttributes attribute_path.Matcher
|
2023-01-09 07:06:38 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) asSlice() ChangeSlice {
|
|
|
|
return ChangeSlice{
|
2023-01-16 08:18:38 -06:00
|
|
|
Before: genericToSlice(change.Before),
|
|
|
|
After: genericToSlice(change.After),
|
|
|
|
Unknown: genericToSlice(change.Unknown),
|
|
|
|
BeforeSensitive: genericToSlice(change.BeforeSensitive),
|
|
|
|
AfterSensitive: genericToSlice(change.AfterSensitive),
|
|
|
|
ReplacePaths: change.ReplacePaths,
|
|
|
|
RelevantAttributes: change.RelevantAttributes,
|
2023-01-09 07:06:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 03:20:24 -06:00
|
|
|
func (s ChangeSlice) getChild(beforeIx, afterIx int) Change {
|
2023-01-09 07:06:38 -06:00
|
|
|
before, beforeExplicit := getFromGenericSlice(s.Before, beforeIx)
|
|
|
|
after, afterExplicit := getFromGenericSlice(s.After, afterIx)
|
|
|
|
unknown, _ := getFromGenericSlice(s.Unknown, afterIx)
|
|
|
|
beforeSensitive, _ := getFromGenericSlice(s.BeforeSensitive, beforeIx)
|
|
|
|
afterSensitive, _ := getFromGenericSlice(s.AfterSensitive, afterIx)
|
|
|
|
|
2023-01-16 08:18:38 -06:00
|
|
|
mostRelevantIx := beforeIx
|
|
|
|
if beforeIx < 0 || beforeIx >= len(s.Before) {
|
|
|
|
mostRelevantIx = afterIx
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
return Change{
|
2023-01-16 08:18:38 -06:00
|
|
|
BeforeExplicit: beforeExplicit,
|
|
|
|
AfterExplicit: afterExplicit,
|
|
|
|
Before: before,
|
|
|
|
After: after,
|
|
|
|
Unknown: unknown,
|
|
|
|
BeforeSensitive: beforeSensitive,
|
|
|
|
AfterSensitive: afterSensitive,
|
|
|
|
ReplacePaths: s.ReplacePaths.GetChildWithIndex(mostRelevantIx),
|
|
|
|
RelevantAttributes: s.RelevantAttributes.GetChildWithIndex(mostRelevantIx),
|
2023-01-09 07:06:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFromGenericSlice(generic []interface{}, ix int) (interface{}, bool) {
|
|
|
|
if generic == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if ix < 0 || ix >= len(generic) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return generic[ix], true
|
|
|
|
}
|
|
|
|
|
|
|
|
func genericToSlice(generic interface{}) []interface{} {
|
|
|
|
if concrete, ok := generic.([]interface{}); ok {
|
|
|
|
return concrete
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|