Skip to content

Commit 491b1fa

Browse files
committed
handle complex types and nulls
1 parent 0a2b874 commit 491b1fa

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

preview_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ func Test_Extract(t *testing.T) {
5353
name: "sometags",
5454
dir: "sometags",
5555
expTags: map[string]string{
56-
"string": "foo",
57-
"number": "42",
58-
"bool": "true",
59-
"list": `["a", "b", "c"]`,
60-
"map": `{"key1": "value1", "key2": "value2"}`,
61-
"complex": `{"nested": {"key": "value"}, "nested_list": [1, 2, 3]}`,
62-
"null": "null",
56+
"string": "foo",
57+
"number": "42",
58+
"bool": "true",
59+
// null tags are omitted
60+
},
61+
unknownTags: []string{
62+
"complex", "map", "list",
6363
},
64-
unknownTags: []string{},
6564
},
6665
{
6766
name: "simple static values",

testdata/sometags/skipe2e

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Complex types are not supported by coder provider

testdata/static/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ terraform {
1515
data "coder_workspace_tags" "custom_workspace_tags" {
1616
tags = {
1717
"zone" = "developers"
18+
"null" = null
1819
}
1920
}
2021

workspacetags.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/aquasecurity/trivy/pkg/iac/terraform"
77
"github.com/hashicorp/hcl/v2"
88
"github.com/zclconf/go-cty/cty"
9-
"github.com/zclconf/go-cty/cty/json"
109

1110
"github.com/coder/preview/types"
1211
)
@@ -50,6 +49,12 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
5049

5150
var tags []types.Tag
5251
tagsValue.ForEachElement(func(key cty.Value, val cty.Value) (stop bool) {
52+
if val.IsNull() {
53+
// null tags with null values are omitted
54+
// This matches the behavior of `terraform apply``
55+
return false
56+
}
57+
5358
r := tagsAttr.HCLAttribute().Expr.Range()
5459
tag, tagDiag := newTag(&r, files, key, val)
5560
if tagDiag != nil {
@@ -101,19 +106,13 @@ func newTag(srcRange *hcl.Range, _ map[string]*hcl.File, key, val cty.Value) (ty
101106
fr = val.Type().FriendlyName()
102107
}
103108

104-
// Unsupported types will be converted to a JSON string representation.
105-
jsonData, err := json.Marshal(val, val.Type())
106-
if err != nil {
107-
tag.Value.ValueDiags = tag.Value.ValueDiags.Append(&hcl.Diagnostic{
108-
Severity: hcl.DiagError,
109-
Summary: fmt.Sprintf("Invalid value type for tag %q", tag.KeyString()),
110-
Detail: fmt.Sprintf("Value must be a string, but got %s. Attempt to marshal to json: %s", fr, err.Error()),
111-
Context: srcRange,
112-
})
113-
} else {
114-
// Value successfully marshaled to JSON, we can store it as a string.
115-
tag.Value.Value = cty.StringVal(string(jsonData))
116-
}
109+
// Unsupported types will be treated as errors.
110+
tag.Value.ValueDiags = tag.Value.ValueDiags.Append(&hcl.Diagnostic{
111+
Severity: hcl.DiagError,
112+
Summary: fmt.Sprintf("Invalid value type for tag %q", tag.KeyString()),
113+
Detail: fmt.Sprintf("Value must be a string, but got %s.", fr),
114+
Context: srcRange,
115+
})
117116
}
118117

119118
return tag, nil

0 commit comments

Comments
 (0)