/
diagnostics.go
97 lines (79 loc) · 2.66 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package diag
import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Diagnostics represents a collection of diagnostics.
//
// While this collection is ordered, the order is not guaranteed as reliable
// or consistent.
type Diagnostics []Diagnostic
// AddAttributeError adds a generic attribute error diagnostic to the collection.
func (diags *Diagnostics) AddAttributeError(path *tftypes.AttributePath, summary string, detail string) {
diags.Append(NewAttributeErrorDiagnostic(path, summary, detail))
}
// AddAttributeWarning adds a generic attribute warning diagnostic to the collection.
func (diags *Diagnostics) AddAttributeWarning(path *tftypes.AttributePath, summary string, detail string) {
diags.Append(NewAttributeWarningDiagnostic(path, summary, detail))
}
// AddError adds a generic error diagnostic to the collection.
func (diags *Diagnostics) AddError(summary string, detail string) {
diags.Append(NewErrorDiagnostic(summary, detail))
}
// AddWarning adds a generic warning diagnostic to the collection.
func (diags *Diagnostics) AddWarning(summary string, detail string) {
diags.Append(NewWarningDiagnostic(summary, detail))
}
// Append adds non-empty and non-duplicate diagnostics to the collection.
func (diags *Diagnostics) Append(in ...Diagnostic) {
for _, diag := range in {
if diag == nil {
continue
}
if diags.Contains(diag) {
continue
}
if diags == nil {
*diags = Diagnostics{diag}
} else {
*diags = append(*diags, diag)
}
}
}
// Contains returns true if the collection contains an equal Diagnostic.
func (diags Diagnostics) Contains(in Diagnostic) bool {
for _, diag := range diags {
if diag.Equal(in) {
return true
}
}
return false
}
// HasError returns true if the collection has an error severity Diagnostic.
func (diags Diagnostics) HasError() bool {
for _, diag := range diags {
if diag.Severity() == SeverityError {
return true
}
}
return false
}
// ToTfprotov6Diagnostics converts the diagnostics into the tfprotov6 collection type.
//
// Usage of this method outside the framework is not supported nor considered
// for backwards compatibility promises.
func (diags Diagnostics) ToTfprotov6Diagnostics() []*tfprotov6.Diagnostic {
var results []*tfprotov6.Diagnostic
for _, diag := range diags {
tfprotov6Diagnostic := &tfprotov6.Diagnostic{
Detail: diag.Detail(),
Severity: diag.Severity().ToTfprotov6DiagnosticSeverity(),
Summary: diag.Summary(),
}
if diagWithPath, ok := diag.(DiagnosticWithPath); ok {
tfprotov6Diagnostic.Attribute = diagWithPath.Path()
}
results = append(results, tfprotov6Diagnostic)
}
return results
}