From 9497b2cd6f457d61e9dcbfc85d260e4a040edcb3 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Mon, 13 Jun 2022 14:33:40 -0400 Subject: [PATCH] json-output: Fix unknowns for tuples and sets The JSON output for sequences previously omitted unknown values for tuples and sets, which made it impossible to interpret the corresponding unknown marks. For example, consider this resource: resource "example_resource" "example" { tags = toset(["alpha", timestamp(), "charlie"]) } This would previously be encoded in JSON as: "after": { "tags": ["alpha", "charlie"] }, "after_unknown": { "id": true, "tags": [false, true, false] }, That is, the timestamp value would be omitted from the output altogether, while the corresponding unknown marks would include a value for each of the set members. This commit changes the behaviour to: "after": { "tags": ["alpha", null, "charlie"] }, "after_unknown": { "id": true, "tags": [false, true, false] }, This aligns tuples and sets with the prior behaviour for lists, and makes it clear which elements are known and which are unknown. --- internal/command/jsonplan/plan.go | 5 +++-- internal/command/jsonplan/plan_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/command/jsonplan/plan.go b/internal/command/jsonplan/plan.go index 0a4f9bcd6e..17cc8ad37f 100644 --- a/internal/command/jsonplan/plan.go +++ b/internal/command/jsonplan/plan.go @@ -566,8 +566,9 @@ func omitUnknowns(val cty.Value) cty.Value { newVal := omitUnknowns(v) if newVal != cty.NilVal { vals = append(vals, newVal) - } else if newVal == cty.NilVal && ty.IsListType() { - // list length may be significant, so we will turn unknowns into nulls + } else if newVal == cty.NilVal { + // element order is how we correlate unknownness, so we must + // replace unknowns with nulls vals = append(vals, cty.NullVal(v.Type())) } } diff --git a/internal/command/jsonplan/plan_test.go b/internal/command/jsonplan/plan_test.go index 3c640cf6da..ef5b6cda24 100644 --- a/internal/command/jsonplan/plan_test.go +++ b/internal/command/jsonplan/plan_test.go @@ -65,6 +65,18 @@ func TestOmitUnknowns(t *testing.T) { "hello": cty.True, }), }, + { + cty.TupleVal([]cty.Value{ + cty.StringVal("alpha"), + cty.UnknownVal(cty.String), + cty.StringVal("charlie"), + }), + cty.TupleVal([]cty.Value{ + cty.StringVal("alpha"), + cty.NullVal(cty.String), + cty.StringVal("charlie"), + }), + }, { cty.SetVal([]cty.Value{ cty.StringVal("dev"), @@ -76,6 +88,7 @@ func TestOmitUnknowns(t *testing.T) { cty.StringVal("dev"), cty.StringVal("foo"), cty.StringVal("stg"), + cty.NullVal(cty.String), }), }, {