command/format: fix missing elements at the end of lists in diffs

This commit is contained in:
Thayne McCombs 2019-11-08 17:05:23 -07:00 committed by Martin Atkins
parent 3e00447789
commit a895a42f85
2 changed files with 7 additions and 4 deletions

View File

@ -1086,8 +1086,8 @@ func ctySequenceDiff(old, new []cty.Value) []*plans.Change {
var oldI, newI, lcsI int
for oldI < len(old) || newI < len(new) || lcsI < len(lcs) {
for oldI < len(old) && (lcsI >= len(lcs) || !old[oldI].RawEquals(lcs[lcsI])) {
isObjectDiff := old[oldI].Type().IsObjectType() && (newI >= len(new) || new[newI].Type().IsObjectType())
if isObjectDiff && newI < len(new) {
isObjectDiff := old[oldI].Type().IsObjectType() && newI < len(new) && new[newI].Type().IsObjectType() && (lcsI >= len(lcs) || !new[newI].RawEquals(lcs[lcsI]))
if isObjectDiff {
ret = append(ret, &plans.Change{
Action: plans.Update,
Before: old[oldI],

View File

@ -888,11 +888,11 @@ func TestResourceChange_JSON(t *testing.T) {
Mode: addrs.ManagedResourceMode,
Before: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("i-02ae66f368e8518a9"),
"json_field": cty.StringVal(`[{"one": "111"}, {"two": "222"}]`),
"json_field": cty.StringVal(`[{"one": "111"}, {"two": "222"}, {"three": "333"}]`),
}),
After: cty.ObjectVal(map[string]cty.Value{
"id": cty.UnknownVal(cty.String),
"json_field": cty.StringVal(`[{"one": "111"}]`),
"json_field": cty.StringVal(`[{"one": "111"}, {"three": "333"}]`),
}),
Schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
@ -913,6 +913,9 @@ func TestResourceChange_JSON(t *testing.T) {
- {
- two = "222"
},
{
three = "333"
},
]
)
}