Skip to content

Commit 8e18e29

Browse files
committed
Remove usage of hcl diagnostics
1 parent 5f3e096 commit 8e18e29

File tree

15 files changed

+101
-516
lines changed

15 files changed

+101
-516
lines changed

internal/addrs/module_instance.go

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ func parseModuleInstance(traversal hcl.Traversal) (ModuleInstance, tfdiags.Diagn
3030
mi, remain, diags := parseModuleInstancePrefix(traversal)
3131
if len(remain) != 0 {
3232
if len(remain) == len(traversal) {
33-
diags = diags.Append(&hcl.Diagnostic{
34-
Severity: hcl.DiagError,
35-
Summary: "Invalid module instance address",
36-
Detail: "A module instance address must begin with \"module.\".",
37-
Subject: remain.SourceRange().Ptr(),
38-
})
33+
diags = append(diags, tfdiags.Diag(
34+
tfdiags.Error,
35+
"Invalid module instance address",
36+
"A module instance address must begin with \"module.\".",
37+
))
3938
} else {
40-
diags = diags.Append(&hcl.Diagnostic{
41-
Severity: hcl.DiagError,
42-
Summary: "Invalid module instance address",
43-
Detail: "The module instance address is followed by additional invalid content.",
44-
Subject: remain.SourceRange().Ptr(),
45-
})
39+
diags = append(diags, tfdiags.Diag(
40+
tfdiags.Error,
41+
"Invalid module instance address",
42+
"The module instance address is followed by additional invalid content.",
43+
))
4644
}
4745
}
4846
return mi, diags
@@ -67,13 +65,16 @@ func ParseModuleInstanceStr(str string) (ModuleInstance, tfdiags.Diagnostics) {
6765
var diags tfdiags.Diagnostics
6866

6967
traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(str), "", hcl.Pos{Line: 1, Column: 1})
70-
diags = diags.Append(parseDiags)
68+
for _, err := range parseDiags.Errs() {
69+
// ignore warnings, they don't matter in this case
70+
diags = append(diags, tfdiags.FromError(err))
71+
}
7172
if parseDiags.HasErrors() {
7273
return nil, diags
7374
}
7475

7576
addr, addrDiags := parseModuleInstance(traversal)
76-
diags = diags.Append(addrDiags)
77+
diags = append(diags, addrDiags...)
7778
return addr, diags
7879
}
7980

@@ -90,31 +91,28 @@ func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Tra
9091
case hcl.TraverseAttr:
9192
next = tt.Name
9293
default:
93-
diags = diags.Append(&hcl.Diagnostic{
94-
Severity: hcl.DiagError,
95-
Summary: "Invalid address operator",
96-
Detail: "Module address prefix must be followed by dot and then a name.",
97-
Subject: remain[0].SourceRange().Ptr(),
98-
})
94+
diags = append(diags, tfdiags.Diag(
95+
tfdiags.Error,
96+
"Invalid address operator",
97+
"Module address prefix must be followed by dot and then a name.",
98+
))
9999
break
100100
}
101101

102102
if next != "module" {
103103
break
104104
}
105105

106-
kwRange := remain[0].SourceRange()
107106
remain = remain[1:]
108107
// If we have the prefix "module" then we should be followed by an
109108
// module call name, as an attribute, and then optionally an index step
110109
// giving the instance key.
111110
if len(remain) == 0 {
112-
diags = diags.Append(&hcl.Diagnostic{
113-
Severity: hcl.DiagError,
114-
Summary: "Invalid address operator",
115-
Detail: "Prefix \"module.\" must be followed by a module name.",
116-
Subject: &kwRange,
117-
})
111+
diags = append(diags, tfdiags.Diag(
112+
tfdiags.Error,
113+
"Invalid address operator",
114+
"Prefix \"module.\" must be followed by a module name.",
115+
))
118116
break
119117
}
120118

