From 849c539243a7a0acc4ed3e54758e3a4ba6b2da1e Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 27 Jun 2025 12:40:03 -0500 Subject: [PATCH 1/2] feat: allow bool + number tag values Tag values accept bool & number in coder/coder. --- preview_test.go | 17 ++++++++++++++++- testdata/sometags/main.tf | 29 +++++++++++++++++++++++++++++ testdata/sometags/skipe2e | 1 + workspacetags.go | 36 +++++++++++++++++++----------------- 4 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 testdata/sometags/main.tf create mode 100644 testdata/sometags/skipe2e diff --git a/preview_test.go b/preview_test.go index b0ea9b3..5c1d2f1 100644 --- a/preview_test.go +++ b/preview_test.go @@ -49,13 +49,28 @@ 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", + }, + unknownTags: []string{ + "map", "list", "null", + }, + }, { name: "simple static values", dir: "static", expTags: map[string]string{ "zone": "developers", }, - unknownTags: []string{}, + unknownTags: []string{ + "list", + }, params: map[string]assertParam{ "region": ap().value("us"). def("us"). diff --git a/testdata/sometags/main.tf b/testdata/sometags/main.tf new file mode 100644 index 0000000..04c7721 --- /dev/null +++ b/testdata/sometags/main.tf @@ -0,0 +1,29 @@ +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" + } + "null" = null + } +} + + +data "coder_workspace_tags" "custom_workspace_tags" { + tags = { + "extra" = "bar" + } +} diff --git a/testdata/sometags/skipe2e b/testdata/sometags/skipe2e new file mode 100644 index 0000000..850fdfc --- /dev/null +++ b/testdata/sometags/skipe2e @@ -0,0 +1 @@ +Bad tags are not going to play well with terraform apply. \ No newline at end of file diff --git a/workspacetags.go b/workspacetags.go index d3f6c76..10cb47a 100644 --- a/workspacetags.go +++ b/workspacetags.go @@ -120,23 +120,6 @@ func newTag(srcRange *hcl.Range, _ map[string]*hcl.File, key, val cty.Value) (ty } } - if val.IsKnown() && val.Type() != cty.String { - fr := "" - 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, - } - } - tag := types.Tag{ Key: types.HCLString{ Value: key, @@ -150,6 +133,25 @@ func newTag(srcRange *hcl.Range, _ map[string]*hcl.File, key, val cty.Value) (ty }, } + // If the value is known, but the type is not a string, bool, or number. + // Then throw an error. Only the supported types can safely be converted to a string. + if !(val.Type() == cty.String || val.Type() == cty.Bool || val.Type() == cty.Number) { + fr := "" + if !val.Type().Equals(cty.NilType) { + fr = val.Type().FriendlyName() + } + + 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", fr), + //Subject: &r, + Context: srcRange, + //Expression: expr.ValueExpr, + //EvalContext: evCtx, + }) + } + // ks, err := source(expr.KeyExpr.Range(), files) // if err == nil { // src := string(ks) From 53a1fe46e3742c94267da78b23295819d62931c0 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 27 Jun 2025 13:01:42 -0500 Subject: [PATCH 2/2] fix bad test assert --- preview_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/preview_test.go b/preview_test.go index 5c1d2f1..45dbc75 100644 --- a/preview_test.go +++ b/preview_test.go @@ -68,9 +68,6 @@ func Test_Extract(t *testing.T) { expTags: map[string]string{ "zone": "developers", }, - unknownTags: []string{ - "list", - }, params: map[string]assertParam{ "region": ap().value("us"). def("us").