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.
This commit is contained in:
Alisdair McDiarmid 2022-06-13 14:33:40 -04:00
parent f6752c6cfa
commit 9497b2cd6f
2 changed files with 16 additions and 2 deletions

View file

@ -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()))
}
}

View file

@ -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),
}),
},
{