Skip to content

feat: allow bool + number tag values #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,27 @@ func Test_Extract(t *testing.T) {
dir: "badparam",
failPreview: true,
},
{
name: "sometags",
dir: "sometags",
expTags: map[string]string{
"string": "foo",
"number": "42",
"bool": "true",
"extra": "bar",
"list": `["a", "b", "c"]`,
"map": `{"key1": "value1", "key2": "value2"}`,
"complex": `{"nested": {"key": "value"}, "nested_list": [1, 2, 3]}`,
"null": "null",
},
unknownTags: []string{},
},
{
name: "simple static values",
dir: "static",
expTags: map[string]string{
"zone": "developers",
},
unknownTags: []string{},
params: map[string]assertParam{
"region": ap().value("us").
def("us").
Expand Down Expand Up @@ -510,7 +524,18 @@ func Test_Extract(t *testing.T) {
// Assert tags
validTags := output.WorkspaceTags.Tags()

assert.Equal(t, tc.expTags, validTags)
for k, expected := range tc.expTags {
tag, ok := validTags[k]
if !ok {
t.Errorf("expected tag %q to be present in output, but it was not", k)
continue
}
if tag != expected {
assert.JSONEqf(t, expected, tag, "tag %q does not match expected, nor is it a json equivalent", k)
}
}
assert.Equal(t, len(tc.expTags), len(output.WorkspaceTags.Tags()), "unexpected number of tags in output")

assert.ElementsMatch(t, tc.unknownTags, output.WorkspaceTags.UnusableTags().SafeNames())

// Assert params
Expand Down
35 changes: 35 additions & 0 deletions testdata/sometags/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "2.4.0-pre0"
}
}
}

data "coder_workspace_tags" "custom_workspace_tags" {
tags = {
"string" = "foo"
"number" = 42
"bool" = true
"list" = ["a", "b", "c"]
"map" = {
"key1" = "value1"
"key2" = "value2"
}
"complex" = {
"nested_list" = [1, 2, 3]
"nested" = {
"key" = "value"
}
}
"null" = null
}
}


data "coder_workspace_tags" "custom_workspace_tags" {
tags = {
"extra" = "bar"
}
}
1 change: 1 addition & 0 deletions testdata/sometags/skipe2e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bad tags are not going to play well with terraform apply.
98 changes: 26 additions & 72 deletions workspacetags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aquasecurity/trivy/pkg/iac/terraform"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/json"

"github.com/coder/preview/types"
)
Expand Down Expand Up @@ -47,21 +48,6 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
continue
}

// tagsObj, ok := tagsAttr.HCLAttribute().Expr.(*hclsyntax.ObjectConsExpr)
// if !ok {
// diags = diags.Append(&hcl.Diagnostic{
// Severity: hcl.DiagError,
// Summary: "Incorrect type for \"tags\" attribute",
// // TODO: better error message for types
// Detail: fmt.Sprintf(`"tags" attribute must be an 'ObjectConsExpr', but got %T`, tagsAttr.HCLAttribute().Expr),
// Subject: &tagsAttr.HCLAttribute().NameRange,
// Context: &tagsAttr.HCLAttribute().Range,
// Expression: tagsAttr.HCLAttribute().Expr,
// EvalContext: block.Context().Inner(),
// })
// continue
//}

var tags []types.Tag
tagsValue.ForEachElement(func(key cty.Value, val cty.Value) (stop bool) {
r := tagsAttr.HCLAttribute().Expr.Range()
Expand All @@ -75,15 +61,7 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types

return false
})
// for _, item := range tagsObj.Items {
// tag, tagDiag := newTag(tagsObj, files, item, evCtx)
// if tagDiag != nil {
// diags = diags.Append(tagDiag)
// continue
// }
//
// tags = append(tags, tag)
//}

tagBlocks = append(tagBlocks, types.TagBlock{
Tags: tags,
Block: block,
Expand All @@ -96,71 +74,47 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types

// newTag creates a workspace tag from its hcl expression.
func newTag(srcRange *hcl.Range, _ map[string]*hcl.File, key, val cty.Value) (types.Tag, *hcl.Diagnostic) {
// key, kdiags := expr.KeyExpr.Value(evCtx)
// val, vdiags := expr.ValueExpr.Value(evCtx)

// TODO: ???

// if kdiags.HasErrors() {
// key = cty.UnknownVal(cty.String)
//}
// if vdiags.HasErrors() {
// val = cty.UnknownVal(cty.String)
//}

if key.IsKnown() && key.Type() != cty.String {
return types.Tag{}, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid key type for tags",
Detail: fmt.Sprintf("Key must be a string, but got %s", key.Type().FriendlyName()),
//Subject: &r,
Context: srcRange,
//Expression: expr.KeyExpr,
//EvalContext: evCtx,
}
}

if val.IsKnown() && val.Type() != cty.String {
fr := "<nil>"
if !val.Type().Equals(cty.NilType) {
fr = val.Type().FriendlyName()
}
// r := expr.ValueExpr.Range()
return types.Tag{}, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid value type for tag",
Detail: fmt.Sprintf("Value must be a string, but got %s", fr),
//Subject: &r,
Context: srcRange,
//Expression: expr.ValueExpr,
//EvalContext: evCtx,
Context: srcRange,
}
}

tag := types.Tag{
Key: types.HCLString{
Value: key,
//ValueDiags: kdiags,
//ValueExpr: expr.KeyExpr,
},
Value: types.HCLString{
Value: val,
//ValueDiags: vdiags,
//ValueExpr: expr.ValueExpr,
},
}

// ks, err := source(expr.KeyExpr.Range(), files)
// if err == nil {
// src := string(ks)
// tag.Key.Source = &src
//}
//
// vs, err := source(expr.ValueExpr.Range(), files)
// if err == nil {
// src := string(vs)
// tag.Value.Source = &src
//}
switch val.Type() {
case cty.String, cty.Bool, cty.Number:
// These types are supported and can be safely converted to a string.
default:
fr := "<nil>"
if !val.Type().Equals(cty.NilType) {
fr = val.Type().FriendlyName()
}

// Unsupported types will be converted to a JSON string representation.
jsonData, err := json.Marshal(val, val.Type())
if err != nil {
tag.Value.ValueDiags = tag.Value.ValueDiags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Invalid value type for tag %q", tag.KeyString()),
Detail: fmt.Sprintf("Value must be a string, but got %s. Attempt to marshal to json: %s", fr, err.Error()),
Context: srcRange,
})
} else {
// Value successfully marshaled to JSON, we can store it as a string.
tag.Value.Value = cty.StringVal(string(jsonData))
}
}

return tag, nil
}
Loading