From d31631675b74d7237753cbbd87c5e209babe631d Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Mon, 9 Jan 2023 17:39:13 +0100 Subject: [PATCH] Add support for Tuples into the structured plan renderer (#32479) * add support for tuples to the structured plan renderer * update after latest main changes --- .../command/jsonformat/differ/attribute.go | 2 ++ internal/command/jsonformat/differ/tuple.go | 19 ++++++++++++++++ .../command/jsonformat/differ/value_test.go | 22 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 internal/command/jsonformat/differ/tuple.go diff --git a/internal/command/jsonformat/differ/attribute.go b/internal/command/jsonformat/differ/attribute.go index d8d719a6e7..48bb90b61a 100644 --- a/internal/command/jsonformat/differ/attribute.go +++ b/internal/command/jsonformat/differ/attribute.go @@ -58,6 +58,8 @@ func (v Value) computeChangeForType(ctype cty.Type) change.Change { return v.computeAttributeChangeAsMap(ctype.ElementType()) case ctype.IsListType(): return v.computeAttributeChangeAsList(ctype.ElementType()) + case ctype.IsTupleType(): + return v.computeAttributeChangeAsTuple(ctype.TupleElementTypes()) case ctype.IsSetType(): return v.computeAttributeChangeAsSet(ctype.ElementType()) default: diff --git a/internal/command/jsonformat/differ/tuple.go b/internal/command/jsonformat/differ/tuple.go new file mode 100644 index 0000000000..6f9b117ce7 --- /dev/null +++ b/internal/command/jsonformat/differ/tuple.go @@ -0,0 +1,19 @@ +package differ + +import ( + "github.com/hashicorp/terraform/internal/command/jsonformat/change" + "github.com/zclconf/go-cty/cty" +) + +func (v Value) computeAttributeChangeAsTuple(elementTypes []cty.Type) change.Change { + var elements []change.Change + current := v.getDefaultActionForIteration() + sliceValue := v.asSlice() + for ix, elementType := range elementTypes { + childValue := sliceValue.getChild(ix, ix, false) + element := childValue.computeChangeForType(elementType) + elements = append(elements, element) + current = compareActions(current, element.Action()) + } + return change.New(change.List(elements), current, v.replacePath()) +} diff --git a/internal/command/jsonformat/differ/value_test.go b/internal/command/jsonformat/differ/value_test.go index aa6e44f849..716f6d0414 100644 --- a/internal/command/jsonformat/differ/value_test.go +++ b/internal/command/jsonformat/differ/value_test.go @@ -1965,6 +1965,28 @@ func TestValue_CollectionAttributes(t *testing.T) { }, validateChange: change.ValidateComputed(change.ValidateSet(nil, plans.Delete, false), plans.Update, false), }, + "tuple_primitive": { + input: Value{ + Before: []interface{}{ + "one", + 2.0, + "three", + }, + After: []interface{}{ + "one", + 4.0, + "three", + }, + }, + attribute: &jsonprovider.Attribute{ + AttributeType: unmarshalType(t, cty.Tuple([]cty.Type{cty.String, cty.Number, cty.String})), + }, + validateChange: change.ValidateList([]change.ValidateChangeFunc{ + change.ValidatePrimitive(strptr("\"one\""), strptr("\"one\""), plans.NoOp, false), + change.ValidatePrimitive(strptr("2"), strptr("4"), plans.Update, false), + change.ValidatePrimitive(strptr("\"three\""), strptr("\"three\""), plans.NoOp, false), + }, plans.Update, false), + }, } for name, tc := range tcs {