Skip to content

Commit 849c539

Browse files
committed
feat: allow bool + number tag values
Tag values accept bool & number in coder/coder.
1 parent fd019fc commit 849c539

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

preview_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,28 @@ func Test_Extract(t *testing.T) {
4949
dir: "badparam",
5050
failPreview: true,
5151
},
52+
{
53+
name: "sometags",
54+
dir: "sometags",
55+
expTags: map[string]string{
56+
"string": "foo",
57+
"number": "42",
58+
"bool": "true",
59+
"extra": "bar",
60+
},
61+
unknownTags: []string{
62+
"map", "list", "null",
63+
},
64+
},
5265
{
5366
name: "simple static values",
5467
dir: "static",
5568
expTags: map[string]string{
5669
"zone": "developers",
5770
},
58-
unknownTags: []string{},
71+
unknownTags: []string{
72+
"list",
73+
},
5974
params: map[string]assertParam{
6075
"region": ap().value("us").
6176
def("us").

testdata/sometags/main.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = "2.4.0-pre0"
6+
}
7+
}
8+
}
9+
10+
data "coder_workspace_tags" "custom_workspace_tags" {
11+
tags = {
12+
"string" = "foo"
13+
"number" = 42
14+
"bool" = true
15+
"list" = ["a", "b", "c"]
16+
"map" = {
17+
"key1" = "value1"
18+
"key2" = "value2"
19+
}
20+
"null" = null
21+
}
22+
}
23+
24+
25+
data "coder_workspace_tags" "custom_workspace_tags" {
26+
tags = {
27+
"extra" = "bar"
28+
}
29+
}

testdata/sometags/skipe2e

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bad tags are not going to play well with terraform apply.

workspacetags.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,6 @@ func newTag(srcRange *hcl.Range, _ map[string]*hcl.File, key, val cty.Value) (ty
120120
}
121121
}
122122

123-
if val.IsKnown() && val.Type() != cty.String {
124-
fr := "<nil>"
125-
if !val.Type().Equals(cty.NilType) {
126-
fr = val.Type().FriendlyName()
127-
}
128-
// r := expr.ValueExpr.Range()
129-
return types.Tag{}, &hcl.Diagnostic{
130-
Severity: hcl.DiagError,
131-
Summary: "Invalid value type for tag",
132-
Detail: fmt.Sprintf("Value must be a string, but got %s", fr),
133-
//Subject: &r,
134-
Context: srcRange,
135-
//Expression: expr.ValueExpr,
136-
//EvalContext: evCtx,
137-
}
138-
}
139-
140123
tag := types.Tag{
141124
Key: types.HCLString{
142125
Value: key,
@@ -150,6 +133,25 @@ func newTag(srcRange *hcl.Range, _ map[string]*hcl.File, key, val cty.Value) (ty
150133
},
151134
}
152135

136+
// If the value is known, but the type is not a string, bool, or number.
137+
// Then throw an error. Only the supported types can safely be converted to a string.
138+
if !(val.Type() == cty.String || val.Type() == cty.Bool || val.Type() == cty.Number) {
139+
fr := "<nil>"
140+
if !val.Type().Equals(cty.NilType) {
141+
fr = val.Type().FriendlyName()
142+
}
143+
144+
tag.Value.ValueDiags = tag.Value.ValueDiags.Append(&hcl.Diagnostic{
145+
Severity: hcl.DiagError,
146+
Summary: fmt.Sprintf("Invalid value type for tag %q", tag.KeyString()),
147+
Detail: fmt.Sprintf("Value must be a string, but got %s", fr),
148+
//Subject: &r,
149+
Context: srcRange,
150+
//Expression: expr.ValueExpr,
151+
//EvalContext: evCtx,
152+
})
153+
}
154+
153155
// ks, err := source(expr.KeyExpr.Range(), files)
154156
// if err == nil {
155157
// src := string(ks)

0 commit comments

Comments
 (0)