@@ -123,12 +121,11 @@ func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Tra
123121
case hcl.TraverseAttr:
124122
moduleName = tt.Name
125123
default:
126-
diags = diags.Append(&hcl.Diagnostic{
127-
Severity: hcl.DiagError,
128-
Summary: "Invalid address operator",
129-
Detail: "Prefix \"module.\" must be followed by a module name.",
130-
Subject: remain[0].SourceRange().Ptr(),
131-
})
124+
diags = append(diags, tfdiags.Diag(
125+
tfdiags.Error,
126+
"Invalid address operator",
127+
"Prefix \"module.\" must be followed by a module name.",
128+
))
132129
break
133130
}
134131
remain = remain[1:]
@@ -149,21 +146,19 @@ func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Tra
149146
if err == nil {
150147
step.InstanceKey = intKey(idxInt)
151148
} else {
152-
diags = diags.Append(&hcl.Diagnostic{
153-
Severity: hcl.DiagError,
154-
Summary: "Invalid address operator",
155-
Detail: fmt.Sprintf("Invalid module index: %s.", err),
156-
Subject: idx.SourceRange().Ptr(),
157-
})
149+
diags = append(diags, tfdiags.Diag(
150+
tfdiags.Error,
151+
"Invalid address operator",
152+
fmt.Sprintf("Invalid module index: %s.", err),
153+
))
158154
}
159155
default:
160156
// Should never happen, because no other types are allowed in traversal indices.
161-
diags = diags.Append(&hcl.Diagnostic{
162-
Severity: hcl.DiagError,
163-
Summary: "Invalid address operator",
164-
Detail: "Invalid module key: must be either a string or an integer.",
165-
Subject: idx.SourceRange().Ptr(),
166-
})
157+
diags = append(diags, tfdiags.Diag(
158+
tfdiags.Error,
159+
"Invalid address operator",
160+
"Invalid module key: must be either a string or an integer.",
161+
))
167162
}
168163
}
169164
}

internal/addrs/provider_config.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,46 +58,42 @@ func ParseAbsProviderConfig(traversal hcl.Traversal) (AbsProviderConfig, tfdiags
5858
Module: modInst,
5959
}
6060
if len(remain) < 2 || remain.RootName() != "provider" {
61-
diags = diags.Append(&hcl.Diagnostic{
62-
Severity: hcl.DiagError,
63-
Summary: "Invalid provider configuration address",
64-
Detail: "Provider address must begin with \"provider.\", followed by a provider type name.",
65-
Subject: remain.SourceRange().Ptr(),
66-
})
61+
diags = append(diags, tfdiags.Diag(
62+
tfdiags.Error,
63+
"Invalid provider configuration address",
64+
"Provider address must begin with \"provider.\", followed by a provider type name.",
65+
))
6766
return ret, diags
6867
}
6968
if len(remain) > 3 {
70-
diags = diags.Append(&hcl.Diagnostic{
71-
Severity: hcl.DiagError,
72-
Summary: "Invalid provider configuration address",
73-
Detail: "Extraneous operators after provider configuration alias.",
74-
Subject: hcl.Traversal(remain[3:]).SourceRange().Ptr(),
75-
})
69+
diags = append(diags, tfdiags.Diag(
70+
tfdiags.Error,
71+
"Invalid provider configuration address",
72+
"Extraneous operators after provider configuration alias.",
73+
))
7674
return ret, diags
7775
}
7876

7977
if tt, ok := remain[1].(hcl.TraverseAttr); ok {
8078
ret.ProviderConfig.Type = tt.Name
8179
} else {
82-
diags = diags.Append(&hcl.Diagnostic{
83-
Severity: hcl.DiagError,
84-
Summary: "Invalid provider configuration address",
85-
Detail: "The prefix \"provider.\" must be followed by a provider type name.",
86-
Subject: remain[1].SourceRange().Ptr(),
87-
})
80+
diags = append(diags, tfdiags.Diag(
81+
tfdiags.Error,
82+
"Invalid provider configuration address",
83+
"The prefix \"provider.\" must be followed by a provider type name.",
84+
))
8885
return ret, diags
8986
}
9087

