/
diagnostics.go
122 lines (98 loc) · 3.08 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package diag
import (
"github.com/hashicorp/terraform-plugin-framework/path"
)
// 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 path.Path, 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 path.Path, 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
}
*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
}
// Equal returns true if all given diagnostics are equivalent in order and
// content, based on the underlying (Diagnostic).Equal() method of each.
func (diags Diagnostics) Equal(other Diagnostics) bool {
if len(diags) != len(other) {
return false
}
for diagIndex, diag := range diags {
if !diag.Equal(other[diagIndex]) {
return false
}
}
return true
}
// 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
}
// ErrorsCount returns the number of Diagnostic in Diagnostics that are SeverityError.
func (diags Diagnostics) ErrorsCount() int {
return len(diags.Errors())
}
// WarningsCount returns the number of Diagnostic in Diagnostics that are SeverityWarning.
func (diags Diagnostics) WarningsCount() int {
return len(diags.Warnings())
}
// Errors returns all the Diagnostic in Diagnostics that are SeverityError.
func (diags Diagnostics) Errors() Diagnostics {
dd := Diagnostics{}
for _, d := range diags {
if SeverityError == d.Severity() {
dd = append(dd, d)
}
}
return dd
}
// Warnings returns all the Diagnostic in Diagnostics that are SeverityWarning.
func (diags Diagnostics) Warnings() Diagnostics {
dd := Diagnostics{}
for _, d := range diags {
if SeverityWarning == d.Severity() {
dd = append(dd, d)
}
}
return dd
}