Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 5b97e30

Browse files
committed
Fixing conversion of null value
1 parent 0d17251 commit 5b97e30

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

workspacehelper/tfc_output.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (t *TerraformCloudClient) GetStateVersionDownloadURL(workspaceID string) (s
2424

2525
func convertValueToString(val cty.Value) string {
2626
if val.IsNull() {
27-
return ""
27+
return "null"
2828
}
2929
ty := val.Type()
3030
switch {
@@ -76,17 +76,19 @@ func convertValueToString(val cty.Value) string {
7676
var b bytes.Buffer
7777

7878
i := 0
79+
valLen := val.LengthInt()
7980
for it := val.ElementIterator(); it.Next(); {
8081
key, value := it.Element()
8182
k := convertValueToString(key)
8283
v := convertValueToString(value)
8384
if k == "" || v == "" {
85+
valLen--
8486
continue
8587
}
8688
b.WriteString(k)
8789
b.WriteString(":")
8890
b.WriteString(v)
89-
if i < (val.LengthInt() - 1) {
91+
if i < (valLen - 1) {
9092
b.WriteString(",")
9193
}
9294
i++
@@ -109,19 +111,20 @@ func convertValueToString(val cty.Value) string {
109111

110112
var b bytes.Buffer
111113
i := 0
114+
atysLen := len(atys)
112115
for _, attr := range attrNames {
113116
val := val.GetAttr(attr)
114117
v := convertValueToString(val)
115118
if v == "" {
119+
atysLen--
116120
continue
117121
}
118-
119122
b.WriteString(`"`)
120123
b.WriteString(attr)
121124
b.WriteString(`"`)
122125
b.WriteString(":")
123126
b.WriteString(v)
124-
if i < (len(atys) - 1) {
127+
if i < (atysLen - 1) {
125128
b.WriteString(",")
126129
}
127130
i++

workspacehelper/tfc_output_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestShouldReturnStringFromObject(t *testing.T) {
7070
}
7171

7272
func TestShouldReturnEmptyStringFromNullObject(t *testing.T) {
73-
expected := ""
73+
expected := "null"
7474
value := cty.NullVal(cty.Map(cty.String))
7575
formatted := convertValueToString(value)
7676
assert.Equal(t, expected, formatted)
@@ -126,7 +126,7 @@ func TestOutputsFromState(t *testing.T) {
126126
}
127127
}
128128
}`,
129-
want: []*v1alpha1.OutputStatus{},
129+
want: []*v1alpha1.OutputStatus{{Key: "map1", Value: "[{\"null_map\":null}]"}},
130130
},
131131
{
132132
name: "embedded JSON empty array returns no status",

0 commit comments

Comments
 (0)