Skip to content

Commit b4931a5

Browse files
feat(diag): migrates Diagnostics to a struct to track error state.
Features: * Adds a number of new methods, `Len()`, `Merge(diags Diagnostics)` and `All() []Diagnostic`. * Adds properties `HasFatal`, `HasError` and `HasWarn` to detect if a >=error level diagnostic has been recorded. Migration: * `diags = append(diags, diag)` should migrate to `diags.Append(diag)`. * `diags = append(diags, newDiags...)` should migrate to `diags.Merge(newDiags)`. * `len(diags)` should migrate to `diags.Len()`. * any calls wanting all diagnostic messages must call `diags.All()`.
1 parent 53386d5 commit b4931a5

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

diag/diagnostics.go

+64-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
package diag
22

33
// Diagnostics contains a report of diagnostic information.
4-
type Diagnostics []Diagnostic
4+
type Diagnostics struct {
5+
// diags contains the list of diagnostic entries.
6+
diags []Diagnostic
7+
8+
// HasFatal reports if the diagnostics have reported a fatal issue.
9+
HasFatal bool
10+
// HasError reports if the diagnostics have reported an error.
11+
HasError bool
12+
// HasWarn reports if the diagnostics have reported a warning.
13+
HasWarn bool
14+
}
15+
16+
// Len reports the number of diagnostic messages logged.
17+
func (d *Diagnostics) Len() int {
18+
if d == nil {
19+
return 0
20+
}
21+
22+
return len(d.diags)
23+
}
24+
25+
// All returns all diagnostic messages.
26+
func (d *Diagnostics) All() []Diagnostic {
27+
if d == nil {
28+
return nil
29+
}
30+
31+
return d.diags
32+
}
533

634
// Append adds a number of new diagnostic entries to the diagnostics.
735
func (d *Diagnostics) Append(diags ...Diagnostic) {
@@ -10,7 +38,38 @@ func (d *Diagnostics) Append(diags ...Diagnostic) {
1038
return
1139
}
1240

13-
*d = append(*d, diags...)
41+
for _, diag := range diags {
42+
switch diag.Severity {
43+
case SeverityFatal:
44+
d.HasFatal = true
45+
case SeverityError:
46+
d.HasError = true
47+
case SeverityWarn:
48+
d.HasWarn = true
49+
}
50+
51+
d.diags = append(d.diags, diag)
52+
}
53+
}
54+
55+
// Merge appends the provided diags into the diagnostics.
56+
func (d *Diagnostics) Merge(diags Diagnostics) {
57+
if diags.Len() == 0 {
58+
// Nothing to append!
59+
return
60+
}
61+
62+
if diags.HasFatal {
63+
d.HasFatal = diags.HasFatal
64+
}
65+
if diags.HasError {
66+
d.HasError = diags.HasError
67+
}
68+
if diags.HasWarn {
69+
d.HasWarn = diags.HasWarn
70+
}
71+
72+
d.diags = append(d.diags, diags.All()...)
1473
}
1574

1675
// GlobalFile enables building up a diagnostic message for a global
@@ -90,14 +149,14 @@ func (d *Diagnostics) Traces() Diagnostics {
90149

91150
// getDiagsWithLevel returns an array of diagnostics that match the specified
92151
// severity level.
93-
func (d Diagnostics) getDiagsWithLevel(sev Severity) Diagnostics {
152+
func (d *Diagnostics) getDiagsWithLevel(sev Severity) Diagnostics {
94153
var diags Diagnostics
95-
for _, diag := range d {
154+
for _, diag := range d.diags {
96155
if diag.Severity != sev {
97156
continue
98157
}
99158

100-
diags = append(diags, diag)
159+
diags.Append(diag)
101160
}
102161

103162
return diags

0 commit comments

Comments
 (0)