Skip to content

Commit

Permalink
Fixing convnersion of null value
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyr committed Apr 11, 2022
1 parent 0c0b52b commit e88f2de
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions workspacehelper/tfc_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++
Expand All @@ -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++
Expand Down
4 changes: 2 additions & 2 deletions workspacehelper/tfc_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit e88f2de

Please sign in to comment.