@@ -38,23 +38,14 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
3838 continue
3939 }
4040
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-
5641 var tags []types.Tag
5742 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+
5849 r := tagsAttr .HCLAttribute ().Expr .Range ()
5950 tag , tagDiag := newTag (& r , files , key , val )
6051 if tagDiag != nil {
@@ -66,15 +57,7 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
6657
6758 return false
6859 })
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+
7861 tagBlocks = append (tagBlocks , types.TagBlock {
7962 Tags : tags ,
8063 Block : block ,
@@ -87,71 +70,41 @@ func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
8770
8871// newTag creates a workspace tag from its hcl expression.
8972func 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-
10273 if key .IsKnown () && key .Type () != cty .String {
10374 return types.Tag {}, & hcl.Diagnostic {
10475 Severity : hcl .DiagError ,
10576 Summary : "Invalid key type for tags" ,
10677 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 ,
12879 }
12980 }
13081
13182 tag := types.Tag {
13283 Key : types.HCLString {
13384 Value : key ,
134- //ValueDiags: kdiags,
135- //ValueExpr: expr.KeyExpr,
13685 },
13786 Value : types.HCLString {
13887 Value : val ,
139- //ValueDiags: vdiags,
140- //ValueExpr: expr.ValueExpr,
14188 },
14289 }
14390
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+ }
155108
156109 return tag , nil
157110}
0 commit comments