9188
if len(remain) == 3 {
9289
if tt, ok := remain[2].(hcl.TraverseAttr); ok {
9390
ret.ProviderConfig.Alias = tt.Name
9491
} else {
95-
diags = diags.Append(&hcl.Diagnostic{
96-
Severity: hcl.DiagError,
97-
Summary: "Invalid provider configuration address",
98-
Detail: "Provider type name must be followed by a configuration alias name.",
99-
Subject: remain[2].SourceRange().Ptr(),
100-
})
92+
diags = append(diags, tfdiags.Diag(
93+
tfdiags.Error,
94+
"Invalid provider configuration address",
95+
"Provider type name must be followed by a configuration alias name.",
96+
))
10197
return ret, diags
10298
}
10399
}
@@ -124,13 +120,16 @@ func ParseAbsProviderConfigStr(str string) (AbsProviderConfig, tfdiags.Diagnosti
124120
var diags tfdiags.Diagnostics
125121

126122
traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(str), "", hcl.Pos{Line: 1, Column: 1})
127-
diags = diags.Append(parseDiags)
123+
for _, err := range parseDiags.Errs() {
124+
// ignore warnings, they don't matter in this case
125+
diags = append(diags, tfdiags.FromError(err))
126+
}
128127
if parseDiags.HasErrors() {
129128
return AbsProviderConfig{}, diags
130129
}
131130

132131
addr, addrDiags := ParseAbsProviderConfig(traversal)
133-
diags = diags.Append(addrDiags)
132+
diags = append(diags, addrDiags...)
134133
return addr, diags
135134
}
136135

internal/plugin/convert/diagnostics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func ProtoToDiagnostics(ds []*proto.Diagnostic) tfdiags.Diagnostics {
7373
newDiag = tfdiags.WholeContainingBody(severity, d.Summary, d.Detail)
7474
}
7575

76-
diags = diags.Append(newDiag)
76+
diags = append(diags, newDiag)
7777
}
7878

7979
return diags

internal/tfdiags/contextual.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ func GetAttribute(d Diagnostic) cty.Path {
5454
type attributeDiagnostic struct {
5555
diagnosticBase
5656
attrPath cty.Path
57-
subject *SourceRange // populated only after ElaborateFromConfigBody
58-
}
59-
60-
func (d *attributeDiagnostic) Source() Source {
61-
return Source{
62-
Subject: d.subject,
63-
}
6457
}
6558

6659
// WholeContainingBody returns a diagnostic about the body that is an implied
@@ -85,11 +78,4 @@ func WholeContainingBody(severity Severity, summary, detail string) Diagnostic {
8578

8679
type wholeBodyDiagnostic struct {
8780
diagnosticBase
88-
subject *SourceRange // populated only after ElaborateFromConfigBody
89-
}
90-
91-
func (d *wholeBodyDiagnostic) Source() Source {
92-
return Source{
93-
Subject: d.subject,
94-
}
9581
}

internal/tfdiags/diagnostic.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tfdiags
33
type Diagnostic interface {
44
Severity() Severity
55
Description() Description
6-
Source() Source
76
}
87

98
type Severity rune
@@ -19,8 +18,3 @@ type Description struct {
1918
Summary string
2019
Detail string
2120
}
22-
23-
type Source struct {
24-
Subject *SourceRange
25-
Context *SourceRange
26-
}

internal/tfdiags/diagnostic_base.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func (d diagnosticBase) Description() Description {
2222
}
2323
}
2424

25-
func (d diagnosticBase) Source() Source {
26-
return Source{}
25+
func Diag(sev Severity, summary, detail string) Diagnostic {
26+
return &diagnosticBase{
27+
severity: sev,
28+
summary: summary,
29+
detail: detail,
30+
}
2731
}

0 commit comments

Comments
 (0)