@@ -38,23 +38,14 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
38
38
continue
39
39
}
40
40
41
- // tagsObj, ok := tagsAttr.HCLAttribute().Expr.(*hclsyntax.ObjectConsExpr)
42
- // if !ok {
43
- // diags = diags.Append(&hcl.Diagnostic{
44
- // Severity: hcl.DiagError,
45
- // Summary: "Incorrect type for \"tags\" attribute",
46
- // // TODO: better error message for types
47
- // Detail: fmt.Sprintf(`"tags" attribute must be an 'ObjectConsExpr', but got %T`, tagsAttr.HCLAttribute().Expr),
48
- // Subject: &tagsAttr.HCLAttribute().NameRange,
49
- // Context: &tagsAttr.HCLAttribute().Range,
50
- // Expression: tagsAttr.HCLAttribute().Expr,
51
- // EvalContext: block.Context().Inner(),
52
- // })
53
- // continue
54
- //}
55
-
56
41
var tags []types.Tag
57
42
tagsValue .ForEachElement (func (key cty.Value , val cty.Value ) (stop bool ) {
43
+ if val .IsNull () {
44
+ // null tags with null values are omitted
45
+ // This matches the behavior of `terraform apply``
46
+ return false
47
+ }
48
+
58
49
r := tagsAttr .HCLAttribute ().Expr .Range ()
59
50
tag , tagDiag := newTag (& r , files , key , val )
60
51
if tagDiag != nil {
@@ -66,15 +57,7 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
66
57
67
58
return false
68
59
})
69
- // for _, item := range tagsObj.Items {
70
- // tag, tagDiag := newTag(tagsObj, files, item, evCtx)
71
- // if tagDiag != nil {
72
- // diags = diags.Append(tagDiag)
73
- // continue
74
- // }
75
- //
76
- // tags = append(tags, tag)
77
- //}
60
+
78
61
tagBlocks = append (tagBlocks , types.TagBlock {
79
62
Tags : tags ,
80
63
Block : block ,
@@ -87,71 +70,41 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
87
70
88
71
// newTag creates a workspace tag from its hcl expression.
89
72
func newTag (srcRange * hcl.Range , _ map [string ]* hcl.File , key , val cty.Value ) (types.Tag , * hcl.Diagnostic ) {
90
- // key, kdiags := expr.KeyExpr.Value(evCtx)
91
- // val, vdiags := expr.ValueExpr.Value(evCtx)
92
-
93
- // TODO: ???
94
-
95
- // if kdiags.HasErrors() {
96
- // key = cty.UnknownVal(cty.String)
97
- //}
98
- // if vdiags.HasErrors() {
99
- // val = cty.UnknownVal(cty.String)
100
- //}
101
-
102
73
if key .IsKnown () && key .Type () != cty .String {
103
74
return types.Tag {}, & hcl.Diagnostic {
104
75
Severity : hcl .DiagError ,
105
76
Summary : "Invalid key type for tags" ,
106
77
Detail : fmt .Sprintf ("Key must be a string, but got %s" , key .Type ().FriendlyName ()),
107
- //Subject: &r,
108
- Context : srcRange ,
109
- //Expression: expr.KeyExpr,
110
- //EvalContext: evCtx,
111
- }
112
- }
113
-
114
- if val .IsKnown () && val .Type () != cty .String {
115
- fr := "<nil>"
116
- if ! val .Type ().Equals (cty .NilType ) {
117
- fr = val .Type ().FriendlyName ()
118
- }
119
- // r := expr.ValueExpr.Range()
120
- return types.Tag {}, & hcl.Diagnostic {
121
- Severity : hcl .DiagError ,
122
- Summary : "Invalid value type for tag" ,
123
- Detail : fmt .Sprintf ("Value must be a string, but got %s" , fr ),
124
- //Subject: &r,
125
- Context : srcRange ,
126
- //Expression: expr.ValueExpr,
127
- //EvalContext: evCtx,
78
+ Context : srcRange ,
128
79
}
129
80
}
130
81
131
82
tag := types.Tag {
132
83
Key : types.HCLString {
133
84
Value : key ,
134
- //ValueDiags: kdiags,
135
- //ValueExpr: expr.KeyExpr,
136
85
},
137
86
Value : types.HCLString {
138
87
Value : val ,
139
- //ValueDiags: vdiags,
140
- //ValueExpr: expr.ValueExpr,
141
88
},
142
89
}
143
90
144
- // ks, err := source(expr.KeyExpr.Range(), files)
145
- // if err == nil {
146
- // src := string(ks)
147
- // tag.Key.Source = &src
148
- //}
149
- //
150
- // vs, err := source(expr.ValueExpr.Range(), files)
151
- // if err == nil {
152
- // src := string(vs)
153
- // tag.Value.Source = &src
154
- //}
91
+ switch val .Type () {
92
+ case cty .String , cty .Bool , cty .Number :
93
+ // These types are supported and can be safely converted to a string.
94
+ default :
95
+ fr := "<nil>"
96
+ if ! val .Type ().Equals (cty .NilType ) {
97
+ fr = val .Type ().FriendlyName ()
98
+ }
99
+
100
+ // Unsupported types will be treated as errors.
101
+ tag .Value .ValueDiags = tag .Value .ValueDiags .Append (& hcl.Diagnostic {
102
+ Severity : hcl .DiagError ,
103
+ Summary : fmt .Sprintf ("Invalid value type for tag %q" , tag .KeyString ()),
104
+ Detail : fmt .Sprintf ("Value must be a string, but got %s." , fr ),
105
+ Context : srcRange ,
106
+ })
107
+ }
155
108
156
109
return tag , nil
157
110
}
0 commit comments