From 5b97e30edb359cdba3d4110fe66df153446940bc Mon Sep 17 00:00:00 2001 From: Jiri Tyr Date: Fri, 8 Apr 2022 15:44:39 +0200 Subject: [PATCH] Fixing conversion of null value --- workspacehelper/tfc_output.go | 11 +++++++---- workspacehelper/tfc_output_test.go | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/workspacehelper/tfc_output.go b/workspacehelper/tfc_output.go index f6418e73..f3b70e7c 100644 --- a/workspacehelper/tfc_output.go +++ b/workspacehelper/tfc_output.go @@ -24,7 +24,7 @@ func (t *TerraformCloudClient) GetStateVersionDownloadURL(workspaceID string) (s func convertValueToString(val cty.Value) string { if val.IsNull() { - return "" + return "null" } ty := val.Type() switch { @@ -76,17 +76,19 @@ func convertValueToString(val cty.Value) string { var b bytes.Buffer i := 0 + valLen := val.LengthInt() for it := val.ElementIterator(); it.Next(); { key, value := it.Element() k := convertValueToString(key) v := convertValueToString(value) if k == "" || v == "" { + valLen-- continue } b.WriteString(k) b.WriteString(":") b.WriteString(v) - if i < (val.LengthInt() - 1) { + if i < (valLen - 1) { b.WriteString(",") } i++ @@ -109,19 +111,20 @@ func convertValueToString(val cty.Value) string { var b bytes.Buffer i := 0 + atysLen := len(atys) for _, attr := range attrNames { val := val.GetAttr(attr) v := convertValueToString(val) if v == "" { + atysLen-- continue } - b.WriteString(`"`) b.WriteString(attr) b.WriteString(`"`) b.WriteString(":") b.WriteString(v) - if i < (len(atys) - 1) { + if i < (atysLen - 1) { b.WriteString(",") } i++ diff --git a/workspacehelper/tfc_output_test.go b/workspacehelper/tfc_output_test.go index 4fddcdfc..c51cf062 100644 --- a/workspacehelper/tfc_output_test.go +++ b/workspacehelper/tfc_output_test.go @@ -70,7 +70,7 @@ func TestShouldReturnStringFromObject(t *testing.T) { } func TestShouldReturnEmptyStringFromNullObject(t *testing.T) { - expected := "" + expected := "null" value := cty.NullVal(cty.Map(cty.String)) formatted := convertValueToString(value) assert.Equal(t, expected, formatted) @@ -126,7 +126,7 @@ func TestOutputsFromState(t *testing.T) { } } }`, - want: []*v1alpha1.OutputStatus{}, + want: []*v1alpha1.OutputStatus{{Key: "map1", Value: "[{\"null_map\":null}]"}}, }, { name: "embedded JSON empty array returns no status",